class Namely::ResourceGateway
Attributes
access_token[R]
endpoint[R]
paged[R]
subdomain[R]
Public Class Methods
new(options)
click to toggle source
# File lib/namely/resource_gateway.rb, line 5 def initialize(options) @access_token = options.fetch(:access_token) @endpoint = options.fetch(:endpoint) @subdomain = options.fetch(:subdomain) @paged = options.fetch(:paged, false) end
Public Instance Methods
create(attributes)
click to toggle source
# File lib/namely/resource_gateway.rb, line 24 def create(attributes) response = post( "/#{endpoint}", endpoint => [attributes] ) extract_id(response) end
json_index()
click to toggle source
# File lib/namely/resource_gateway.rb, line 12 def json_index paged ? json_index_paged : json_index_all end
json_show(id)
click to toggle source
# File lib/namely/resource_gateway.rb, line 16 def json_show(id) get("/#{endpoint}/#{id}")[resource_name].first end
show_head(id)
click to toggle source
# File lib/namely/resource_gateway.rb, line 20 def show_head(id) head("/#{endpoint}/#{id}") end
update(id, changes)
click to toggle source
# File lib/namely/resource_gateway.rb, line 32 def update(id, changes) put("/#{endpoint}/#{id}", endpoint => [changes]) end
Private Instance Methods
extract_id(response)
click to toggle source
# File lib/namely/resource_gateway.rb, line 67 def extract_id(response) JSON.parse(response)[endpoint].first["id"] rescue StandardError => e raise( FailedRequestError, "Couldn't parse \"id\" from response: #{e.message}" ) end
get(path, params = {})
click to toggle source
# File lib/namely/resource_gateway.rb, line 85 def get(path, params = {}) params.merge!(access_token: access_token) JSON.parse(RestClient.get(url(path), accept: :json, params: params)) end
head(path, params = {})
click to toggle source
# File lib/namely/resource_gateway.rb, line 90 def head(path, params = {}) params.merge!(access_token: access_token) RestClient.head(url(path), accept: :json, params: params) end
json_index_all()
click to toggle source
# File lib/namely/resource_gateway.rb, line 38 def json_index_all get("/#{endpoint}", limit: :all)[resource_name] end
json_index_paged()
click to toggle source
# File lib/namely/resource_gateway.rb, line 42 def json_index_paged Enumerator.new do |y| params = {} loop do objects = with_retry { get("/#{endpoint}", params)[resource_name] } break if objects.empty? objects.each { |o| y << o } params[:after] = objects.last["id"] end end end
post(path, params)
click to toggle source
# File lib/namely/resource_gateway.rb, line 95 def post(path, params) params.merge!(access_token: access_token) RestClient.post( url(path), params.to_json, accept: :json, content_type: :json, ) end
put(path, params)
click to toggle source
# File lib/namely/resource_gateway.rb, line 105 def put(path, params) params.merge!(access_token: access_token) RestClient.put( url(path), params.to_json, accept: :json, content_type: :json ) end
resource_name()
click to toggle source
# File lib/namely/resource_gateway.rb, line 59 def resource_name endpoint.split("/").last end
url(path)
click to toggle source
# File lib/namely/resource_gateway.rb, line 63 def url(path) "https://#{subdomain}.namely.com/api/v1#{path}" end
with_retry() { || ... }
click to toggle source
# File lib/namely/resource_gateway.rb, line 76 def with_retry retries ||= 0 yield rescue RestClient::Exception => e raise unless Namely.configuration.http_codes_to_retry.include?(e.http_code) retry if (retries += 1) < Namely.configuration.retries raise end