Class: CLX::API

Inherits:
Object
  • Object
show all
Defined in:
lib/clx_api/api.rb

Overview

Library class for CLX Networks REST API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, http_adapter = nil) ⇒ CLX::API

Initialize a new API client

Examples:

CLX::API.new 'my_username', 'my_password'

Parameters:

  • username (String)
  • password (String)
  • http_adapter (HTTPAdapter) (defaults to: nil)

    Used for testing



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_clientObject

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

Parameters:

  • gateway_id (Integer)

    Gateway ID

Returns:

  • (hash)

    gateway One operators as hash.



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_gatewayshash

Get all gateways

Returns:

  • (hash)

    gateways list of gateways as hash.



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

Examples:

clx_api = CLX::API.new 'my_username', 'my_password'
operator = clx_api.get_operator(1)

Parameters:

  • operator_id (Integer)

    Operator ID

Returns:

  • (hash)

    operator One operator as hash.



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_operatorshash

Get all operators from the API

Examples:

clx_api = CLX::API.new 'my_username', 'my_password'
operators = clx_api.get_operators

Returns:

  • (hash)

    operators list of operators as hash.



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

Parameters:

  • gateway_id (Integer)

    Gateway ID

Returns:

  • (hash)

    price_entires list of price entries as hash.



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

Parameters:

  • gateway_id (Integer)

    Gateway ID

  • operator_id (Integer)

    Operator ID

Returns:

  • (hash)

    price_entry one price entry as hash.



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

Parameters:

  • gateway_id (Integer)

    Gateway ID

  • operator_id (Integer)

    Operator ID

  • date (DateTime)

    DateTime

Returns:

  • (hash)

    price_entry one price entry as hash.



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

Parameters:

  • username (String)
  • password (String)


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

Parameters:

  • url (String)


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

Returns:

  • (Boolean)

Raises:



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

Returns:

  • (Boolean)

Raises:



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