Class: PropertySolutions::ApiConsumer

Inherits:
Object
  • Object
show all
Defined in:
app/models/property_solutions/api_consumer.rb

Direct Known Subclasses

Properties, Units

Constant Summary collapse

@@field_order =
'id'
@@base =
''

Class Method Summary collapse

Class Method Details

.request(method, data = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/property_solutions/api_consumer.rb', line 7

def self.request(method, data = nil)

  data = {} if data.nil?
  data.delete_if { |k,v| v.nil? }
  
  data = {
    'auth' => {
      'type'     => "basic",
      'username' => PropertySolutions::username,
      'password' => PropertySolutions::password,
    },
    'method' => {
      'name'   => method, # required_service_name
      'params' => data # parameters required for the web service
    }
  }

  resp = Typhoeus::Request.new(
      "https://#{PropertySolutions::domain}.propertysolutions.com/api/#{@@base}",
      method:   :post,
      headers:  { 'Content-type' => 'application/json, charset: utf-8' },
      body:     data.to_json       
    ).run
  resp = JSON.parse(resp.response_body)
  if (resp['response'].nil? || !resp['response']['error'].nil? || resp['response']['result'].nil?)
    return false
  end
  return resp = resp['response']['result']
end

.sort(arr) ⇒ Object



37
38
39
# File 'app/models/property_solutions/api_consumer.rb', line 37

def self.sort(arr)
  return arr.sort { |x,y| x.send(@@field_order.to_sym) <=> y.send(@@field_order.to_sym) }
end