Class: Camper::Request
- Includes:
- Logging, HTTParty
- Defined in:
- lib/camper/request.rb
Defined Under Namespace
Modules: Result
Constant Summary collapse
- MAX_RETRY_ATTEMPTS =
5
Instance Attribute Summary collapse
-
#attempts ⇒ Object
readonly
Returns the value of attribute attempts.
Attributes included from Logging
Class Method Summary collapse
-
.decode(response) ⇒ Object
Decodes a JSON response into Ruby object.
-
.parse(body) ⇒ Object
Converts the response body to a Resource.
Instance Method Summary collapse
-
#execute ⇒ Object
Executes the request.
-
#initialize(client, method, path, options = {}) ⇒ Request
constructor
A new instance of Request.
- #maxed_attempts? ⇒ Boolean
Constructor Details
#initialize(client, method, path, options = {}) ⇒ Request
Returns a new instance of Request.
26 27 28 29 30 31 32 33 34 |
# File 'lib/camper/request.rb', line 26 def initialize(client, method, path, = {}) @client = client @path = path @options = @attempts = 0 @method = method self.class.headers 'User-Agent' => @client.user_agent end |
Instance Attribute Details
#attempts ⇒ Object (readonly)
Returns the value of attribute attempts.
14 15 16 |
# File 'lib/camper/request.rb', line 14 def attempts @attempts end |
Class Method Details
.decode(response) ⇒ Object
Decodes a JSON response into Ruby object.
56 57 58 59 60 |
# File 'lib/camper/request.rb', line 56 def self.decode(response) response ? JSON.parse(response) : {} rescue JSON::ParserError raise Error::Parsing, 'The response is not a valid JSON' end |
.parse(body) ⇒ Object
Converts the response body to a Resource.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/camper/request.rb', line 37 def self.parse(body) body = decode(body) if body.is_a? Hash Resource.create(body) elsif body.is_a? Array PaginatedResponse.new(body.collect! { |e| Resource.create(e) }) elsif body true elsif !body false elsif body.nil? false else raise Error::Parsing, "Couldn't parse a response body" end end |
Instance Method Details
#execute ⇒ Object
Executes the request
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/camper/request.rb', line 63 def execute endpoint, params = prepare_request_data raise Error::InvalidURL, endpoint unless UrlUtils.basecamp_url?(endpoint) raise Error::TooManyRetries, endpoint if maxed_attempts? @attempts += 1 logger.debug("Method: #{@method}; URL: #{endpoint}") response, result = validate self.class.send(@method, endpoint, params) response = extract_parsed(response) if result == Result::VALID return response, result end |
#maxed_attempts? ⇒ Boolean
79 80 81 |
# File 'lib/camper/request.rb', line 79 def maxed_attempts? @attempts >= MAX_RETRY_ATTEMPTS end |