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, headers: { 'X-API-KEY' => API_KEY }, debug: false) ⇒ Client

Returns a new instance of Client.

Raises:



26
27
28
29
30
31
32
33
34
# File 'lib/fulfil/client.rb', line 26

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

  raise InvalidClientError if invalid?
end

Instance Method Details

#count(model:, domain:) ⇒ Object



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

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



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

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



44
45
46
47
48
49
50
51
52
# File 'lib/fulfil/client.rb', line 44

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



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

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



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

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



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

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)


36
37
38
# File 'lib/fulfil/client.rb', line 36

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

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



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

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



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

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



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

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)


40
41
42
# File 'lib/fulfil/client.rb', line 40

def valid?
  !invalid?
end