Class: Fulfil::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/fulfil/client.rb

Defined Under Namespace

Classes: ConnectionError, InvalidClientError, NotAuthorizedError, ResponseError, UnknownHTTPError

Instance Method Summary collapse

Constructor Details

#initialize(subdomain: SUBDOMAIN, token: oauth_token, api_key: API_KEY, headers: nil, debug: false) ⇒ Client

Returns a new instance of Client.

Raises:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fulfil/client.rb', line 26

def initialize(subdomain: SUBDOMAIN, token: oauth_token, api_key: API_KEY, headers: nil, debug: false)
  @subdomain = subdomain
  @debug = debug

  normalized_api_key = api_key || headers&.[]('X-API-KEY') || headers&.[]('X-Api-Key')

  if !token.to_s.empty?
    @token = token
  elsif !normalized_api_key.to_s.empty?
    @api_key = normalized_api_key
  else
    raise InvalidClientError, 'No token or API key provided.'
  end

  raise InvalidClientError if invalid?
end

Instance Method Details

#count(model:, domain:) ⇒ Object



85
86
87
88
89
90
# File 'lib/fulfil/client.rb', line 85

def count(model:, domain:)
  uri = URI("#{model_url(model: model)}/search_count")
  body = [domain]

  request(verb: :put, endpoint: uri, json: body)
end

#delete(model:, id:) ⇒ Object



107
108
109
110
111
112
# File 'lib/fulfil/client.rb', line 107

def delete(model:, id:)
  uri = URI(model_url(model: model, id: id))

  result = request(verb: :delete, endpoint: uri)
  parse(result: result)
end

#find(model:, ids: [], id: nil, fields: %w[id rec_name])) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/fulfil/client.rb', line 51

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) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/fulfil/client.rb', line 69

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:) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/fulfil/client.rb', line 61

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

#interactive_report(endpoint:, body: nil) ⇒ Object



114
115
116
117
118
# File 'lib/fulfil/client.rb', line 114

def interactive_report(endpoint:, body: nil)
  uri = URI("#{base_url}/model/#{endpoint}")
  result = request(verb: :put, endpoint: uri, json: body)
  parse(result: result)
end

#invalid?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/fulfil/client.rb', line 43

def invalid?
  @subdomain.nil? || @subdomain.empty?
end

#post(model:, body: {}) ⇒ Object



92
93
94
95
96
97
# File 'lib/fulfil/client.rb', line 92

def post(model:, body: {})
  uri = URI(model_url(model: model))

  results = request(verb: :post, endpoint: uri, json: body)
  parse(results: results)
end

#put(model: nil, id: nil, endpoint: nil, body: {}) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/fulfil/client.rb', line 99

def put(model: nil, id: nil, 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])) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/fulfil/client.rb', line 77

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

#valid?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/fulfil/client.rb', line 47

def valid?
  !invalid?
end