Class: BitBucket::ResponseWrapper
- Inherits:
-
Object
- Object
- BitBucket::ResponseWrapper
- Extended by:
- Forwardable
- Includes:
- Pagination, Enumerable
- Defined in:
- lib/bitbucket_rest_api/response_wrapper.rb
Overview
A class responsible for proxing to faraday response
Constant Summary
Constants included from Constants
Constants::ACCEPT, Constants::ACCEPT_CHARSET, Constants::CACHE_CONTROL, Constants::CONTENT_LENGTH, Constants::CONTENT_TYPE, Constants::DATE, Constants::ETAG, Constants::LOCATION, Constants::META_FIRST, Constants::META_LAST, Constants::META_NEXT, Constants::META_PREV, Constants::META_REL, Constants::PARAM_PAGE, Constants::PARAM_START_PAGE, Constants::QUERY_STR_SEP, Constants::RATELIMIT_LIMIT, Constants::RATELIMIT_REMAINING, Constants::SERVER, Constants::USER_AGENT
Instance Attribute Summary collapse
-
#current_api ⇒ Object
readonly
Returns the value of attribute current_api.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #eql?)
Compare the wrapper with other wrapper for equality.
-
#[](key) ⇒ Object
Lookup an attribute from the body if hash, otherwise behave like array index.
-
#body ⇒ Object
Response raw body.
- #body=(value) ⇒ Object
- #client_error? ⇒ Boolean
-
#each ⇒ Object
Iterate over each resource inside the body.
-
#has_key?(key) ⇒ Boolean
Check if body has an attribute.
-
#headers ⇒ Object
Return response headers.
-
#initialize(response, current_api) ⇒ ResponseWrapper
constructor
A new instance of ResponseWrapper.
-
#inspect ⇒ Object
Print only response body.
-
#method_missing(method_name, *args, &block) ⇒ Object
Coerce any method calls for body attributes.
- #redirect? ⇒ Boolean
-
#respond_to?(method_name) ⇒ Boolean
Check if method is defined on the body.
- #server_error? ⇒ Boolean
-
#status ⇒ Object
Response status.
- #success? ⇒ Boolean
-
#to_ary ⇒ Object
Convert the ResponseWrapper into an Array.
-
#to_hash ⇒ Object
Convert the ResponseWrapper into a Hash.
-
#to_s ⇒ Object
Return response body as string.
-
#url ⇒ Object
Request url.
Methods included from Pagination
#auto_paginate, #each_page, #first_page, #has_next_page?, #links, #next_page, #page, #paginated?, #prev_page
Constructor Details
#initialize(response, current_api) ⇒ ResponseWrapper
Returns a new instance of ResponseWrapper.
19 20 21 22 23 |
# File 'lib/bitbucket_rest_api/response_wrapper.rb', line 19 def initialize(response, current_api) @response = response @current_api = current_api @env = response.env end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
Coerce any method calls for body attributes
123 124 125 126 127 128 129 |
# File 'lib/bitbucket_rest_api/response_wrapper.rb', line 123 def method_missing(method_name, *args, &block) if self.has_key?(method_name.to_s) self.[](method_name, &block) else super end end |
Instance Attribute Details
#current_api ⇒ Object (readonly)
Returns the value of attribute current_api.
13 14 15 |
# File 'lib/bitbucket_rest_api/response_wrapper.rb', line 13 def current_api @current_api end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
15 16 17 |
# File 'lib/bitbucket_rest_api/response_wrapper.rb', line 15 def env @env end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
11 12 13 |
# File 'lib/bitbucket_rest_api/response_wrapper.rb', line 11 def response @response end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
Compare the wrapper with other wrapper for equality
149 150 151 152 153 |
# File 'lib/bitbucket_rest_api/response_wrapper.rb', line 149 def ==(other) return false unless other.is_a?(self.class) return false unless (other.respond_to?(:env) && other.respond_to?(:body)) self.env == other.env && self.body == other.body end |
#[](key) ⇒ Object
Lookup an attribute from the body if hash, otherwise behave like array index. Convert any key to string before calling.
81 82 83 84 85 86 87 |
# File 'lib/bitbucket_rest_api/response_wrapper.rb', line 81 def [](key) if self.body.is_a?(Array) self.body[key] else self.body.send(:"#{key}") end end |
#body ⇒ Object
Response raw body
46 47 48 |
# File 'lib/bitbucket_rest_api/response_wrapper.rb', line 46 def body @body ? @body : response.body end |
#body=(value) ⇒ Object
39 40 41 42 |
# File 'lib/bitbucket_rest_api/response_wrapper.rb', line 39 def body=(value) @body = value @env[:body] = value end |
#client_error? ⇒ Boolean
64 65 66 |
# File 'lib/bitbucket_rest_api/response_wrapper.rb', line 64 def client_error? status.to_i >= 400 && status.to_i < 500 end |
#each ⇒ Object
Iterate over each resource inside the body
109 110 111 112 113 |
# File 'lib/bitbucket_rest_api/response_wrapper.rb', line 109 def each body_parts = self.body.respond_to?(:each) ? self.body : [self.body] return body_parts.to_enum unless block_given? body_parts.each { |part| yield(part) } end |
#has_key?(key) ⇒ Boolean
Check if body has an attribute
117 118 119 |
# File 'lib/bitbucket_rest_api/response_wrapper.rb', line 117 def has_key?(key) self.body.is_a?(Hash) && self.body.has_key?(key) end |
#headers ⇒ Object
Return response headers
74 75 76 |
# File 'lib/bitbucket_rest_api/response_wrapper.rb', line 74 def headers BitBucket::Response::Header.new(env) end |
#inspect ⇒ Object
Print only response body
143 144 145 |
# File 'lib/bitbucket_rest_api/response_wrapper.rb', line 143 def inspect "#<#{self.class.name} @body=\"#{self.body}\">" end |
#redirect? ⇒ Boolean
60 61 62 |
# File 'lib/bitbucket_rest_api/response_wrapper.rb', line 60 def redirect? status.to_i >= 300 && status.to_i < 400 end |
#respond_to?(method_name) ⇒ Boolean
Check if method is defined on the body
133 134 135 136 137 138 139 |
# File 'lib/bitbucket_rest_api/response_wrapper.rb', line 133 def respond_to?(method_name) if self.has_key?(method_name.to_s) true else super end end |
#server_error? ⇒ Boolean
68 69 70 |
# File 'lib/bitbucket_rest_api/response_wrapper.rb', line 68 def server_error? status.to_i >= 500 && status.to_i < 600 end |
#status ⇒ Object
Response status
52 53 54 |
# File 'lib/bitbucket_rest_api/response_wrapper.rb', line 52 def status response.status end |
#success? ⇒ Boolean
56 57 58 |
# File 'lib/bitbucket_rest_api/response_wrapper.rb', line 56 def success? response.success? end |
#to_ary ⇒ Object
Convert the ResponseWrapper into an Array
103 104 105 |
# File 'lib/bitbucket_rest_api/response_wrapper.rb', line 103 def to_ary body.to_ary end |
#to_hash ⇒ Object
Convert the ResponseWrapper into a Hash
97 98 99 |
# File 'lib/bitbucket_rest_api/response_wrapper.rb', line 97 def to_hash body.to_hash end |
#to_s ⇒ Object
Return response body as string
91 92 93 |
# File 'lib/bitbucket_rest_api/response_wrapper.rb', line 91 def to_s body.to_s end |
#url ⇒ Object
Request url
35 36 37 |
# File 'lib/bitbucket_rest_api/response_wrapper.rb', line 35 def url response.env[:url].to_s end |