Class: CLX::API
- Inherits:
-
Object
- Object
- CLX::API
- Defined in:
- lib/clx_api/api.rb
Overview
Library class for CLX Networks REST API
Instance Attribute Summary collapse
-
#http_client ⇒ Object
Mostly for testing.
Instance Method Summary collapse
-
#get_gateway_by_id(gateway_id) ⇒ hash
Get one gateway based on it’s gateway id.
-
#get_gateways ⇒ hash
Get all gateways.
-
#get_operator_by_id(operator_id) ⇒ hash
Get one operator based on it’s operator id.
-
#get_operators ⇒ hash
Get all operators from the API.
-
#get_price_entires_by_gateway_id(gateway_id) ⇒ hash
Get price entires based on gateway id.
-
#get_price_entries_by_gateway_id_and_operator_id(gateway_id, operator_id) ⇒ hash
Get price entiry based on gateway id and operator id.
-
#get_price_entries_by_gateway_id_and_operator_id_and_date(gateway_id, operator_id, date) ⇒ hash
Get price entiry based on gateway id, operator id and a date.
-
#initialize(username, password, http_adapter = nil) ⇒ CLX::API
constructor
Initialize a new API client.
-
#set_auth(username, password) ⇒ Object
Enables change of credentials after initialization.
-
#set_base_url(url) ⇒ Object
Enables change of base URL after initialization.
-
#valid_date?(date) ⇒ Boolean
private
Validates that date is of type DateTime.
-
#valid_id?(id) ⇒ Boolean
private
Validates that in is of type integer and is grater than zero.
Constructor Details
#initialize(username, password, http_adapter = nil) ⇒ CLX::API
Initialize a new API client
18 19 20 21 22 23 |
# File 'lib/clx_api/api.rb', line 18 def initialize(username, password, http_adapter = nil) http_adapter = http_adapter.nil? ? HTTPAdapter.new : http_adapter http_adapter.set_auth(username, password) @http_client = HTTPClient.new(CLX::base_url, http_adapter) end |
Instance Attribute Details
#http_client ⇒ Object
Mostly for testing
8 9 10 |
# File 'lib/clx_api/api.rb', line 8 def http_client @http_client end |
Instance Method Details
#get_gateway_by_id(gateway_id) ⇒ hash
Get one gateway based on it’s gateway id
74 75 76 77 78 |
# File 'lib/clx_api/api.rb', line 74 def get_gateway_by_id(gateway_id) valid_id?(gateway_id) url = CLX.paths[:gateway] + '/' + gateway_id.to_s @http_client.get(url) end |
#get_gateways ⇒ hash
Get all gateways
65 66 67 68 |
# File 'lib/clx_api/api.rb', line 65 def get_gateways url = CLX.paths[:gateway] @http_client.get(url) end |
#get_operator_by_id(operator_id) ⇒ hash
Get one operator based on it’s operator id
56 57 58 59 60 |
# File 'lib/clx_api/api.rb', line 56 def get_operator_by_id(operator_id) valid_id?(operator_id) url = CLX.paths[:operator] + '/' + operator_id.to_s @http_client.get(url) end |
#get_operators ⇒ hash
Get all operators from the API
44 45 46 47 |
# File 'lib/clx_api/api.rb', line 44 def get_operators url = CLX.paths[:operator] @http_client.get(url) end |
#get_price_entires_by_gateway_id(gateway_id) ⇒ hash
Get price entires based on gateway id
84 85 86 87 88 |
# File 'lib/clx_api/api.rb', line 84 def get_price_entires_by_gateway_id(gateway_id) valid_id?(gateway_id) url = CLX.paths[:gateway] + '/' + gateway_id.to_s + '/price' @http_client.get(url) end |
#get_price_entries_by_gateway_id_and_operator_id(gateway_id, operator_id) ⇒ hash
Get price entiry based on gateway id and operator id
95 96 97 98 99 100 |
# File 'lib/clx_api/api.rb', line 95 def get_price_entries_by_gateway_id_and_operator_id(gateway_id, operator_id) valid_id?(gateway_id) valid_id?(operator_id) url = CLX.paths[:gateway] + '/' + gateway_id.to_s + '/price/' + operator_id.to_s @http_client.get(url) end |
#get_price_entries_by_gateway_id_and_operator_id_and_date(gateway_id, operator_id, date) ⇒ hash
Get price entiry based on gateway id, operator id and a date
108 109 110 111 112 113 114 115 |
# File 'lib/clx_api/api.rb', line 108 def get_price_entries_by_gateway_id_and_operator_id_and_date(gateway_id, operator_id, date) valid_id?(gateway_id) valid_id?(operator_id) valid_date?(date) date_query = "?date=#{date.year}-#{date.month}-#{date.day}" url = CLX.paths[:gateway] + '/' + gateway_id.to_s + '/price/' + operator_id.to_s + '/' + date_query @http_client.get(url) end |
#set_auth(username, password) ⇒ Object
Enables change of credentials after initialization
28 29 30 |
# File 'lib/clx_api/api.rb', line 28 def set_auth(username, password) @http_client.http_adapter.set_auth(username, password) end |
#set_base_url(url) ⇒ Object
Enables change of base URL after initialization
34 35 36 |
# File 'lib/clx_api/api.rb', line 34 def set_base_url(url) @http_client.base_url = url end |
#valid_date?(date) ⇒ Boolean (private)
Validates that date is of type DateTime
126 127 128 |
# File 'lib/clx_api/api.rb', line 126 def valid_date?(date) raise CLXException, 'Date must be of type DateTime' unless date.is_a? DateTime end |
#valid_id?(id) ⇒ Boolean (private)
Validates that in is of type integer and is grater than zero
120 121 122 123 |
# File 'lib/clx_api/api.rb', line 120 def valid_id?(id) raise CLXException, 'Id must be integer' unless id.is_a? Integer raise CLXException, 'Id must be greater than zero' unless id > 0 end |