Module: MachineShop
- Defined in:
- lib/machineshop/api_operations/create.rb,
lib/machineshop.rb,
lib/machineshop/json.rb,
lib/machineshop/rule.rb,
lib/machineshop/user.rb,
lib/machineshop/util.rb,
lib/machineshop/meter.rb,
lib/machineshop/device.rb,
lib/machineshop/report.rb,
lib/machineshop/mapping.rb,
lib/machineshop/utility.rb,
lib/machineshop/version.rb,
lib/machineshop/customer.rb,
lib/machineshop/database.rb,
lib/machineshop/api_resource.rb,
lib/machineshop/configuration.rb,
lib/machineshop/device_instance.rb,
lib/machineshop/errors/api_error.rb,
lib/machineshop/machineshop_cache.rb,
lib/machineshop/machineshop_object.rb,
lib/machineshop/api_operations/list.rb,
lib/machineshop/api_operations/delete.rb,
lib/machineshop/api_operations/update.rb,
lib/machineshop/errors/database_error.rb,
lib/machineshop/errors/machineshop_error.rb,
lib/machineshop/errors/api_connection_error.rb,
lib/machineshop/errors/authentication_error.rb,
lib/machineshop/errors/invalid_request_error.rb
Overview
Defining class method inside a module
Defined Under Namespace
Modules: APIOperations, JSON, Util
Classes: APIConnectionError, APIError, APIResource, AuthenticationError, Configuration, Customer, DatabaseError, Device, DeviceInstance, InvalidRequestError, MachineShopError, MachineShopObject, MachineshopCache, Mapping, Meter, Report, Rule, User, Utility
Constant Summary
collapse
- VERSION =
"0.0.3"
- @@api_base_url =
'http://stage.services.machineshop.io/api/v0'
Class Attribute Summary collapse
Class Method Summary
collapse
-
.api_base_url ⇒ Object
-
.api_base_url=(api_base_url) ⇒ Object
-
.api_error(error, rcode, rbody, error_obj) ⇒ Object
-
.authentication_error(error, rcode, rbody, error_obj) ⇒ Object
-
.configure {|configuration| ... } ⇒ Object
-
.delete(url, auth_token, body_hash) ⇒ Object
-
.execute_request(opts) ⇒ Object
-
.get(url, auth_token, body_hash = nil) ⇒ Object
-
.handle_api_error(rcode, rbody) ⇒ Object
-
.handle_restclient_error(e) ⇒ Object
-
.headers(auth_token) ⇒ Object
-
.invalid_request_error(error, rcode, rbody, error_obj) ⇒ Object
-
.platform_request(url, auth_token, body_hash = nil, http_verb = :get) ⇒ Object
-
.post(url, auth_token, body_hash) ⇒ Object
-
.put(url, auth_token, body_hash) ⇒ Object
-
.reset ⇒ Object
Class Attribute Details
.configuration ⇒ Object
57
58
59
|
# File 'lib/machineshop.rb', line 57
def configuration
@configuration ||= Configuration.new
end
|
Class Method Details
.api_base_url ⇒ Object
76
77
78
|
# File 'lib/machineshop.rb', line 76
def api_base_url
@@api_base_url
end
|
.api_base_url=(api_base_url) ⇒ Object
72
73
74
|
# File 'lib/machineshop.rb', line 72
def api_base_url=(api_base_url)
@@api_base_url = api_base_url
end
|
.api_error(error, rcode, rbody, error_obj) ⇒ Object
211
212
213
|
# File 'lib/machineshop.rb', line 211
def api_error(error, rcode, rbody, error_obj)
APIError.new(error, rcode, rbody, error_obj)
end
|
.authentication_error(error, rcode, rbody, error_obj) ⇒ Object
207
208
209
|
# File 'lib/machineshop.rb', line 207
def authentication_error(error, rcode, rbody, error_obj)
AuthenticationError.new(error, rcode, rbody, error_obj)
end
|
61
62
63
|
# File 'lib/machineshop.rb', line 61
def configure
yield(configuration)
end
|
.delete(url, auth_token, body_hash) ⇒ Object
88
89
90
|
# File 'lib/machineshop.rb', line 88
def delete(url, auth_token, body_hash)
platform_request(url, auth_token, body_hash, :delete)
end
|
.execute_request(opts) ⇒ Object
178
179
180
|
# File 'lib/machineshop.rb', line 178
def execute_request(opts)
RestClient::Request.execute(opts)
end
|
.get(url, auth_token, body_hash = nil) ⇒ Object
80
81
82
|
# File 'lib/machineshop.rb', line 80
def get(url, auth_token, body_hash=nil)
platform_request(url, auth_token, body_hash)
end
|
.handle_api_error(rcode, rbody) ⇒ Object
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
# File 'lib/machineshop.rb', line 182
def handle_api_error(rcode, rbody)
begin
error_obj = MachineShop::JSON.load(rbody)
error_obj = Util.symbolize_names(error_obj)
error = error_obj[:error] or raise MachineShopError.new
rescue MultiJson::DecodeError, MachineShopError
raise APIError.new("Invalid response object from API: #{rbody.inspect} (HTTP response code was #{rcode})", rcode, rbody)
end
case rcode
when 400, 404 then
raise invalid_request_error(error, rcode, rbody, error_obj)
when 401
raise authentication_error(error, rcode, rbody, error_obj)
when 402
else
raise api_error(error, rcode, rbody, error_obj)
end
end
|
.handle_restclient_error(e) ⇒ Object
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
# File 'lib/machineshop.rb', line 215
def handle_restclient_error(e)
case e
when RestClient::ServerBrokeConnection, RestClient::RequestTimeout
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."
when RestClient::SSLCertificateNotVerified
message = "Could not verify MachineShops's SSL certificate. Please make sure that your network is not intercepting certificates."
when SocketError
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."
else
message = "Unexpected error communicating with MachineShop"
end
message += "\n\n(Network error: #{e.message})"
raise APIConnectionError.new(message)
end
|
96
97
98
99
100
101
|
# File 'lib/machineshop.rb', line 96
def (auth_token)
={:content_type => :json,
:accept => :json}
.merge!({ authorization: "Basic " + Base64.encode64(auth_token + ':X') }) if auth_token
end
|
.invalid_request_error(error, rcode, rbody, error_obj) ⇒ Object
203
204
205
|
# File 'lib/machineshop.rb', line 203
def invalid_request_error(error, rcode, rbody, error_obj)
InvalidRequestError.new(error, error, rcode, rbody, error_obj)
end
|
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/machineshop.rb', line 103
def platform_request(url, auth_token, body_hash=nil, http_verb=:get )
puts "body_hash: #{body_hash}"
opts = nil
api_uri = api_base_url + url
= self.(auth_token)
if http_verb == :get
if (body_hash && !body_hash.empty?)
uri = Addressable::URI.new
uri.query_values = body_hash
api_uri += "?" + uri.query
end
opts = {
:method => :get,
:url => api_uri,
:headers => ,
:open_timeout => 30,
:timeout => 80
}
else
opts = {
:method => http_verb,
:url => api_uri,
:headers => ,
:open_timeout => 30,
:payload => MachineShop::JSON.dump(body_hash),
:timeout => 80
}
end
puts "request params: #{opts} "
begin
response = execute_request(opts)
rescue SocketError => e
self.handle_restclient_error(e)
rescue NoMethodError => e
if e.message =~ /\WRequestFailed\W/
e = APIConnectionError.new('Unexpected HTTP response code')
self.handle_restclient_error(e)
else
raise
end
rescue RestClient::ExceptionWithResponse => e
if rcode = e.http_code and rbody = e.http_body
self.handle_api_error(rcode, rbody)
else
self.handle_restclient_error(e)
end
rescue RestClient::Exception, Errno::ECONNREFUSED => e
self.handle_restclient_error(e)
end
rbody = response.body
rcode = response.code
begin
resp = MachineShop::JSON.load(rbody)
resp ||= {}
resp = Util.symbolize_names(resp)
resp.merge!({:http_code => rcode}) if resp.is_a?(Hash)
return resp
rescue MultiJson::DecodeError
raise APIError.new("Invalid response object from API: #{rbody.inspect} (HTTP response code was #{rcode})", rcode, rbody)
end
end
|
.post(url, auth_token, body_hash) ⇒ Object
84
85
86
|
# File 'lib/machineshop.rb', line 84
def post(url, auth_token, body_hash)
platform_request(url, auth_token, body_hash, :post)
end
|
.put(url, auth_token, body_hash) ⇒ Object
92
93
94
|
# File 'lib/machineshop.rb', line 92
def put(url, auth_token, body_hash)
platform_request(url, auth_token, body_hash, :put)
end
|
.reset ⇒ Object
66
67
68
|
# File 'lib/machineshop.rb', line 66
def reset
Configuration.new
end
|