class NovaPoshta::Api

Constants

API_URL

Attributes

api_key[RW]

Public Class Methods

new() click to toggle source
# File lib/nova_poshta/api.rb, line 11
def initialize
  self.api_key = NovaPoshta.configuration.api_key
end

Public Instance Methods

request(model_name, called_method, method_properties={}) click to toggle source
# File lib/nova_poshta/api.rb, line 15
def request(model_name, called_method, method_properties={})
  response = post(request_body(model_name, called_method, method_properties))
  ::NovaPoshta::Result.new(response, called_method)
end
request_body(model_name, called_method, method_props={}) click to toggle source
# File lib/nova_poshta/api.rb, line 20
def request_body(model_name, called_method, method_props={})
  camelize_keys(
      {
          api_key: api_key,
          model_name: model_name,
          called_method: called_method,
          method_properties: method_properties(method_props)
      }, :lower
  ).to_json
end

Protected Instance Methods

camelize_keys(h, first_letter = :upper) click to toggle source
# File lib/nova_poshta/api.rb, line 46
def camelize_keys(h, first_letter = :upper)
  h.keys.each do |k|
    new_key = k.to_s.camelize(first_letter)
    new_key = new_key.to_sym if k.is_a? Symbol
    h[new_key] = h.delete(k)
  end
  h
end
method_properties(method_props) click to toggle source
# File lib/nova_poshta/api.rb, line 33
def method_properties(method_props)
  camelize_keys(method_props, :upper)
end
post(req_body) click to toggle source
# File lib/nova_poshta/api.rb, line 37
def post(req_body)
  uri = URI.parse(API_URL)
  https = Net::HTTP.new(uri.host, uri.port)
  https.use_ssl = true if uri.scheme == 'https'
  req = Net::HTTP::Post.new(uri.path)
  req.body = req_body
  https.request(req).body
end