#!/usr/bin/env ruby
# Using Auth Tokens in ActiveResource
# Borrowed from http://stackoverflow.com/questions/2918419/add-api-key-to-every-request-in-activeresource/6124110#6124110
require 'active_resource'
require 'awesome_print'
class Resource < ActiveResource::Base
class << self
def element_path_with_auth_token(*args)
args[1]['auth_token'] = self.auth_token
element_path_without_auth_token(*args)
end
def new_element_path_with_auth_token(*args)
args[1]['auth_token'] = self.auth_token
new_element_path_without_auth_token(*args)
end
def collection_path_with_auth_token(*args)
args[1]['auth_token'] = self.auth_token
collection_path_without_auth_token(*args)
end
def inherited(klass)
class << klass
attr_accessor :auth_token
alias_method_chain :element_path, :auth_token
alias_method_chain :new_element_path, :auth_token
alias_method_chain :collection_path, :auth_token
end
klass.site = 'https://apitest.batchbook.com/'
klass.prefix = '/api/v1/'
klass.format = :json
klass.auth_token = 'GR5doLv88FrnLyLGIwok'
end
end
end
class Person < Resource; end
ap Person.collection_path({}, { email: 'dummy@address.com' })
ap Person.first({ email: 'dummy@address.com' })