Module: Queenbee
- Defined in:
- lib/queenbee.rb,
lib/queenbee/event.rb,
lib/queenbee/order.rb,
lib/queenbee/metrics.rb,
lib/queenbee/version.rb,
lib/queenbee/api_resource.rb,
lib/queenbee/queenbee_object.rb,
lib/queenbee/errors/api_error.rb,
lib/queenbee/api_operations/get.rb,
lib/queenbee/api_operations/create.rb,
lib/queenbee/api_operations/delete.rb,
lib/queenbee/api_operations/update.rb,
lib/queenbee/errors/queenbee_error.rb,
lib/queenbee/errors/api_connection_error.rb,
lib/queenbee/errors/authentication_error.rb,
lib/queenbee/errors/invalid_request_error.rb
Defined Under Namespace
Modules: APIOperations
Classes: APIConnectionError, APIError, APIResource, AuthenticationError, Event, InvalidRequestError, Metrics, Order, QueenbeeError, QueenbeeObject
Constant Summary
collapse
- VERSION =
"1.1.0"
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
-
.general_api_error(rcode, rbody) ⇒ Object
-
.handle_api_error(rcode, rbody) ⇒ Object
-
.handle_connection_error(e) ⇒ Object
-
.invalid_request_error(error, rcode, rbody, error_obj) ⇒ Object
-
.request(method, url, token, params = {}, headers = {}) ⇒ Object
-
.retrieve(id, opts = {}) ⇒ Object
Class Attribute Details
.api_base ⇒ Object
Returns the value of attribute api_base.
36
37
38
|
# File 'lib/queenbee.rb', line 36
def api_base
@api_base
end
|
.api_version ⇒ Object
Returns the value of attribute api_version.
36
37
38
|
# File 'lib/queenbee.rb', line 36
def api_version
@api_version
end
|
.token ⇒ Object
Returns the value of attribute token.
36
37
38
|
# File 'lib/queenbee.rb', line 36
def token
@token
end
|
.verify_ssl_certs ⇒ Object
Returns the value of attribute verify_ssl_certs.
36
37
38
|
# File 'lib/queenbee.rb', line 36
def verify_ssl_certs
@verify_ssl_certs
end
|
Class Method Details
.api_error(error, rcode, rbody, error_obj) ⇒ Object
137
138
139
|
# File 'lib/queenbee.rb', line 137
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/queenbee.rb', line 39
def self.api_url(url="")
@api_base + url
end
|
.authentication_error(error, rcode, rbody, error_obj) ⇒ Object
133
134
135
|
# File 'lib/queenbee.rb', line 133
def self.authentication_error(error, rcode, rbody, error_obj)
AuthenticationError.new(error[:message], rcode, rbody, error_obj)
end
|
.general_api_error(rcode, rbody) ⇒ Object
141
142
143
144
|
# File 'lib/queenbee.rb', line 141
def self.general_api_error(rcode, rbody)
APIError.new("Invalid response object from API: #{rbody.inspect} " +
"(HTTP response code was #{rcode})", rcode, rbody)
end
|
.handle_api_error(rcode, rbody) ⇒ Object
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/queenbee.rb', line 110
def self.handle_api_error(rcode, rbody)
begin
error_obj = JSON.parse(rbody)
rescue JSON::ParserError
raise general_api_error(rcode, rbody)
end
case rcode
when 400, 404, 422
raise invalid_request_error error, rcode, rbody, error_obj
when 401
raise authentication_error error, rcode, rbody, error_obj
when 500
raise api_error error, rcode, rbody, error_obj
else
end
end
|
.handle_connection_error(e) ⇒ Object
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/queenbee.rb', line 97
def self.handle_connection_error(e)
case e
when SocketError
message = "Unexpected error when trying to connect to Queenbee."
when NoMethodError
message = "Unexpected HTTP response code"
else
message = "Unexpected error communicating with Queenbee."
end
raise APIConnectionError.new(message + "\n\n(Network error: #{e.message})")
end
|
.invalid_request_error(error, rcode, rbody, error_obj) ⇒ Object
129
130
131
|
# File 'lib/queenbee.rb', line 129
def self.invalid_request_error(error, rcode, rbody, error_obj)
InvalidRequestError.new(error[:message], error[:param], rcode, rbody, error_obj)
end
|
.request(method, url, token, 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
|
# File 'lib/queenbee.rb', line 43
def self.request(method, url, token, params={}, ={})
unless token ||= @token
raise AuthenticationError.new("No token provided")
end
url = api_url(url)
begin
uri = URI(url)
request = Net::HTTP::Get.new(uri) if method == :get
request = Net::HTTP::Post.new(uri) if method == :post
request = Net::HTTP::Put.new(uri) if method == :put
request = Net::HTTP::Delete.new(uri) if method == :delete
request["User-Agent"] = "Queenbee gem"
request["Authorization"] = "Token token=\"#{token}\""
request["Content-Type"] = "application/json"
request.body = params.to_json
http = Net::HTTP.new(uri.hostname, uri.port)
http.use_ssl = true if uri.scheme == "https"
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if uri.scheme == "https"
response = http.start {|h|
h.request(request)
}
handle_api_error(response.code, response.body)
rescue SocketError => e
handle_connection_error(e)
rescue NoMethodError => e
handle_connection_error(e)
rescue OpenSSL::SSL::SSLError => e
handle_connection_error(e)
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH => e
handle_connection_error(e)
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
handle_connection_error(e)
end
[response, token]
end
|
.retrieve(id, opts = {}) ⇒ Object
92
93
94
95
|
# File 'lib/queenbee.rb', line 92
def self.retrieve(id, opts={})
instance = self.new(id, opts)
instance
end
|