Class: BeelineIot::Client

Inherits:
Object
  • Object
show all
Extended by:
Methods
Includes:
Methods
Defined in:
lib/beeline_iot/client.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Methods

communication_plans, get_sims, rate_plans, sim_list

Constructor Details

#initialize(auth_data = {}) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/beeline_iot/client.rb', line 14

def initialize(auth_data = {})
  @username = auth_data[:username]
  @password = auth_data[:password]
  @client_id = auth_data[:client_id]
  @client_secret = auth_data[:client_secret]
  @grant_type = auth_data[:grant_type]
  @token_type = auth_data[:token_type]
  @expires_in = auth_data[:expires_in]
  @access_token = auth_data[:access_token]
  @refresh_token = auth_data[:refresh_token]
end

Class Method Details

.login(auth_data) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/beeline_iot/client.rb', line 84

def (auth_data)
  client = BeelineIot::Client.new(auth_data)
  response = client.request :post, "/oauth/token", auth_data
  BeelineIot.username = auth_data[:username]
  BeelineIot.password = auth_data[:password]
  BeelineIot.client_id = auth_data[:client_id]
  BeelineIot.client_secret = auth_data[:client_secret]
  BeelineIot.grant_type = auth_data[:grant_type]
  BeelineIot.token_type = response["token_type"]
  BeelineIot.expires_in = response["expires_in"]
  BeelineIot.access_token = response["access_token"]
  BeelineIot.refresh_token = response["refresh_token"]
  BeelineIot
end

.request(method, path, params = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/beeline_iot/client.rb', line 99

def request(method, path, params = {})
  client = BeelineIot::Client.new(
    username: BeelineIot.username,
    password: BeelineIot.password,
    client_id: BeelineIot.client_id,
    client_secret: BeelineIot.client_secret,
    grant_type: BeelineIot.grant_type,
    token_type: BeelineIot.token_type,
    expires_in: BeelineIot.expires_in,
    access_token: BeelineIot.access_token,
    refresh_token: BeelineIot.refresh_token
  )
  client.request(method, path, params)
end

Instance Method Details

#login(auth_data) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/beeline_iot/client.rb', line 26

def (auth_data)
  return self if @access_token

  response = request :post, "/oauth/token", auth_data
  @token_type = response["token_type"]
  @expires_in = response["expires_in"]
  @access_token = response["access_token"]
  @refresh_token = response["refresh_token"]
  self
end

#request(method, path, params = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/beeline_iot/client.rb', line 37

def request(method, path, params = {})
  raise "No auth data provided" unless enough_auth_data?

  params = params.to_json
  response = client.send(method, path, params) do |request|
    add_request_headers(request, path)
  end

  result = ActiveSupport::JSON.decode(response.body)&.with_indifferent_access
  raise BeelineIot::Error, "Error [HTTP #{response.status}]: #{response.reason_phrase}" unless response.success?

  result
rescue ActiveSupport::JSON.parse_error
  raise BeelineIot::Error, "Response is not JSON: #{response.body}"
end