machineshop 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,3 @@
1
1
  module MachineShop
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/machineshop.rb CHANGED
@@ -50,185 +50,186 @@ module MachineShop
50
50
  class << self
51
51
  # @@api_base_url = 'http://api.machineshop.io/api/v0'
52
52
  @@api_base_url = 'http://stage.services.machineshop.io/api/v0'
53
+ # @@api_base_url = 'https://services.machineshop.io/api/v0'
53
54
 
54
- #configs starts
55
- attr_writer :configuration
55
+ #configs starts
56
+ attr_writer :configuration
56
57
 
57
- def configuration
58
- @configuration ||= Configuration.new
59
- end
58
+ def configuration
59
+ @configuration ||= Configuration.new
60
+ end
60
61
 
61
- def configure
62
- yield(configuration)
63
- end
62
+ def configure
63
+ yield(configuration)
64
+ end
64
65
 
65
- #reset the config object
66
- def reset
67
- Configuration.new
68
- end
66
+ #reset the config object
67
+ def reset
68
+ Configuration.new
69
+ end
69
70
 
70
- #configs ends
71
+ #configs ends
71
72
 
72
- def api_base_url=(api_base_url)
73
- @@api_base_url = api_base_url
74
- end
73
+ def api_base_url=(api_base_url)
74
+ @@api_base_url = api_base_url
75
+ end
75
76
 
76
- def api_base_url
77
- @@api_base_url
78
- end
77
+ def api_base_url
78
+ @@api_base_url
79
+ end
79
80
 
80
- def get(url, auth_token, body_hash=nil)
81
- platform_request(url, auth_token, body_hash)
82
- end
81
+ def get(url, auth_token, body_hash=nil)
82
+ platform_request(url, auth_token, body_hash)
83
+ end
83
84
 
84
- def post(url, auth_token, body_hash)
85
- platform_request(url, auth_token, body_hash, :post)
86
- end
85
+ def post(url, auth_token, body_hash)
86
+ platform_request(url, auth_token, body_hash, :post)
87
+ end
87
88
 
88
- def delete(url, auth_token, body_hash)
89
- platform_request(url, auth_token, body_hash, :delete)
90
- end
89
+ def delete(url, auth_token, body_hash)
90
+ platform_request(url, auth_token, body_hash, :delete)
91
+ end
91
92
 
92
- def put(url, auth_token, body_hash)
93
- platform_request(url, auth_token, body_hash, :put)
94
- end
93
+ def put(url, auth_token, body_hash)
94
+ platform_request(url, auth_token, body_hash, :put)
95
+ end
95
96
 
96
- def headers(auth_token)
97
- header ={:content_type => :json,
98
- :accept => :json}
99
- header.merge!({ authorization: "Basic " + Base64.encode64(auth_token + ':X') }) if auth_token
100
- header
101
- end
97
+ def headers(auth_token)
98
+ header ={:content_type => :json,
99
+ :accept => :json}
100
+ header.merge!({ authorization: "Basic " + Base64.encode64(auth_token + ':X') }) if auth_token
101
+ header
102
+ end
102
103
 
103
- def platform_request(url, auth_token, body_hash=nil, http_verb=:get )
104
- puts "body_hash: #{body_hash}"
105
- opts = nil
106
- api_uri = api_base_url + url
107
- headers = self.headers(auth_token)
108
- if http_verb == :get
109
- if (body_hash && !body_hash.empty?)
110
- uri = Addressable::URI.new
111
- uri.query_values = body_hash
112
- api_uri += "?" + uri.query
113
- end
104
+ def platform_request(url, auth_token, body_hash=nil, http_verb=:get )
105
+ puts "body_hash: #{body_hash}"
106
+ opts = nil
107
+ api_uri = api_base_url + url
108
+ headers = self.headers(auth_token)
109
+ if http_verb == :get
110
+ if (body_hash && !body_hash.empty?)
111
+ uri = Addressable::URI.new
112
+ uri.query_values = body_hash
113
+ api_uri += "?" + uri.query
114
+ end
115
+
116
+ opts = {
117
+ :method => :get,
118
+ :url => api_uri,
119
+ :headers => headers,
120
+ :open_timeout => 30,
121
+ :timeout => 80
122
+ }
114
123
 
115
- opts = {
116
- :method => :get,
117
- :url => api_uri,
118
- :headers => headers,
119
- :open_timeout => 30,
120
- :timeout => 80
121
- }
122
-
123
- else
124
- opts = {
125
- :method => http_verb,
126
- :url => api_uri,
127
- :headers => headers,
128
- :open_timeout => 30,
129
- :payload => MachineShop::JSON.dump(body_hash),
130
- :timeout => 80
131
- }
132
-
133
- end
134
-
135
- puts "request params: #{opts} "
136
-
137
- begin
138
- response = execute_request(opts)
139
- rescue SocketError => e
140
- self.handle_restclient_error(e)
141
- rescue NoMethodError => e
142
- # Work around RestClient bug
143
- if e.message =~ /\WRequestFailed\W/
144
- e = APIConnectionError.new('Unexpected HTTP response code')
145
- self.handle_restclient_error(e)
146
124
  else
147
- raise
125
+ opts = {
126
+ :method => http_verb,
127
+ :url => api_uri,
128
+ :headers => headers,
129
+ :open_timeout => 30,
130
+ :payload => MachineShop::JSON.dump(body_hash),
131
+ :timeout => 80
132
+ }
133
+
148
134
  end
149
- rescue RestClient::ExceptionWithResponse => e
150
- if rcode = e.http_code and rbody = e.http_body
151
- self.handle_api_error(rcode, rbody)
152
- else
135
+
136
+ puts "request params: #{opts} "
137
+
138
+ begin
139
+ response = execute_request(opts)
140
+ rescue SocketError => e
141
+ self.handle_restclient_error(e)
142
+ rescue NoMethodError => e
143
+ # Work around RestClient bug
144
+ if e.message =~ /\WRequestFailed\W/
145
+ e = APIConnectionError.new('Unexpected HTTP response code')
146
+ self.handle_restclient_error(e)
147
+ else
148
+ raise
149
+ end
150
+ rescue RestClient::ExceptionWithResponse => e
151
+ if rcode = e.http_code and rbody = e.http_body
152
+ self.handle_api_error(rcode, rbody)
153
+ else
154
+ self.handle_restclient_error(e)
155
+ end
156
+ rescue RestClient::Exception, Errno::ECONNREFUSED => e
153
157
  self.handle_restclient_error(e)
154
158
  end
155
- rescue RestClient::Exception, Errno::ECONNREFUSED => e
156
- self.handle_restclient_error(e)
157
- end
158
159
 
159
- rbody = response.body
160
- rcode = response.code
160
+ rbody = response.body
161
+ rcode = response.code
162
+
163
+ begin
164
+ # Would use :symbolize_names => true, but apparently there is
165
+ # some library out there that makes symbolize_names not work.
166
+ resp = MachineShop::JSON.load(rbody)
167
+ resp ||= {}
168
+ resp = Util.symbolize_names(resp)
161
169
 
162
- begin
163
- # Would use :symbolize_names => true, but apparently there is
164
- # some library out there that makes symbolize_names not work.
165
- resp = MachineShop::JSON.load(rbody)
166
- resp ||= {}
167
- resp = Util.symbolize_names(resp)
170
+ resp.merge!({:http_code => rcode}) if resp.is_a?(Hash)
171
+ return resp
172
+ rescue MultiJson::DecodeError
173
+ raise APIError.new("Invalid response object from API: #{rbody.inspect} (HTTP response code was #{rcode})", rcode, rbody)
174
+ end
168
175
 
169
- resp.merge!({:http_code => rcode}) if resp.is_a?(Hash)
170
- return resp
171
- rescue MultiJson::DecodeError
172
- raise APIError.new("Invalid response object from API: #{rbody.inspect} (HTTP response code was #{rcode})", rcode, rbody)
173
- end
176
+ end
174
177
 
175
- end
176
178
 
179
+ def execute_request(opts)
180
+ RestClient::Request.execute(opts)
181
+ end
177
182
 
178
- def execute_request(opts)
179
- RestClient::Request.execute(opts)
180
- end
183
+ def handle_api_error(rcode, rbody)
184
+ begin
185
+ error_obj = MachineShop::JSON.load(rbody)
186
+ error_obj = Util.symbolize_names(error_obj)
187
+ error = error_obj[:error] or raise MachineShopError.new # escape from parsing
188
+ rescue MultiJson::DecodeError, MachineShopError
189
+ raise APIError.new("Invalid response object from API: #{rbody.inspect} (HTTP response code was #{rcode})", rcode, rbody)
190
+ end
181
191
 
182
- def handle_api_error(rcode, rbody)
183
- begin
184
- error_obj = MachineShop::JSON.load(rbody)
185
- error_obj = Util.symbolize_names(error_obj)
186
- error = error_obj[:error] or raise MachineShopError.new # escape from parsing
187
- rescue MultiJson::DecodeError, MachineShopError
188
- raise APIError.new("Invalid response object from API: #{rbody.inspect} (HTTP response code was #{rcode})", rcode, rbody)
192
+ case rcode
193
+ when 400, 404 then
194
+ raise invalid_request_error(error, rcode, rbody, error_obj)
195
+ when 401
196
+ raise authentication_error(error, rcode, rbody, error_obj)
197
+ when 402
198
+ # TODO Come up with errors
199
+ else
200
+ raise api_error(error, rcode, rbody, error_obj)
201
+ end
189
202
  end
190
203
 
191
- case rcode
192
- when 400, 404 then
193
- raise invalid_request_error(error, rcode, rbody, error_obj)
194
- when 401
195
- raise authentication_error(error, rcode, rbody, error_obj)
196
- when 402
197
- # TODO Come up with errors
198
- else
199
- raise api_error(error, rcode, rbody, error_obj)
204
+ def invalid_request_error(error, rcode, rbody, error_obj)
205
+ InvalidRequestError.new(error, error, rcode, rbody, error_obj)
200
206
  end
201
- end
202
207
 
203
- def invalid_request_error(error, rcode, rbody, error_obj)
204
- InvalidRequestError.new(error, error, rcode, rbody, error_obj)
205
- end
208
+ def authentication_error(error, rcode, rbody, error_obj)
209
+ AuthenticationError.new(error, rcode, rbody, error_obj)
210
+ end
206
211
 
207
- def authentication_error(error, rcode, rbody, error_obj)
208
- AuthenticationError.new(error, rcode, rbody, error_obj)
209
- end
212
+ def api_error(error, rcode, rbody, error_obj)
213
+ APIError.new(error, rcode, rbody, error_obj)
214
+ end
210
215
 
211
- def api_error(error, rcode, rbody, error_obj)
212
- APIError.new(error, rcode, rbody, error_obj)
213
- end
216
+ def handle_restclient_error(e)
217
+ case e
218
+ when RestClient::ServerBrokeConnection, RestClient::RequestTimeout
219
+ message = "Could not connect to MachineShop (#{@@api_base_url}). Please check your internet connection and try again. If this problem persists, you should check MachineShop's service status."
220
+ when RestClient::SSLCertificateNotVerified
221
+ message = "Could not verify MachineShops's SSL certificate. Please make sure that your network is not intercepting certificates."
222
+ when SocketError
223
+ message = "Unexpected error communicating when trying to connect to MachineShop (#{@@api_base_url}). HINT: You may be seeing this message because your DNS is not working."
224
+ else
225
+ message = "Unexpected error communicating with MachineShop"
226
+ end
227
+ message += "\n\n(Network error: #{e.message})"
228
+ # puts "error message string : #{message}"
229
+ raise APIConnectionError.new(message)
230
+ end
214
231
 
215
- def handle_restclient_error(e)
216
- case e
217
- when RestClient::ServerBrokeConnection, RestClient::RequestTimeout
218
- message = "Could not connect to MachineShop (#{@@api_base_url}). Please check your internet connection and try again. If this problem persists, you should check MachineShop's service status."
219
- when RestClient::SSLCertificateNotVerified
220
- message = "Could not verify MachineShops's SSL certificate. Please make sure that your network is not intercepting certificates."
221
- when SocketError
222
- message = "Unexpected error communicating when trying to connect to MachineShop (#{@@api_base_url}). HINT: You may be seeing this message because your DNS is not working."
223
- else
224
- message = "Unexpected error communicating with MachineShop"
225
- end
226
- message += "\n\n(Network error: #{e.message})"
227
- # puts "error message string : #{message}"
228
- raise APIConnectionError.new(message)
229
- end
230
232
 
231
-
232
-
233
- end
233
+
234
+ end
234
235
  end
@@ -1,9 +1,5 @@
1
1
  require 'spec_helper'
2
2
 
3
- #MachineShop.api_base_url= 'http://machineshop.dev:3000/api/v0'
4
- MachineShop.api_base_url= 'http://stage.services.machineshop.io/api/v0'
5
-
6
- #publisher_username = 'publisher@machineshop.com'
7
3
  publisher_username = 'publisher@csr.com'
8
4
  publisher_password = 'password'
9
5
 
@@ -1,9 +1,6 @@
1
1
  require_relative '../spec_helper'
2
2
 
3
3
 
4
- #MachineShop.api_base_url= 'http://machineshop.dev:3000/api/v0'
5
- MachineShop.api_base_url= 'http://stage.services.machineshop.io/api/v0'
6
-
7
4
  #publisher_username = 'publisher@machineshop.com'
8
5
  publisher_username = 'admin@csr.com'
9
6
  publisher_password = 'password'
@@ -1,9 +1,5 @@
1
1
  require_relative '../spec_helper'
2
2
 
3
- #MachineShop.api_base_url= 'http://machineshop.dev:3000/api/v0'
4
- MachineShop.api_base_url= 'http://stage.services.machineshop.io/api/v0'
5
-
6
- #publisher_username = 'publisher@machineshop.com'
7
3
  publisher_username = 'admin@csr.com'
8
4
  publisher_password = 'password'
9
5
 
@@ -1,8 +1,5 @@
1
1
  require_relative '../spec_helper'
2
2
 
3
- #MachineShop.api_base_url= 'http://machineshop.dev:3000/api/v0'
4
- MachineShop.api_base_url= 'http://stage.services.machineshop.io/api/v0'
5
-
6
3
  #publisher_username = 'publisher@machineshop.com'
7
4
  publisher_username = 'admin@csr.com'
8
5
  publisher_password = 'password'
@@ -1,10 +1,7 @@
1
1
  require_relative '../spec_helper'
2
2
 
3
- #MachineShop.api_base_url= 'http://machineshop.dev:3000/api/v0'
4
- MachineShop.api_base_url= 'http://stage.services.machineshop.io/api/v0'
5
-
6
3
  #publisher_username = 'publisher@machineshop.com'
7
- publisher_username = 'publisher@csr.com'
4
+ publisher_username = 'admin@csr.com'
8
5
  publisher_password = 'password'
9
6
 
10
7
 
@@ -16,6 +13,7 @@ publisher_password = 'password'
16
13
  describe MachineShop::Mapping do
17
14
 
18
15
  it "should get a geocoded address" do
16
+ ap "geocoding address"
19
17
  element_data = MachineShop::Mapping.geocode(
20
18
  {
21
19
  :address => "1600 Amphitheatre Parkway, Mountain View, CA",
@@ -23,6 +21,7 @@ describe MachineShop::Mapping do
23
21
  },
24
22
  auth_token)
25
23
 
24
+ ap element_data.as_json
26
25
  #puts "GEO: #{element_data}"
27
26
 
28
27
  element_data.should_not be_nil
@@ -30,6 +29,7 @@ describe MachineShop::Mapping do
30
29
  end
31
30
 
32
31
  it "should get directions" do
32
+ ap "getting directions"
33
33
  element_data = MachineShop::Mapping.directions(
34
34
  {
35
35
  :origin => "Denver",
@@ -37,7 +37,7 @@ describe MachineShop::Mapping do
37
37
  :sensor => "false"
38
38
  },
39
39
  auth_token)
40
-
40
+ # ap element_data.as_json
41
41
  #puts "GEO: #{element_data}"
42
42
 
43
43
  element_data.should_not be_nil
@@ -45,6 +45,7 @@ describe MachineShop::Mapping do
45
45
  end
46
46
 
47
47
  it "should get distance" do
48
+ ap "getting distance "
48
49
  element_data = MachineShop::Mapping.distance(
49
50
  {
50
51
  :origins => "Vancouver BC",
@@ -54,7 +55,7 @@ describe MachineShop::Mapping do
54
55
  :sensor => "false"
55
56
  },
56
57
  auth_token)
57
-
58
+ ap element_data.as_json
58
59
  #puts "GEO: #{element_data}"
59
60
 
60
61
  element_data.should_not be_nil
@@ -1,10 +1,7 @@
1
1
  require_relative '../spec_helper'
2
2
 
3
- #MachineShop.api_base_url= 'http://machineshop.dev:3000/api/v0'
4
- MachineShop.api_base_url= 'http://stage.services.machineshop.io/api/v0'
5
-
6
3
  #publisher_username = 'publisher@machineshop.com'
7
- publisher_username = 'publisher@csr.com'
4
+ publisher_username = 'admin@csr.com'
8
5
  publisher_password = 'password'
9
6
 
10
7
 
@@ -19,7 +16,7 @@ describe MachineShop::Meter do
19
16
  it "should get all meter data" do
20
17
  element_data = MachineShop::Meter.all({}, auth_token)
21
18
 
22
- # puts "element_data from all: #{element_data}"
19
+ puts "element_data from all: #{element_data}"
23
20
 
24
21
  element_data.should_not be_nil
25
22
  element_data.should_not be_empty
@@ -28,17 +25,20 @@ describe MachineShop::Meter do
28
25
  it "should get meter by id " do
29
26
 
30
27
  meter_id = element_data[0].id
28
+ ap "retrieving meter for id #{meter_id}"
31
29
 
32
30
  # element_data = MachineShop::Meter.retrieve(meter_id, auth_token)
33
- element_data = MachineShop::Meter.retrieve("53847ac2b99e9c0a30000001", auth_token)
31
+ element_data = MachineShop::Meter.retrieve(meter_id, auth_token)
34
32
  puts "meter by id : #{element_data}"
35
33
  element_data.should_not be_nil
36
34
  end
37
35
 
38
36
  it "should get meters via a user" do
37
+
38
+ ap "meters by user "
39
39
  element_data = user.meters
40
40
 
41
- # puts "meters via user: #{element_data}"
41
+ puts "meters via user: #{element_data}"
42
42
  element_data.should_not be_nil
43
43
  element_data.should_not be_empty
44
44
  end
@@ -1,7 +1,5 @@
1
1
  require_relative '../spec_helper'
2
2
 
3
- #MachineShop.api_base_url= 'http://machineshop.dev:3000/api/v0'
4
- MachineShop.api_base_url= 'http://stage.services.machineshop.io/api/v0'
5
3
 
6
4
  #publisher_username = 'publisher@machineshop.com'
7
5
  publisher_username = 'publisher@csr.com'
@@ -19,7 +17,9 @@ describe MachineShop::Report do
19
17
  it "should get all report data" do
20
18
  element_data = MachineShop::Report.all({}, auth_token)
21
19
  reports=element_data
22
- # puts "element_data: #{element_data}"
20
+
21
+ puts "yaaaaaaaaaaaaaaaaaaaaa"
22
+ puts "element_data: #{element_data}"
23
23
 
24
24
  element_data.should_not be_nil
25
25
  element_data.should_not be_empty
@@ -1,9 +1,5 @@
1
1
  require_relative '../spec_helper'
2
2
 
3
- #MachineShop.api_base_url= 'http://machineshop.dev:3000/api/v0'
4
- MachineShop.api_base_url= 'http://stage.services.machineshop.io/api/v0'
5
-
6
- #publisher_username = 'publisher@machineshop.com'
7
3
  publisher_username = 'admin@csr.com'
8
4
  publisher_password = 'password'
9
5
 
@@ -1,10 +1,6 @@
1
1
  require_relative '../spec_helper'
2
2
 
3
3
 
4
- #MachineShop.api_base_url= 'http://machineshop.dev:3000/api/v0'
5
- MachineShop.api_base_url= 'http://stage.services.machineshop.io/api/v0'
6
-
7
- #publisher_username = 'publisher@machineshop.com'
8
4
  publisher_username = 'admin@csr.com'
9
5
  publisher_password = 'password'
10
6
 
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  require 'machineshop'
2
2
  require "awesome_print"
3
+ require 'active_support'
3
4
  require "active_support/core_ext"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: machineshop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - machineshop
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-09 00:00:00.000000000 Z
11
+ date: 2014-07-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -110,6 +110,7 @@ files:
110
110
  - LICENSE
111
111
  - README.md
112
112
  - Rakefile
113
+ - Readme_old.bkmMd
113
114
  - doc.txt
114
115
  - lib/machineshop.rb
115
116
  - lib/machineshop/api_operations/create.rb
@@ -133,7 +134,6 @@ files:
133
134
  - lib/machineshop/machineshop_object.rb
134
135
  - lib/machineshop/mapping.rb
135
136
  - lib/machineshop/meter.rb
136
- - lib/machineshop/models/people.rb
137
137
  - lib/machineshop/report.rb
138
138
  - lib/machineshop/rule.rb
139
139
  - lib/machineshop/user.rb
@@ -1,11 +0,0 @@
1
- module MachineShop
2
- # require 'active_record'
3
-
4
- # require 'machineshop/database'
5
-
6
- # Database.new
7
-
8
- class People
9
- # end
10
-
11
- end