class Novaposhta::Base

Public Class Methods

class_attr_accessor(*names) click to toggle source
# File lib/novaposhta/base.rb, line 3
def self.class_attr_accessor(*names)
  names.each do |name|
    define_singleton_method("#{name.to_s}=".to_sym){ |attr| class_variable_set("@@#{name.to_s}", attr) }
    define_singleton_method(name.to_sym){ class_variable_get("@@#{name.to_s}") }
  end
end
make_body(model, method, options={}) click to toggle source
# File lib/novaposhta/base.rb, line 24
def self.make_body(model, method, options={})
  {
    "modelName":        model,
    "calledMethod":     method,
    "methodProperties": options,
    "apiKey":           api_key
  }
end
post_request(body) click to toggle source
# File lib/novaposhta/base.rb, line 12
def self.post_request(body)
  uri      = URI( url.gsub(/\{format\}/, "#{format.to_s}") )
  request  = Net::HTTP::Post.new(uri)
  format_request!(request, body)
  response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
    http.request request
  end
  format_response(response)
rescue
  nil
end

Private Class Methods

format_request!(request, body) click to toggle source
# File lib/novaposhta/base.rb, line 35
def self.format_request!(request, body)
  if format.to_sym == :json
    request.content_type = 'application/json'
    request.body         = body.to_json
  elsif format.to_sym == :xml
    request.content_type = 'text/xml'
    request.body         = body.to_xml.gsub(/\n/, '').gsub(/>\s+</, '><')
  end
end
format_response(response) click to toggle source
# File lib/novaposhta/base.rb, line 45
def self.format_response(response)
  if format.to_sym == :json
    JSON.parse(response.body)
  elsif format.to_sym == :xml
    Nokogiri::XML(response.body)
  end
end