class Myparcel::API::Base
Base
class for all endpoints
Attributes
authentication[R]
path[RW]
Public Class Methods
new(authentication)
click to toggle source
# File lib/myparcel/api/base.rb, line 8 def initialize(authentication) @authentication = authentication end
Protected Instance Methods
headers_for_shipment(type)
click to toggle source
rubocop:enable MethodLength
# File lib/myparcel/api/base.rb, line 35 def headers_for_shipment(type) case type when :standard then 'application/vnd.shipment+json; charset=utf-8' when :return then 'application/vnd.return_shipment+json; charset=utf-8' when :unrelated then 'application/vnd.unrelated_return_shipment+json; charset=utf-8' else 'application/vnd.shipment+json; charset=utf-8' end end
request(method, path, options = {})
click to toggle source
rubocop:disable MethodLength
# File lib/myparcel/api/base.rb, line 15 def request(method, path, options = {}) url = [authentication.host, path].join '/' httparty_options = { query: options.fetch(:query, {}), body: options.fetch(:body, ''), headers: authentication.headers.update(options[:headers] || {}) } response = HTTParty.send method, url, httparty_options case response.code when 200..201 response when 422 raise "Unprocessable entity for `#{method} #{url}` with #{httparty_options}." else raise 'Something went wrong' end end