class AxTrack::Resource
Constants
- ApiError
- ApiRequestsQuotaReachedError
- BadRequestError
- ForbiddenError
- HTTP_BAD_REQUEST_CODE
- HTTP_FORBIDDEN_CODE
- HTTP_NOT_FOUND_CODE
- HTTP_OK_CODE
- HTTP_UNAUTHORIZED_CODE
- HTTP_UNPROCESSABLE_ENTITY_CODE
- NotFoundError
- UnprocessableEntityError
Attributes
client[R]
response[R]
Public Class Methods
new(client)
click to toggle source
# File lib/ax_track/resource.rb, line 21 def initialize(client) @client = client @response = nil end
Public Instance Methods
error_class(status)
click to toggle source
# File lib/ax_track/resource.rb, line 44 def error_class(status) case status when HTTP_BAD_REQUEST_CODE BadRequestError when HTTP_UNAUTHORIZED_CODE UnauthorizedError when HTTP_NOT_FOUND_CODE, HTTP_FORBIDDEN_CODE NotFoundError when HTTP_UNPROCESSABLE_ENTITY_CODE UnprocessableEntityError else ApiError end end
request(http_method: :get, endpoint:, headers: {}, params: {}, body: {}, result_subset: nil)
click to toggle source
# File lib/ax_track/resource.rb, line 26 def request(http_method: :get, endpoint:, headers: {}, params: {}, body: {}, result_subset: nil) raise "Client not defined" unless defined? @client endpoint = endpoint + "/" unless endpoint[-1] == "/" if body.keys.include?(:picture) body[:picture] = Faraday::FilePart.new(body[:picture].url, body[:picture].content_type, body[:picture].filename) end # client.connection['headers']['Content-Type'] = 'multipart/form-data' if body.key? :picture @response = client.connection.public_send(http_method, endpoint, params.merge(body)) unless response_successful? raise error_class(response.status), "Code: #{response.status}, response: #{response.reason_phrase}" end result_subset ? response[result_subset.to_s] : response end
response_successful?()
click to toggle source
# File lib/ax_track/resource.rb, line 59 def response_successful? response.status == HTTP_OK_CODE end