Class: AlPapi::Http
- Inherits:
-
Object
- Object
- AlPapi::Http
- Defined in:
- lib/al_papi/http.rb
Constant Summary collapse
- HANDLED_CODES =
[200, 204, 401, 403]
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#over_limit ⇒ Object
Returns the value of attribute over_limit.
-
#response ⇒ Object
Returns the value of attribute response.
-
#success ⇒ Object
Returns the value of attribute success.
-
#suspended ⇒ Object
Returns the value of attribute suspended.
Instance Method Summary collapse
- #build_params(params = {}) ⇒ Object
- #get(path, params = {}) ⇒ Object
- #handle_200(body, _code, _path, _params) ⇒ Object
- #handle_204(_body, code, path, params) ⇒ Object
- #handle_401(_body, code, path, params) ⇒ Object
- #handle_403(body, code, path, params) ⇒ Object
-
#initialize(config) ⇒ Http
constructor
A new instance of Http.
- #post(path, params = {}) ⇒ Object
- #request(http_verb, path, params = nil) ⇒ Object
Constructor Details
#initialize(config) ⇒ Http
Returns a new instance of Http.
6 7 8 |
# File 'lib/al_papi/http.rb', line 6 def initialize(config) @config, @errors, @success, @over_limit, @suspended = config, [], false, false, false end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
4 5 6 |
# File 'lib/al_papi/http.rb', line 4 def config @config end |
#errors ⇒ Object
Returns the value of attribute errors.
4 5 6 |
# File 'lib/al_papi/http.rb', line 4 def errors @errors end |
#over_limit ⇒ Object
Returns the value of attribute over_limit.
4 5 6 |
# File 'lib/al_papi/http.rb', line 4 def over_limit @over_limit end |
#response ⇒ Object
Returns the value of attribute response.
4 5 6 |
# File 'lib/al_papi/http.rb', line 4 def response @response end |
#success ⇒ Object
Returns the value of attribute success.
4 5 6 |
# File 'lib/al_papi/http.rb', line 4 def success @success end |
#suspended ⇒ Object
Returns the value of attribute suspended.
4 5 6 |
# File 'lib/al_papi/http.rb', line 4 def suspended @suspended end |
Instance Method Details
#build_params(params = {}) ⇒ Object
18 19 20 |
# File 'lib/al_papi/http.rb', line 18 def build_params(params = {}) params.merge(auth_token: @config.api_key, format: 'json') end |
#get(path, params = {}) ⇒ Object
10 11 12 |
# File 'lib/al_papi/http.rb', line 10 def get(path, params = {}) request 'get', "#{path}?#{RestClient::Payload.generate(build_params(params))}" end |
#handle_200(body, _code, _path, _params) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/al_papi/http.rb', line 44 def handle_200(body, _code, _path, _params) # Check valid JSON body. # API returns 'OK' in some cases where response has no # meaningful body, which is invalid JSON, so let it be # a successfull call JSON.parse body unless body == 'OK' self.success = true rescue JSON::ParserError self.response = body end |
#handle_204(_body, code, path, params) ⇒ Object
55 56 57 |
# File 'lib/al_papi/http.rb', line 55 def handle_204(_body, code, path, params) errors << RequestError.new('No Content', code, path, params) end |
#handle_401(_body, code, path, params) ⇒ Object
59 60 61 |
# File 'lib/al_papi/http.rb', line 59 def handle_401(_body, code, path, params) errors << RequestError.new('Invalid Auth Token Provided', code, path, params) end |
#handle_403(body, code, path, params) ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/al_papi/http.rb', line 63 def handle_403(body, code, path, params) if body.match(/Account Suspended/i) self.suspended = true errors << RequestError.new('Account Suspended', code, path, params) elsif body.match(/Request Limit Exceeded/i) self.over_limit = true errors << RequestError.new('Request Limit Exceeded', code, path, params) end end |
#post(path, params = {}) ⇒ Object
14 15 16 |
# File 'lib/al_papi/http.rb', line 14 def post(path, params = {}) request 'post', path, build_params(params) end |
#request(http_verb, path, params = nil) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/al_papi/http.rb', line 22 def request(http_verb, path, params = nil) url = "#{@config.host}#{path}" args = [http_verb, url] args << params if http_verb == 'post' RestClient.send(*args) do |res| body = res.body code = res.code.to_i self.response = body self.errors = [] if HANDLED_CODES.include?(code) send "handle_#{code}", body, code, path, params else errors << RequestError.new(body, code, path, params) end Response.new(self, code, path, params) end end |