class Pin::Base
Public Class Methods
all(options = {})
click to toggle source
# File lib/pin-payments/base.rb, line 26 def all(options = {}) options = {path: api_path, page: 1}.merge(options) paging = {page: options[:page]} unless options[:page] == 1 build_collection_from_response(authenticated_get(options[:path], paging)) end
create(options, path = api_path)
click to toggle source
# File lib/pin-payments/base.rb, line 17 def create(options, path = api_path) response = authenticated_post(path, options) if response.code == 201 # object created build_instance_from_response(response) else raise Pin::APIError.new(response) end end
find(token, options = {})
click to toggle source
# File lib/pin-payments/base.rb, line 39 def find(token, options = {}) options = options.merge(path: api_path) build_instance_from_response(authenticated_get("#{options[:path]}/#{token}")) end
first(options = {})
click to toggle source
# File lib/pin-payments/base.rb, line 32 def first(options = {}) all(options).first end
last(options = {})
click to toggle source
# File lib/pin-payments/base.rb, line 35 def last(options = {}) all(options).last end
new(attributes = {})
click to toggle source
# File lib/pin-payments/base.rb, line 5 def initialize(attributes = {}) attributes.each do |name, value| if name == 'card' # TODO: this should be generalised (has_one relationship i suppose) self.card = Card.new value else send("#{name}=", value) if respond_to? "#{name}=" end end end
Protected Class Methods
api_path()
click to toggle source
# File lib/pin-payments/base.rb, line 49 def api_path short = if i = name.rindex('::') name[(i+2)..-1] else name end "/#{short}s".downcase end
auth()
click to toggle source
# File lib/pin-payments/base.rb, line 45 def auth Pin.auth end
authenticated_get(url, query = nil)
click to toggle source
# File lib/pin-payments/base.rb, line 61 def authenticated_get(url, query = nil) get(url, query: query, basic_auth: auth) end
authenticated_post(url, body)
click to toggle source
# File lib/pin-payments/base.rb, line 58 def authenticated_post(url, body) post(url, body: body, basic_auth: auth) end
build_collection_from_response(response)
click to toggle source
# File lib/pin-payments/base.rb, line 69 def build_collection_from_response(response) models = [] if response.code == 200 response.parsed_response['response'].each do |model| models << new(model) end end pg = response.parsed_response['pagination'] CollectionResponse.new(models, pg['per_page'], pg['pages'], pg['current']) end
build_instance_from_response(response)
click to toggle source
# File lib/pin-payments/base.rb, line 65 def build_instance_from_response(response) new(response.parsed_response['response']) end