Class: TopsConnect::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty, Communities, Owners, Properties
Defined in:
lib/tops_connect/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Properties

#properties, #property

Methods included from Owners

#balance, #charges, #owner, #owners

Methods included from Communities

#charge_codes, #communities, #community

Constructor Details

#initialize(community_id, community_api_key) ⇒ Client



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tops_connect/client.rb', line 17

def initialize(community_id, community_api_key)
  authorization = Base64.strict_encode64 [
    TopsConnect.configuration.client_id,
    TopsConnect.configuration.software_key
  ].join(':')

  self.class.headers('authorization' => "Basic #{authorization}")

  @subscription_key = TopsConnect.configuration.subscription_key

  switch_community(community_id, community_api_key)
end

Instance Attribute Details

#community_api_keyObject (readonly)

Returns the value of attribute community_api_key.



10
11
12
# File 'lib/tops_connect/client.rb', line 10

def community_api_key
  @community_api_key
end

#community_idObject (readonly)

Returns the value of attribute community_id.



10
11
12
# File 'lib/tops_connect/client.rb', line 10

def community_id
  @community_id
end

Instance Method Details

#get(endpoint, headers: {}, query: {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tops_connect/client.rb', line 35

def get(endpoint, headers: {}, query: {})
  response = self.class.get(
    "/#{TopsConnect.configuration.zone}/api#{endpoint}",
    query: query.merge('subscription-key' => @subscription_key),
    headers: headers.merge('community-api-key' => @community_api_key)
  )

  raise_exception(response) unless response.code == 200

  response.parsed_response
end

#post(endpoint, body: {}, headers: {}, query: {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/tops_connect/client.rb', line 60

def post(endpoint, body: {}, headers: {}, query: {})
  response = self.class.post(
    "/#{TopsConnect.configuration.zone}/api#{endpoint}",
    query: query.merge('subscription-key' => @subscription_key),
    headers: headers.merge('community-api-key' => @community_api_key),
    body: body
  )

  raise_exception(response) unless response.code == 200

  response.parsed_response
end

#put(endpoint, body: {}, headers: {}, query: {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tops_connect/client.rb', line 47

def put(endpoint, body: {}, headers: {}, query: {})
  response = self.class.put(
    "/#{TopsConnect.configuration.zone}/api#{endpoint}",
    query: query.merge('subscription-key' => @subscription_key),
    headers: headers.merge('community-api-key' => @community_api_key),
    body: body
  )

  raise_exception(response) unless response.code == 204

  response.parsed_response
end

#switch_community(community_id, community_api_key) ⇒ Object



30
31
32
33
# File 'lib/tops_connect/client.rb', line 30

def switch_community(community_id, community_api_key)
  @community_id = community_id
  @community_api_key = community_api_key
end