cradlepoint 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a39b1ded7fedf95f572b701bc48aa6a8610b49e8
4
- data.tar.gz: c74d80eb9a4246dde59bc3ff97a2e0ef7bf1a3a4
3
+ metadata.gz: 829fb48e12131b6d4ea897f9344275051062999a
4
+ data.tar.gz: 623697fe461d2a039bd8539afb7819b676370cac
5
5
  SHA512:
6
- metadata.gz: ce512a39b670eb5b1eb47ee41f75612882c2577a6460d2168a17df92ef02a6dbc107fa73b1b2e47ee1866c75af42ddfba6fd707e79f258f81d05059909d28f9b
7
- data.tar.gz: 426f4d58f1d264c61916fa353d987e487ed4db19b84315c547286aff65602504c933863e0f599ea67ffaf4219350be637b4af9f8c8a04053f14b521025bc657d
6
+ metadata.gz: 05fae43cf310e2960965dcbabe49c1e04ed1d9dfc3baaaaa758e83a2cfc79bf5d399bd406b81de625c87952c10bb37b378f22ea2b389850aaa3474427335b354
7
+ data.tar.gz: d410545ed0e3f38ea6774ad14f886c00abe0c9b1d6f6d80a8a624f20b1a1394198359b2fd2665d50256abd561597b6bf6b2feb8dc0aed4c92ba8f5115ed675cf
data/lib/cradlepoint.rb CHANGED
@@ -3,6 +3,8 @@ require 'rest-client'
3
3
 
4
4
  require 'cradlepoint/version'
5
5
 
6
+ require 'cradlepoint/hash_helpers'
7
+
6
8
  require 'cradlepoint/cradlepoint_object'
7
9
  require 'cradlepoint/account'
8
10
  require 'cradlepoint/net_device'
@@ -21,24 +23,24 @@ module Cradlepoint
21
23
  def self.make_request(method, url = '', params = {})
22
24
  raise 'You need to call Cradlepoint.authenticate(username, password) first.' unless username and password
23
25
 
24
- parameters = { format: :json }
26
+ params.merge!(format: :json)
25
27
  headers = { accept: :json, content_type: :json }
26
28
 
27
29
  response = case method
28
- when :get then RestClient.get()
29
- when :configs then get_configs
30
- else false
30
+ when :get then RestClient.get(url, params: params, headers: headers)
31
+ else return false
31
32
  end
32
33
 
33
- response ? handle_response(response) : false
34
+ handle_response(response)
34
35
  rescue RestClient::Exception => e
35
- return case e.code
36
- when 400 then { data: :unavailable }
37
- when 401 then { data: :unavailable }
38
- when 403 then { data: :unavailable }
39
- when 404 then { data: :unavailable }
40
- when 500 then { data: :unavailable }
41
- else raise(e)
36
+ puts "RestClient::Exception received: #{ e.response.code }"
37
+ return case e.response.code
38
+ when 400 then { success: false, error_code: 400, error: e }
39
+ when 401 then { success: false, error_code: 401, error: e }
40
+ when 403 then { success: false, error_code: 403, error: e }
41
+ when 404 then { success: false, error_code: 404, error: e }
42
+ when 500 then { success: false, error_code: 500, error: e }
43
+ else raise(e) # Not an error we are expecting.
42
44
  end
43
45
  end
44
46
 
@@ -52,10 +54,6 @@ module Cradlepoint
52
54
  "https://#{ @username }:#{ @password }@"
53
55
  end
54
56
 
55
- def self.url_append
56
- "?format=json"
57
- end
58
-
59
57
  def self.handle_response(response)
60
58
  begin
61
59
  parsed_response = JSON.parse(response)
@@ -63,11 +61,8 @@ module Cradlepoint
63
61
  raise "Cradlepoint received an invalid json response."
64
62
  end
65
63
 
66
- case response.code
67
- when 200, 302 then parsed_response
68
- when 400, 401 then false
69
- when 500 then false
70
- else false
71
- end
64
+ parsed_response['success'] ?
65
+ symbolize_keys(parsed_response['data']) :
66
+ raise("Unsuccessful response received.") # TODO: Handle more elegantly.
72
67
  end
73
68
  end
@@ -1,7 +1,7 @@
1
1
  module Cradlepoint
2
2
  class Account < CradlepointObject
3
3
 
4
- attr_accessor :ecm_id, :data
4
+ attr_accessor :ecm_id, :data, :disabled, :expiration, :name
5
5
 
6
6
  def initialize(id = nil)
7
7
  self.ecm_id = id
@@ -28,10 +28,8 @@ module Cradlepoint
28
28
  end
29
29
 
30
30
  def lazy_load_id
31
- self.data = Cradlepoint.handle_response RestClient.get(build_url(rel_url),
32
- content_type: :json,
33
- accept: :json)
34
- self.ecm_id = self.data['data'][0]['id']
31
+ self.data = Cradlepoint.make_request(:get, build_url(rel_url))
32
+ self.ecm_id = self.data[0][:id]
35
33
  self.ecm_id
36
34
  end
37
35
  end
@@ -46,7 +46,7 @@ module Cradlepoint
46
46
  router.get_configuration_editor_data.to_json,
47
47
  content_type: :json,
48
48
  accept: :json)
49
- self.id = self.data['data']['id']
49
+ self.id = self.data[:id]
50
50
  self.data
51
51
  end
52
52
 
@@ -2,7 +2,7 @@ module Cradlepoint
2
2
  class CradlepointObject
3
3
 
4
4
  def self.build_url(rel_url = '/')
5
- "#{ Cradlepoint.url_prepend }#{ Cradlepoint.base_url }#{ rel_url }#{ Cradlepoint.url_append }"
5
+ "#{ Cradlepoint.url_prepend }#{ Cradlepoint.base_url }#{ rel_url }"
6
6
  end
7
7
 
8
8
  def build_url(rel_url = '/')
@@ -0,0 +1,24 @@
1
+ module Cradlepoint
2
+ module HashHelpers
3
+ # From
4
+ # http://devblog.avdi.org/2009/07/14/recursively-symbolize-keys/
5
+ def symbolize_keys(hash)
6
+ if hash.is_a?(Array)
7
+ hash.map { |h| symbolize_keys(h) }
8
+ else
9
+ hash.inject({}) { |result, (key, value)|
10
+ new_key = case key
11
+ when String then key.to_sym
12
+ else key
13
+ end
14
+ new_value = case value
15
+ when Hash then symbolize_keys(value)
16
+ else value
17
+ end
18
+ result[new_key] = new_value
19
+ result
20
+ }
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,7 +1,10 @@
1
1
  module Cradlepoint
2
2
  class NetDevice < CradlepointObject
3
+ include Cradlepoint::HashHelpers
3
4
 
4
- attr_accessor :id, :router, :data
5
+ attr_accessor :id, :router, :data, :bytes_in, :bytes_out, :carrier, :esn, :imei, :info,
6
+ :ip_address, :mac, :mode, :name, :type, :uptime, :netmask, :dns0, :dns1,
7
+ :connection_state, :ip_mode
5
8
 
6
9
  def initialize(id = nil, router = nil)
7
10
  self.id = id
@@ -34,9 +37,50 @@ module Cradlepoint
34
37
 
35
38
  def get_all_from_router
36
39
  raise 'You must provide an ECM router' if router.nil?
37
- self.data = Cradlepoint.handle_response RestClient.get(build_url(rel_url_from_router),
38
- content_type: :json,
39
- accept: :json)
40
+ self.data = Cradlepoint.make_request(:get, build_url(rel_url_from_router))
41
+ assign_attributes_from_data(group: true)
42
+ end
43
+
44
+ def assign_attributes_from_data(options = {})
45
+ return unless self.data and self.data.any?
46
+ raw_data = self.data
47
+
48
+ if options[:group]
49
+ return unless raw_data.is_a?(Array)
50
+
51
+ net_devices = []
52
+ raw_data.each do |nd|
53
+ new_net_device = NetDevice.new(nd[:id], self.id)
54
+ new_net_device.assign_attributes_from_blob(nd)
55
+ net_devices << new_net_device
56
+ end
57
+
58
+ net_devices
59
+ else
60
+ assign_attributes_from_blob(raw_data)
61
+ end
62
+ end
63
+
64
+ def assign_attributes_from_blob(blob = {})
65
+ return unless blob and blob.any?
66
+
67
+ self.connection_state = blob[:connection_state]
68
+ self.bytes_in = blob[:bytes_in]
69
+ self.bytes_out = blob[:bytes_out]
70
+ self.carrier = blob[:carrier]
71
+ self.esn = blob[:esn]
72
+ self.imei = blob[:imei]
73
+ self.info = blob[:info]
74
+ self.ip_address = blob[:ip_address]
75
+ self.mac = blob[:mac]
76
+ self.mode = blob[:mode]
77
+ self.name = blob[:name]
78
+ self.type = blob[:type]
79
+ self.uptime = blob[:uptime]
80
+ self.netmask = blob[:netmask]
81
+ self.dns0 = blob[:dns0]
82
+ self.dns1 = blob[:dns1]
83
+ self.ip_mode = blob[:config][:ipmode] if blob[:config].is_a?(Hash)
40
84
  end
41
85
  end
42
- end
86
+ end
@@ -18,16 +18,16 @@ module Cradlepoint
18
18
 
19
19
  def get
20
20
  raise 'You must provide a mac' unless self.mac
21
- self.data = Cradlepoint.handle_response RestClient.get(build_new_url(rel_url), params)
21
+ self.data = Cradlepoint.make_request(:get, build_new_url(rel_url), params)
22
22
  end
23
23
 
24
24
  def get_status
25
25
  raise 'You must provide a mac' unless self.mac
26
- self.status_data = Cradlepoint.handle_response RestClient.get(build_new_url("#{ rel_url }/status"), params)
26
+ self.status_data = Cradlepoint.make_request(:get, build_new_url("#{ rel_url }/status"), params)
27
27
  end
28
28
 
29
29
  def params
30
- { params: { format: :json, mac: self.mac, limit: 1 }, accept: :json, content_type: :json }
30
+ { params: { format: :json, mac: self.mac, limit: 1 } }
31
31
  end
32
32
  end
33
33
  end
@@ -1,11 +1,15 @@
1
1
  module Cradlepoint
2
2
  class Router < CradlepointObject
3
+ include Cradlepoint::HashHelpers
3
4
 
4
5
  attr_accessor :id, :data, :ecm_firmware_uri, :ecm_configuration_uri,
5
- :ecm_configuration_manager_uri, :ecm_configuration_manager_data
6
+ :ecm_configuration_manager_uri, :ecm_configuration_manager_data,
7
+ :mac, :config_status, :description, :full_product_name, :ip_address,
8
+ :name, :stream_usage_in, :stream_usage_out, :stream_usage_period
6
9
 
7
- def initialize(id = nil)
10
+ def initialize(id = nil, options = {})
8
11
  self.id = id
12
+ options.each { |k, v| send("#{ k }=", v) if v }
9
13
  end
10
14
 
11
15
  def self.rel_url
@@ -33,12 +37,14 @@ module Cradlepoint
33
37
  end
34
38
 
35
39
  def self.index
36
- Cradlepoint.handle_response RestClient.get(build_url(rel_url))
40
+ build_array_of_routers_from_response(Cradlepoint.make_request(:get, build_url(rel_url)))
37
41
  end
38
42
 
39
43
  def get
40
44
  check_for_id_or_raise_error
41
- self.data = Cradlepoint.handle_response RestClient.get(build_url(rel_url_with_id))
45
+ self.data = Cradlepoint.make_request(:get, build_url(rel_url_with_id))
46
+ assign_attributes_from_data
47
+ self.data
42
48
  end
43
49
 
44
50
  def apply_new_config(config_settings = {})
@@ -56,7 +62,7 @@ module Cradlepoint
56
62
 
57
63
  def firmware_data
58
64
  check_for_id_or_raise_error
59
- Cradlepoint.handle_response RestClient.get(build_url(firmware_uri.split('/api/v1').last)) if firmware_uri
65
+ Cradlepoint.make_request(:get, build_url(firmware_uri.split('/api/v1').last))
60
66
  end
61
67
 
62
68
  def firmware_uri
@@ -72,14 +78,12 @@ module Cradlepoint
72
78
 
73
79
  def lazy_load_router_data
74
80
  get # Grab the data from the api.
75
- self.ecm_firmware_uri = self.data['data']['actual_firmware']
76
- self.ecm_configuration_manager_uri = self.data['data']['configuration_manager']
81
+ self.ecm_firmware_uri = self.data[:actual_firmware]
82
+ self.ecm_configuration_manager_uri = self.data[:configuration_manager]
77
83
  end
78
84
 
79
85
  def lazy_load_configuration_manager_data
80
- self.ecm_configuration_manager_data = Cradlepoint.handle_response RestClient.get(build_url(rel_url_for_configuration_managers),
81
- content_type: :json,
82
- accept: :json)
86
+ self.ecm_configuration_manager_data = Cradlepoint.make_request(:get, build_url(rel_url_for_configuration_managers))
83
87
  end
84
88
 
85
89
  def get_configuration_editor_data
@@ -91,8 +95,34 @@ module Cradlepoint
91
95
  }
92
96
  end
93
97
 
98
+ def assign_attributes_from_data
99
+ return unless self.data and self.data.any?
100
+
101
+ self.mac = self.data[:mac]
102
+ self.name = self.data[:name]
103
+ self.ip_address = self.data[:ip_address]
104
+ self.config_status = self.data[:config_status]
105
+ self.description = self.data[:description]
106
+ self.full_product_name = self.data[:full_product_name]
107
+ self.stream_usage_in = self.data[:stream_usage_in]
108
+ self.stream_usage_out = self.data[:stream_usage_out]
109
+ self.stream_usage_period = self.data[:stream_usage_period]
110
+ end
111
+
94
112
  private
95
113
 
114
+ def self.build_array_of_routers_from_response(response)
115
+ return unless response and response.any?
116
+ response.map { |r| create_and_assign_attributes_from_data(r) }
117
+ end
118
+
119
+ def self.create_and_assign_attributes_from_data(data)
120
+ return unless data and data.any? and data[:id]
121
+ router = Cradlepoint::Router.new(data[:id], data: data)
122
+ router.assign_attributes_from_data
123
+ router
124
+ end
125
+
96
126
  def check_for_id_or_raise_error
97
127
  raise 'You must provide an ECM router id' if id.nil?
98
128
  end
@@ -1,3 +1,3 @@
1
1
  module Cradlepoint
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/spec/account_spec.rb CHANGED
@@ -7,7 +7,7 @@ describe Cradlepoint::Account do
7
7
  let(:account) { Cradlepoint::Account.new }
8
8
 
9
9
  before do
10
- authenticate_with_valid_credentials
10
+ login
11
11
  account.id
12
12
  end
13
13
 
@@ -5,7 +5,7 @@ describe Cradlepoint::CradlepointObject do
5
5
  let(:factory_cradlepoint_object) { Cradlepoint::CradlepointObject }
6
6
  let(:cradlepoint_object) { Cradlepoint::CradlepointObject.new }
7
7
 
8
- let(:url) { 'https://:@cradlepointecm.com/api/v1/blah?format=json' }
8
+ let(:url) { 'https://:@cradlepointecm.com/api/v1/blah' }
9
9
 
10
10
  context 'factory' do
11
11
 
@@ -27,7 +27,7 @@ describe Cradlepoint do
27
27
 
28
28
  context 'make_request' do
29
29
 
30
- before { authenticate_with_valid_credentials }
30
+ before { login }
31
31
 
32
32
  it 'should return the proper credentials' do
33
33
  cradlepoint.username.should == USERNAME
@@ -16,33 +16,49 @@ describe Cradlepoint::NetDevice do
16
16
 
17
17
  context 'when authenticated' do
18
18
 
19
- before { authenticate_with_valid_credentials }
19
+ before { login }
20
20
 
21
21
  describe '.get_all_from_router' do
22
22
 
23
- let(:router) { Cradlepoint::Router.new(ROUTER_ID) }
24
- let(:device) { net_device.new(nil, router) }
23
+ let(:router) { Cradlepoint::Router.new(ROUTER_ID) }
24
+ let(:device) { net_device.new(nil, router) }
25
+ let(:devices) { device.get_all_from_router }
25
26
 
26
- before { device.get_all_from_router }
27
-
28
- it 'should have been successful' do
29
- device.data['success'].should be_true
30
- end
27
+ before { devices }
31
28
 
32
29
  it 'should raise an error when there is no router' do
33
30
  device_with_no_router = net_device.new
34
31
  -> { device_with_no_router.get_all_from_router }.should raise_error
35
32
  end
36
33
 
37
- # TODO: Overhaul these to allow them to endure the test of time.
38
- # These are brittle, temporary tests to make sure the correct
39
- # blob is being returned.
40
34
  it 'should return a blob' do
41
- device.data['data'].any?.should be_true
35
+ devices.any?.should be_true
42
36
  end
43
37
 
44
38
  it 'should be an array' do
45
- device.data['data'].is_a?(Array).should be_true
39
+ device.data.is_a?(Array).should be_true
40
+ end
41
+
42
+ describe 'devices' do
43
+
44
+ let(:first_net_device) { devices.first }
45
+ let(:first_raw_data) { device.data.first }
46
+ let(:attrs) { [:bytes_in, :bytes_out, :carrier, :esn, :imei, :info,
47
+ :ip_address, :mac, :mode, :name, :type, :uptime] }
48
+
49
+ it 'should return an array' do
50
+ devices.is_a?(Array).should be_true
51
+ end
52
+
53
+ it 'should return an array of Cradlepoint::NetDevices' do
54
+ devices.each { |d| d.is_a?(Cradlepoint::NetDevice).should be_true }
55
+ end
56
+
57
+ it 'should have the correct attributes' do
58
+ attrs.each do |a|
59
+ first_net_device.send(a).should == first_raw_data[a]
60
+ end
61
+ end
46
62
  end
47
63
  end
48
64
  end
@@ -11,7 +11,7 @@ describe Cradlepoint::NetFlow do
11
11
  let(:net_flow_data) { net_flow.get }
12
12
 
13
13
  it 'should return successfully' do
14
- net_flow_data['success'].should be_true
14
+ net_flow_data.any?.should be_true
15
15
  end
16
16
  end
17
17
 
@@ -20,7 +20,7 @@ describe Cradlepoint::NetFlow do
20
20
  let(:net_flow_status_data) { net_flow.get_status }
21
21
 
22
22
  it 'should return data successfully' do
23
- net_flow_status_data['success'].should be_true
23
+ net_flow_status_data.any?.should be_true
24
24
  end
25
25
  end
26
26
  end
data/spec/router_spec.rb CHANGED
@@ -17,48 +17,40 @@ describe Cradlepoint::Router do
17
17
 
18
18
  context 'when authenticated' do
19
19
 
20
- before { authenticate_with_valid_credentials }
20
+ before { login }
21
21
 
22
22
  describe '.get' do
23
23
 
24
- let(:response) { router.new(ROUTER_ID).get }
25
- let(:response_hash) { response['data'] }
24
+ describe 'attrs' do
26
25
 
27
- subject { response }
28
- it { should be }
26
+ let(:ecm_router) { router.new(ROUTER_ID) }
27
+ let(:attrs) { [:mac, :config_status, :description, :full_product_name, :ip_address, :name, :stream_usage_in,
28
+ :stream_usage_out, :stream_usage_period] }
29
29
 
30
- it 'should be successful' do
31
- response['success'].should be_true
32
- end
33
-
34
- it 'should raise an error when the id is not provided' do
35
- -> { router.get }.should raise_error
36
- end
30
+ before { ecm_router.get }
37
31
 
38
- it 'should return the correct blob' do
39
- response['data'].any?.should be_true
40
- end
41
-
42
- it 'should have the correct keys' do
43
- ['full_product_name', 'config_status', 'mac'].all? { |k| response_hash.has_key?(k) }.should be_true
32
+ it 'should be assigned correctly' do
33
+ attrs.each do |a|
34
+ ecm_router.send(a).should == ecm_router.data[a]
35
+ end
36
+ end
44
37
  end
45
38
  end
46
39
 
47
40
  describe '.index' do
48
41
 
49
- let(:response) { router.index }
50
- let(:response_hash) { response['data'].first }
42
+ let(:routers) { router.index }
51
43
 
52
- subject { response }
53
- it { should be }
44
+ it 'should return routers' do
45
+ routers.any?.should be_true
46
+ end
54
47
 
55
- it 'should be successful' do
56
- response['success'].should be_true
48
+ it 'should return the correct collection' do
49
+ routers.is_a?(Array).should be_true
57
50
  end
58
51
 
59
- it 'should return the correct blob' do
60
- response['data'].any?.should be_true
61
- response['data'].is_a?(Array).should be_true
52
+ it 'should be of the correct type' do
53
+ routers.each{ |r| r.is_a?(Cradlepoint::Router).should be_true }
62
54
  end
63
55
  end
64
56
 
data/spec/spec_helper.rb CHANGED
@@ -31,3 +31,5 @@ def logout
31
31
  Cradlepoint.username = nil
32
32
  Cradlepoint.password = nil
33
33
  end
34
+
35
+ include Cradlepoint::HashHelpers
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cradlepoint
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - uceem
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-13 00:00:00.000000000 Z
11
+ date: 2013-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -97,6 +97,7 @@ files:
97
97
  - lib/cradlepoint/account.rb
98
98
  - lib/cradlepoint/config.rb
99
99
  - lib/cradlepoint/cradlepoint_object.rb
100
+ - lib/cradlepoint/hash_helpers.rb
100
101
  - lib/cradlepoint/net_device.rb
101
102
  - lib/cradlepoint/net_flow.rb
102
103
  - lib/cradlepoint/router.rb
@@ -131,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
132
  version: '0'
132
133
  requirements: []
133
134
  rubyforge_project:
134
- rubygems_version: 2.0.6
135
+ rubygems_version: 2.0.5
135
136
  signing_key:
136
137
  specification_version: 4
137
138
  summary: Cradlepoint ECM API gem