Module: Cradlepoint

Defined in:
lib/cradlepoint.rb,
lib/cradlepoint/config.rb,
lib/cradlepoint/router.rb,
lib/cradlepoint/account.rb,
lib/cradlepoint/version.rb,
lib/cradlepoint/net_flow.rb,
lib/cradlepoint/net_device.rb,
lib/cradlepoint/wlan_survey.rb,
lib/cradlepoint/cradlepoint_object.rb

Defined Under Namespace

Classes: Account, Config, CradlepointObject, NetDevice, NetFlow, Router, WlanSurvey

Constant Summary collapse

VERSION =
"0.2.7.2"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.accountObject

Returns the value of attribute account.



18
19
20
# File 'lib/cradlepoint.rb', line 18

def 
  @account
end

.base_urlObject

Returns the value of attribute base_url.



18
19
20
# File 'lib/cradlepoint.rb', line 18

def base_url
  @base_url
end

.passwordObject

Returns the value of attribute password.



18
19
20
# File 'lib/cradlepoint.rb', line 18

def password
  @password
end

.usernameObject

Returns the value of attribute username.



18
19
20
# File 'lib/cradlepoint.rb', line 18

def username
  @username
end

Class Method Details

.authenticate(username, password) ⇒ Object



47
48
49
50
51
52
# File 'lib/cradlepoint.rb', line 47

def self.authenticate(username, password)
  self.username = username
  self.password = password
  self.  = Cradlepoint::Account.new unless self.
  true
end

.handle_response(response) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cradlepoint.rb', line 58

def self.handle_response(response)
  begin
    parsed_response = JSON.parse(response)
  rescue JSON::ParserError, TypeError
    raise "Cradlepoint received an invalid json response."
  end
  
  parsed_response['success'] ? 
    Utils::HashHelpers.symbolize_keys(parsed_response['data']) : 
    raise("Unsuccessful response received: #{ parsed_response.inspect }")  # TODO: Handle more elegantly.
end

.make_request(method, url = '', params = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cradlepoint.rb', line 23

def self.make_request(method, url = '', params = {})
  raise 'You need to call Cradlepoint.authenticate(username, password) first.' unless username and password

  params.merge!(format: :json)
  headers = { accept: :json, content_type: :json }

  response = case method
             when :get then RestClient.get(url, { params: params })
             else return false
             end
  
  handle_response(response)
rescue RestClient::Exception => e
  puts "RestClient::Exception received: #{ e.response.code }"
  return case e.response.code
         when 400 then { success: false, error_code: 400, error: e }
         when 401 then { success: false, error_code: 401, error: e }
         when 403 then { success: false, error_code: 403, error: e }
         when 404 then { success: false, error_code: 404, error: e }
         when 500 then { success: false, error_code: 500, error: e }
         else raise(e) # Not an error we are expecting.
         end
end

.url_prependObject



54
55
56
# File 'lib/cradlepoint.rb', line 54

def self.url_prepend
  "https://#{ @username }:#{ @password }@"
end