Class: Luminate::Base

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

Direct Known Subclasses

Constituent, Donation

Class Method Summary collapse

Class Method Details

.def_endpoints(api, *endpoints) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/luminate/base.rb', line 10

def self.def_endpoints(api,*endpoints)
  endpoints.each do |endpoint|
    method_name = endpoint.gsub(/(.)([A-Z])/,'\1_\2').downcase
    define_singleton_method method_name do |params|
      send_request(api,params.merge({method: endpoint}))
    end
  end
end

.parse_response(raw) ⇒ Object

Converts the API response to a Hash



45
46
47
48
# File 'lib/luminate/base.rb', line 45

def self.parse_response(raw)
  return {} if raw.to_s.empty?
  ::MultiJson.decode(raw)
end

.send_request(api = '', params = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/luminate/base.rb', line 19

def self.send_request(api = '', params = {})
  raise 'No API specified' if api.empty?
  raise 'No method specified' if params[:method].nil?

  config = Luminate.config
  params.merge!(response_format: 'json',
                v: '1.0',
                api_key: config.api_key,
                login_name: config.,
                login_password: config.api_password)

  url = URI.join(config.url,"#{config.organization}/",'site/',api)

  http = Net::HTTP.new(url.host, url.port)

  if url.port == 443
    http.use_ssl = true
  end

  req = Net::HTTP::Post.new(url.path)
  req.set_form_data(params)
  res = http.start { |http| http.request(req) }
  Map.for(parse_response(res.body))
end