Sha256: 72291cac79ad262b8c83504a83c6a28ff4af4fe5504e3357f883e5cae8a1f40b

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

require 'net/http'
require 'net/https'
require 'json'

module NovaPoshta
  class Api
    API_URL = 'https://api.novaposhta.ua/v2.0/json/'.freeze

    attr_accessor :api_key

    def initialize
      self.api_key = NovaPoshta.configuration.api_key
    end

    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

    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

    def method_properties(method_props)
      camelize_keys(method_props, :upper)
    end

    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

    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

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nova_poshta-0.3.0 lib/nova_poshta/api.rb