lib/cradlepoint/router.rb in cradlepoint-0.1.0 vs lib/cradlepoint/router.rb in cradlepoint-0.1.1
- old
+ new
@@ -1,13 +1,17 @@
module Cradlepoint
class Router < CradlepointObject
+ include Cradlepoint::HashHelpers
attr_accessor :id, :data, :ecm_firmware_uri, :ecm_configuration_uri,
- :ecm_configuration_manager_uri, :ecm_configuration_manager_data
+ :ecm_configuration_manager_uri, :ecm_configuration_manager_data,
+ :mac, :config_status, :description, :full_product_name, :ip_address,
+ :name, :stream_usage_in, :stream_usage_out, :stream_usage_period
- def initialize(id = nil)
+ def initialize(id = nil, options = {})
self.id = id
+ options.each { |k, v| send("#{ k }=", v) if v }
end
def self.rel_url
'/routers'
end
@@ -31,16 +35,18 @@
def rel_url_for_configuration_managers
Cradlepoint::Router.rel_url_with_id(id)
end
def self.index
- Cradlepoint.handle_response RestClient.get(build_url(rel_url))
+ build_array_of_routers_from_response(Cradlepoint.make_request(:get, build_url(rel_url)))
end
def get
check_for_id_or_raise_error
- self.data = Cradlepoint.handle_response RestClient.get(build_url(rel_url_with_id))
+ self.data = Cradlepoint.make_request(:get, build_url(rel_url_with_id))
+ assign_attributes_from_data
+ self.data
end
def apply_new_config(config_settings = {})
config = Cradlepoint::Config.new(self, config_settings)
config.create_editor
@@ -54,11 +60,11 @@
self.ecm_configuration_manager_data
end
def firmware_data
check_for_id_or_raise_error
- Cradlepoint.handle_response RestClient.get(build_url(firmware_uri.split('/api/v1').last)) if firmware_uri
+ Cradlepoint.make_request(:get, build_url(firmware_uri.split('/api/v1').last))
end
def firmware_uri
lazy_load_router_data unless self.ecm_firmware_uri
self.ecm_firmware_uri
@@ -70,18 +76,16 @@
self.ecm_configuration_uri
end
def lazy_load_router_data
get # Grab the data from the api.
- self.ecm_firmware_uri = self.data['data']['actual_firmware']
- self.ecm_configuration_manager_uri = self.data['data']['configuration_manager']
+ self.ecm_firmware_uri = self.data[:actual_firmware]
+ self.ecm_configuration_manager_uri = self.data[:configuration_manager]
end
def lazy_load_configuration_manager_data
- self.ecm_configuration_manager_data = Cradlepoint.handle_response RestClient.get(build_url(rel_url_for_configuration_managers),
- content_type: :json,
- accept: :json)
+ self.ecm_configuration_manager_data = Cradlepoint.make_request(:get, build_url(rel_url_for_configuration_managers))
end
def get_configuration_editor_data
{
account: '/api/v1' + Cradlepoint.account.rel_url_with_id,
@@ -89,10 +93,36 @@
firmware: firmware_uri,
router: '/api/v1' + rel_url_with_id
}
end
+ def assign_attributes_from_data
+ return unless self.data and self.data.any?
+
+ self.mac = self.data[:mac]
+ self.name = self.data[:name]
+ self.ip_address = self.data[:ip_address]
+ self.config_status = self.data[:config_status]
+ self.description = self.data[:description]
+ self.full_product_name = self.data[:full_product_name]
+ self.stream_usage_in = self.data[:stream_usage_in]
+ self.stream_usage_out = self.data[:stream_usage_out]
+ self.stream_usage_period = self.data[:stream_usage_period]
+ end
+
private
+
+ def self.build_array_of_routers_from_response(response)
+ return unless response and response.any?
+ response.map { |r| create_and_assign_attributes_from_data(r) }
+ end
+
+ def self.create_and_assign_attributes_from_data(data)
+ return unless data and data.any? and data[:id]
+ router = Cradlepoint::Router.new(data[:id], data: data)
+ router.assign_attributes_from_data
+ router
+ end
def check_for_id_or_raise_error
raise 'You must provide an ECM router id' if id.nil?
end
end