class CraftyClicks::ApiBase
Constants
- ADDRESS_ENDPOINT
- POSTCODE_ENDPOINT
Attributes
result[RW]
Public Class Methods
new(product:, service:, params: {}, http_method: :get)
click to toggle source
# File lib/crafty_clicks/api_base.rb, line 12 def initialize(product:, service:, params: {}, http_method: :get) @product = self.class.const_get("#{product.upcase}_ENDPOINT") @service = service @params = params.merge(key: CraftyClicks.configuration.api_key) @http_method = http_method end
Public Instance Methods
perform_request()
click to toggle source
# File lib/crafty_clicks/api_base.rb, line 19 def perform_request process_result( RestClient::Request.execute( method: @http_method, url: "#{@product}/#{@service}", payload: @params.to_json, headers: { content_type: :json, accept: :json } ) ) rescue RestClient::Unauthorized, RestClient::Forbidden, RestClient::ResourceNotFound => e raise Exceptions::ApiError.new(e.response), "Unauthorized: #{e.response}" rescue RestClient::InternalServerError => e raise Exceptions::ApiError.new(e.response), "Server error: #{e.response}" end
Private Instance Methods
process_result(request)
click to toggle source
# File lib/crafty_clicks/api_base.rb, line 36 def process_result(request) @result = Oj.safe_load(request) @result = @result.is_a?(Array) ? { 'results' => @result } : @result raise Exceptions::ApiError.new(result), "API Error: #{result}" if @result.keys.map { |k| k =~ /error/ }.any? @result end