Class: Novaposhta::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/novaposhta/base.rb

Direct Known Subclasses

Address

Class Method Summary collapse

Class Method Details

.class_attr_accessor(*names) ⇒ Object



3
4
5
6
7
8
# 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 = {}) ⇒ Object



24
25
26
27
28
29
30
31
# 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) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# 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