module Cradlepoint

Constants

VERSION

Attributes

account[RW]
base_url[RW]
password[RW]
username[RW]

Public Class Methods

authenticate(username, password) click to toggle source
# File lib/cradlepoint.rb, line 47
def self.authenticate(username, password)
  self.username = username
  self.password = password
  self.account  = Cradlepoint::Account.new unless self.account
  true
end
handle_response(response) click to toggle source
# 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 = {}) click to toggle source
# 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_prepend() click to toggle source
# File lib/cradlepoint.rb, line 54
def self.url_prepend
  "https://#{ @username }:#{ @password }@"
end