Module: BoffinIO
- Defined in:
- lib/boffinio.rb,
lib/boffinio/plan.rb,
lib/boffinio/util.rb,
lib/boffinio/version.rb,
lib/boffinio/customer.rb,
lib/boffinio/list_object.rb,
lib/boffinio/api_resource.rb,
lib/boffinio/subscription.rb,
lib/boffinio/boffinio_object.rb,
lib/boffinio/errors/api_error.rb,
lib/boffinio/api_operations/list.rb,
lib/boffinio/api_operations/create.rb,
lib/boffinio/api_operations/delete.rb,
lib/boffinio/api_operations/update.rb,
lib/boffinio/errors/boffinio_error.rb,
lib/boffinio/errors/authentication_error.rb,
lib/boffinio/errors/invalid_request_error.rb
Defined Under Namespace
Modules: APIOperations, Util
Classes: APIError, APIResource, AuthenticationError, BoffinIOError, BoffinIOObject, Customer, InvalidRequestError, ListObject, Plan, Subscription
Constant Summary
collapse
- VERSION =
"0.1.2"
Class Attribute Summary collapse
Class Method Summary
collapse
-
.api_error(error, rcode, rbody, error_obj) ⇒ Object
-
.api_url(url = '') ⇒ Object
-
.authentication_error(error, rcode, rbody, error_obj) ⇒ Object
-
.execute_request(opts) ⇒ Object
-
.general_api_error(rcode, rbody) ⇒ Object
-
.get_uname ⇒ Object
-
.handle_api_error(rcode, rbody) ⇒ Object
-
.handle_restclient_error(e) ⇒ Object
-
.invalid_request_error(error, rcode, rbody, error_obj) ⇒ Object
-
.parse(response) ⇒ Object
-
.request(method, url, api_key, params = {}, headers = {}) ⇒ Object
-
.request_headers(api_key) ⇒ Object
-
.uri_encode(params) ⇒ Object
-
.user_agent ⇒ Object
Class Attribute Details
.api_base ⇒ Object
Returns the value of attribute api_base.
36
37
38
|
# File 'lib/boffinio.rb', line 36
def api_base
@api_base
end
|
.api_key ⇒ Object
Returns the value of attribute api_key.
36
37
38
|
# File 'lib/boffinio.rb', line 36
def api_key
@api_key
end
|
.api_version ⇒ Object
Returns the value of attribute api_version.
36
37
38
|
# File 'lib/boffinio.rb', line 36
def api_version
@api_version
end
|
.verify_ssl_certs ⇒ Object
Returns the value of attribute verify_ssl_certs.
36
37
38
|
# File 'lib/boffinio.rb', line 36
def verify_ssl_certs
@verify_ssl_certs
end
|
Class Method Details
.api_error(error, rcode, rbody, error_obj) ⇒ Object
188
189
190
|
# File 'lib/boffinio.rb', line 188
def self.api_error(error, rcode, rbody, error_obj)
APIError.new(error[:message], rcode, rbody, error_obj)
end
|
.api_url(url = '') ⇒ Object
39
40
41
|
# File 'lib/boffinio.rb', line 39
def self.api_url(url='')
@api_base+url
end
|
.authentication_error(error, rcode, rbody, error_obj) ⇒ Object
184
185
186
|
# File 'lib/boffinio.rb', line 184
def self.authentication_error(error, rcode, rbody, error_obj)
AuthenticationError.new(error[:message], rcode, rbody, error_obj)
end
|
.execute_request(opts) ⇒ Object
142
143
144
|
# File 'lib/boffinio.rb', line 142
def self.execute_request(opts)
RestClient::Request.execute(opts)
end
|
.general_api_error(rcode, rbody) ⇒ Object
192
193
194
195
|
# File 'lib/boffinio.rb', line 192
def self.general_api_error(rcode, rbody)
BoffinIOError.new("Invalid response object from API: #{rbody.inspect} " +
"(HTTP response code was #{rcode})", rcode, rbody)
end
|
.get_uname ⇒ Object
114
115
116
117
118
|
# File 'lib/boffinio.rb', line 114
def self.get_uname
`uname -a 2>/dev/null`.strip if RUBY_PLATFORM =~ /linux|darwin/i
rescue Errno::ENOMEM => ex "uname lookup failed"
end
|
.handle_api_error(rcode, rbody) ⇒ Object
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
# File 'lib/boffinio.rb', line 158
def self.handle_api_error(rcode, rbody)
begin
error_obj = JSON.parse(rbody)
error_obj = Util.symbolize_names(error_obj)
error = error_obj[:error] or raise BoffinIOError.new
rescue JSON::ParserError, BoffinIOError
raise general_api_error(rcode, rbody)
end
case rcode
when 400, 404
raise invalid_request_error error, rcode, rbody, error_obj
when 401
raise authentication_error error, rcode, rbody, error_obj
else
raise api_error error, rcode, rbody, error_obj
end
end
|
.handle_restclient_error(e) ⇒ Object
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
# File 'lib/boffinio.rb', line 197
def self.handle_restclient_error(e)
connection_message = "Please check your internet connection and try again. " \
"If this problem persists, you should check Boffin's service status at " \
"https://twitter.com/boffinio, or let us know at [email protected]."
case e
when RestClient::RequestTimeout
message = "Could not connect to Boffin (#{@api_base}). #{connection_message}"
when RestClient::ServerBrokeConnection
message = "The connection to the server (#{@api_base}) broke before the " \
"request completed. #{connection_message}"
when RestClient::SSLCertificateNotVerified
message = "Could not verify Boffin's SSL certificate. " \
"Please make sure that your network is not intercepting certificates. " \
"(Try going to https://api.boffin.com/v1 in your browser.) " \
"If this problem persists, let us know at [email protected]."
when SocketError
message = "Unexpected error communicating when trying to connect to Boffin. " \
"You may be seeing this message because your DNS is not working. " \
"To check, try running 'host boffin.io' from the command line."
else
message = "Unexpected error communicating with Boffin. " \
"If this problem persists, let us know at [email protected]."
end
raise BoffinIOError.new(message + "\n\n(Network error: #{e.message})")
end
|
.invalid_request_error(error, rcode, rbody, error_obj) ⇒ Object
179
180
181
182
|
# File 'lib/boffinio.rb', line 179
def self.invalid_request_error(error, rcode, rbody, error_obj)
InvalidRequestError.new(error[:message], error[:param], rcode,
rbody, error_obj)
end
|
.parse(response) ⇒ Object
146
147
148
149
150
151
152
153
154
155
156
|
# File 'lib/boffinio.rb', line 146
def self.parse(response)
begin
response = JSON.parse(response.body)
rescue JSON::ParserError
raise general_api_error(response.code, response.body)
end
Util.symbolize_names(response)
end
|
.request(method, url, api_key, params = {}, headers = {}) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/boffinio.rb', line 43
def self.request(method, url, api_key, params={}, ={})
unless api_key ||= @api_key
raise AuthenticationError.new('No API key provided. ' +
'Set your API key using "BoffinIO.api_key = <API-KEY>". ' +
'You can generate API keys from the Boffin web interface. ')
end
if api_key =~ /\s/
raise AuthenticationError.new('Your API key is invalid, as it contains ' +
'whitespace. (HINT: You can double-check your API key from the ' +
'Boffin web interface. See https://boffion.io/api for details, or ' +
'email [email protected] if you have any questions.)')
end
request_opts = { :verify_ssl => false, :ssl_version => 'TLSv1' }
params = Util.objects_to_ids(params)
url = api_url(url)
case method.to_s.downcase.to_sym
when :get, :head, :delete
url += "#{URI.parse(url).query ? '&' : '?'}#{uri_encode(params)}" if params && params.any?
payload = nil
else
payload = uri_encode(params)
end
request_opts.update(:headers => (api_key).update(),
:method => method, :open_timeout => 30,
:payload => payload, :url => url, :timeout => 80)
begin
response = execute_request(request_opts)
rescue SocketError => e
handle_restclient_error(e)
rescue NoMethodError => e
if e.message =~ /\WRequestFailed\W/
e = APIConnectionError.new('Unexpected HTTP response code')
handle_restclient_error(e)
else
raise
end
rescue RestClient::ExceptionWithResponse => e
if rcode = e.http_code and rbody = e.http_body
handle_api_error(rcode, rbody)
else
handle_restclient_error(e)
end
rescue RestClient::Exception, Errno::ECONNREFUSED => e
handle_restclient_error(e)
end
[parse(response), api_key]
end
|
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/boffinio.rb', line 120
def self.(api_key)
= {
:user_agent => "Boffin/v1 RubyBindings/#{BoffinIO::VERSION}",
:authorization => "Token token=#{api_key}",
:content_type => 'application/x-www-form-urlencoded'
}
[:boffinio_version] = api_version if api_version
begin
.update(:x_boffinio_client_user_agent => JSON.generate(user_agent))
rescue => e
.update(:x_boffinio_client_raw_user_agent => user_agent.inspect,
:error => "#{e} (#{e.class})")
end
end
|
.uri_encode(params) ⇒ Object
137
138
139
140
|
# File 'lib/boffinio.rb', line 137
def self.uri_encode(params)
Util.flatten_params(params).
map { |k,v| "#{k}=#{Util.url_encode(v)}" }.join('&')
end
|
.user_agent ⇒ Object
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/boffinio.rb', line 99
def self.user_agent
@uname ||= get_uname
lang_version = "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})"
{
:bindings_version => BoffinIO::VERSION,
:lang => 'ruby',
:lang_version => lang_version,
:platform => RUBY_PLATFORM,
:publisher => 'boffinio',
:uname => @uname
}
end
|