class Wor::Requests::Base
Constants
- COMMON_ATTRIBUTES
According to RFC 7231
- DELETE_ATTRIBUTES
- GET_ATTRIBUTES
- HAS_BODY
- HAS_QUERY
- HTTP_COMPLETE
- HTTP_QUERY_ONLY
- PATCH_ATTRIBUTES
- POST_ATTRIBUTES
- PUT_ATTRIBUTES
- VALID_HTTP_VERBS
Public Instance Methods
logger()
click to toggle source
# File lib/wor/requests/base.rb, line 52 def logger Wor::Requests.logger end
request(options = {}, &block)
click to toggle source
# File lib/wor/requests/base.rb, line 41 def request(options = {}, &block) validate_method!(options[:method]) log_attempt(options[:attempting_to]) resp = HTTParty.send(options[:method], uri(options[:path]), request_parameters(options)) return after_success(resp, options, &block) if resp.success? after_error(resp, options) end
Protected Instance Methods
base_url()
click to toggle source
# File lib/wor/requests/base.rb, line 58 def base_url raise NoMethodError, "Subclass must implement method: '#{__method__}'" end
default_response_type()
click to toggle source
# File lib/wor/requests/base.rb, line 66 def default_response_type Wor::Requests.default_response_type end
external_api_name()
click to toggle source
# File lib/wor/requests/base.rb, line 62 def external_api_name self.class.name end
Private Instance Methods
after_error(response, options)
click to toggle source
# File lib/wor/requests/base.rb, line 80 def after_error(response, options) log_error(response, options[:attempting_to]) raise Wor::Requests::RequestError.new(response), exception_message end
after_success(response, options) { |response| ... }
click to toggle source
# File lib/wor/requests/base.rb, line 72 def after_success(response, options) log_success(options[:attempting_to]) return yield(response) if block_given? handle_response(response, options[:response_type]) end
constantize(string)
click to toggle source
# File lib/wor/requests/base.rb, line 142 def constantize(string) self.class.class_eval(string) end
exception_message()
click to toggle source
# File lib/wor/requests/base.rb, line 126 def exception_message "#{external_api_name} got an error. See logs for more information." end
formatted_base_url()
click to toggle source
# File lib/wor/requests/base.rb, line 89 def formatted_base_url raise MalformedBaseUrl if base_url.nil? base_url[-1] != '/' ? "#{base_url}/" : base_url end
formatted_path(path)
click to toggle source
# File lib/wor/requests/base.rb, line 95 def formatted_path(path) return '' if path.nil? return path if path[0] != '/' path.slice!(0) path end
handle_response(response, response_type)
click to toggle source
# File lib/wor/requests/base.rb, line 158 def handle_response(response, response_type) case response_type(response_type) when :json JSON.parse(response.body) if response.body.present? else response.body end end
log_attempt(attempt)
click to toggle source
# File lib/wor/requests/base.rb, line 109 def log_attempt(attempt) return unless present?(attempt) logger.info "ATTEMPTING TO: #{attempt}" end
log_error(response, attempting_to)
click to toggle source
# File lib/wor/requests/base.rb, line 115 def log_error(response, attempting_to) return unless present?(attempting_to) response_error = "ERROR when trying to #{attempting_to}. " response_error << "Got status code: #{response.code}. " response_error << "Response error: #{JSON.parse(response.body)}" if present?(response.body) logger.error response_error rescue JSON::ParserError => e logger.error("#{response_error} ERROR while parsing response body: #{e.message}.") end
log_success(attempt)
click to toggle source
# File lib/wor/requests/base.rb, line 103 def log_success(attempt) return unless present?(attempt) logger.info "SUCCESS: #{attempt}" end
present?(object)
click to toggle source
# File lib/wor/requests/base.rb, line 138 def present?(object) !object.nil? end
request_parameters(options_hash)
click to toggle source
# File lib/wor/requests/base.rb, line 130 def request_parameters(options_hash) answer = {} answer[:body] = options_hash[:body] if present?(options_hash[:body]) answer[:query] = options_hash[:query] if present?(options_hash[:query]) answer[:headers] = options_hash[:headers] if present?(options_hash[:headers]) answer end
response_type(type)
click to toggle source
# File lib/wor/requests/base.rb, line 152 def response_type(type) return type if Wor::Requests::VALID_RESPONSE_TYPES.include?(type) default_response_type end
uri(path)
click to toggle source
# File lib/wor/requests/base.rb, line 85 def uri(path) URI.join(formatted_base_url, formatted_path(path)) end
validate_method!(method)
click to toggle source
# File lib/wor/requests/base.rb, line 146 def validate_method!(method) return true if VALID_HTTP_VERBS.include?(method) raise ArgumentError, "#{method} is not a valid method." end