class Fulfil::Client
Public Class Methods
new(subdomain: SUBDOMAIN, token: OAUTH_TOKEN, headers: { 'X-API-KEY' => API_KEY }, debug: false)
click to toggle source
# File lib/fulfil/client.rb, line 18 def initialize(subdomain: SUBDOMAIN, token: OAUTH_TOKEN, headers: { 'X-API-KEY' => API_KEY }, debug: false) @subdomain = subdomain @token = token @debug = debug @headers = headers @headers.delete('X-API-KEY') if @token end
Public Instance Methods
count(model:, domain:)
click to toggle source
# File lib/fulfil/client.rb, line 60 def count(model:, domain:) uri = URI("#{model_url(model: model)}/search_count") body = [domain] request(verb: :put, endpoint: uri, json: body) end
find(model:, ids: [], id: nil, fields: %w[id rec_name])
click to toggle source
# File lib/fulfil/client.rb, line 26 def find(model:, ids: [], id: nil, fields: %w[id rec_name]) if ids.any? find_many(model: model, ids: ids, fields: fields) elsif !id.nil? find_one(model: model, id: id) else raise end end
find_many(model:, ids:, fields: nil)
click to toggle source
# File lib/fulfil/client.rb, line 44 def find_many(model:, ids:, fields: nil) raise 'missing ids' if ids.empty? uri = URI("#{model_url(model: model)}/read") results = request(verb: :put, endpoint: uri, json: [ids, fields]) parse(results: results) end
find_one(model:, id:)
click to toggle source
# File lib/fulfil/client.rb, line 36 def find_one(model:, id:) raise 'missing id' if id.nil? uri = URI("#{model_url(model: model)}/#{id}") result = request(endpoint: uri) parse(result: result) end
post(model:, body: {})
click to toggle source
# File lib/fulfil/client.rb, line 67 def post(model:, body: {}) uri = URI(model_url(model: model)) results = request(verb: :post, endpoint: uri, json: body) parse(results: results) end
put(model:, id:, endpoint: nil, body: {})
click to toggle source
# File lib/fulfil/client.rb, line 74 def put(model:, id:, endpoint: nil, body: {}) uri = URI(model_url(model: model, id: id, endpoint: endpoint)) result = request(verb: :put, endpoint: uri, json: body) parse(result: result) end
search(model:, domain:, offset: nil, limit: 100, sort: nil, fields: %w[id])
click to toggle source
# File lib/fulfil/client.rb, line 52 def search(model:, domain:, offset: nil, limit: 100, sort: nil, fields: %w[id]) uri = URI("#{model_url(model: model)}/search_read") body = [domain, offset, limit, sort, fields] results = request(verb: :put, endpoint: uri, json: body) parse(results: results) end
Private Instance Methods
base_url()
click to toggle source
# File lib/fulfil/client.rb, line 99 def base_url "https://#{@subdomain}.fulfil.io/api/v2/model" end
client()
click to toggle source
# File lib/fulfil/client.rb, line 130 def client return @client if defined?(@client) @client = HTTP.persistent(base_url).use(logging: @debug ? { logger: Logger.new(STDOUT) } : {}) @client = @client.auth("Bearer #{@token}") if @token @client = @client.headers(@headers) @client end
model_url(model:, id: nil, endpoint: nil)
click to toggle source
# File lib/fulfil/client.rb, line 103 def model_url(model:, id: nil, endpoint: nil) [base_url, model, id, endpoint].compact.join('/') end
parse(result: nil, results: [])
click to toggle source
# File lib/fulfil/client.rb, line 83 def parse(result: nil, results: []) if result parse_single(result: result) else parse_multiple(results: results) end end
parse_multiple(results:)
click to toggle source
# File lib/fulfil/client.rb, line 95 def parse_multiple(results:) results.map { |result| Fulfil::ResponseParser.parse(item: result) } end
parse_single(result:)
click to toggle source
# File lib/fulfil/client.rb, line 91 def parse_single(result:) Fulfil::ResponseParser.parse(item: result) end
request(verb: :get, endpoint:, **args)
click to toggle source
# File lib/fulfil/client.rb, line 107 def request(verb: :get, endpoint:, **args) response = client.request(verb, endpoint, args) if response.status.ok? || response.status.created? response.parse elsif response.code == 401 error = response.parse raise NotAuthorizedError, "Not authorized: #{error['error']}: #{error['error_description']}" else puts response.body.to_s raise Error, 'Error encountered while processing response:' end rescue HTTP::Error => e puts e raise UnknownHTTPError, 'Unhandled HTTP error encountered' rescue HTTP::ConnectionError => e puts "Couldn't connect" raise ConnectionError, "Can't connect to #{base_url}" rescue HTTP::ResponseError => ex raise ResponseError, "Can't process response: #{ex}" [] end