fulfil-io 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.circleci/config.yml +43 -0
- data/.gitignore +8 -0
- data/.travis.yml +7 -0
- data/CHANGELOG.md +3 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +63 -0
- data/LICENSE.txt +21 -0
- data/README.md +94 -0
- data/Rakefile +10 -0
- data/bin/authenticate +54 -0
- data/bin/console +13 -0
- data/bin/setup +8 -0
- data/fulfil.gemspec +35 -0
- data/lib/fulfil.rb +8 -0
- data/lib/fulfil/client.rb +100 -0
- data/lib/fulfil/model.rb +82 -0
- data/lib/fulfil/query.rb +115 -0
- data/lib/fulfil/response_parser.rb +49 -0
- data/lib/fulfil/version.rb +3 -0
- metadata +148 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 95fb599e7f5a781ab44fb3026f108c1e9952caad1f6db580989e915c2b9498f6
|
4
|
+
data.tar.gz: 8c56400a193b6ef8de6b60588dd793213b794fe9dc4959068402b4a0816ea4e1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 577b1cb9f71d27cfa533fd8dce8db1e6491f0c9ac8e798b412c6f454ac64879b6ca9e9de86d87670ece787047295d74e533f15d5de343fc0820fd9dfc6aa3a62
|
7
|
+
data.tar.gz: 579ccb9a08afe4c30c2ad9d8627c8f941b6b0f03da5a6397c13f3264061aae227777d7c0929c36dd08b56e53839802a8d3dbbaa2dc73ec65278cc59613501c5c
|
@@ -0,0 +1,43 @@
|
|
1
|
+
version: 2
|
2
|
+
jobs:
|
3
|
+
build:
|
4
|
+
docker:
|
5
|
+
- image: circleci/ruby:2.6.0
|
6
|
+
working_directory: ~/repo
|
7
|
+
|
8
|
+
steps:
|
9
|
+
- checkout
|
10
|
+
|
11
|
+
# Download and cache dependencies
|
12
|
+
- restore_cache:
|
13
|
+
keys:
|
14
|
+
- v1-dependencies-{{ checksum "Gemfile.lock" }}
|
15
|
+
# fallback to using the latest cache if no exact match is found
|
16
|
+
- v1-dependencies-
|
17
|
+
|
18
|
+
- run:
|
19
|
+
name: install dependencies
|
20
|
+
command: |
|
21
|
+
gem install bundler --no-document
|
22
|
+
bundle install --jobs=4 --retry=3 --path vendor/bundle
|
23
|
+
|
24
|
+
- save_cache:
|
25
|
+
paths:
|
26
|
+
- ./vendor/bundle
|
27
|
+
key: v1-dependencies-{{ checksum "Gemfile.lock" }}
|
28
|
+
|
29
|
+
# run tests!
|
30
|
+
- run:
|
31
|
+
name: run tests
|
32
|
+
environment:
|
33
|
+
TESTOPTS: "--ci-dir=/tmp/test-results"
|
34
|
+
command: |
|
35
|
+
mkdir /tmp/test-results
|
36
|
+
bundle exec rake test
|
37
|
+
|
38
|
+
# collect reports
|
39
|
+
- store_test_results:
|
40
|
+
path: /tmp/test-results
|
41
|
+
- store_artifacts:
|
42
|
+
path: /tmp/test-results
|
43
|
+
destination: test-results
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at chris@cdmwebs.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
fulfil (0.1.0)
|
5
|
+
http
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
addressable (2.6.0)
|
11
|
+
public_suffix (>= 2.0.2, < 4.0)
|
12
|
+
ansi (1.5.0)
|
13
|
+
builder (3.2.3)
|
14
|
+
domain_name (0.5.20190701)
|
15
|
+
unf (>= 0.0.5, < 1.0.0)
|
16
|
+
faraday (0.17.1)
|
17
|
+
multipart-post (>= 1.2, < 3)
|
18
|
+
http (4.1.1)
|
19
|
+
addressable (~> 2.3)
|
20
|
+
http-cookie (~> 1.0)
|
21
|
+
http-form_data (~> 2.0)
|
22
|
+
http_parser.rb (~> 0.6.0)
|
23
|
+
http-cookie (1.0.3)
|
24
|
+
domain_name (~> 0.5)
|
25
|
+
http-form_data (2.1.1)
|
26
|
+
http_parser.rb (0.6.0)
|
27
|
+
jwt (2.2.1)
|
28
|
+
minitest (5.11.3)
|
29
|
+
minitest-reporters (1.3.6)
|
30
|
+
ansi
|
31
|
+
builder
|
32
|
+
minitest (>= 5.0)
|
33
|
+
ruby-progressbar
|
34
|
+
multi_json (1.14.1)
|
35
|
+
multi_xml (0.6.0)
|
36
|
+
multipart-post (2.1.1)
|
37
|
+
oauth2 (1.4.2)
|
38
|
+
faraday (>= 0.8, < 2.0)
|
39
|
+
jwt (>= 1.0, < 3.0)
|
40
|
+
multi_json (~> 1.3)
|
41
|
+
multi_xml (~> 0.5)
|
42
|
+
rack (>= 1.2, < 3)
|
43
|
+
public_suffix (3.1.1)
|
44
|
+
rack (2.0.7)
|
45
|
+
rake (10.5.0)
|
46
|
+
ruby-progressbar (1.10.1)
|
47
|
+
unf (0.1.4)
|
48
|
+
unf_ext
|
49
|
+
unf_ext (0.0.7.6)
|
50
|
+
|
51
|
+
PLATFORMS
|
52
|
+
ruby
|
53
|
+
|
54
|
+
DEPENDENCIES
|
55
|
+
bundler (~> 2.0)
|
56
|
+
fulfil!
|
57
|
+
minitest (~> 5.0)
|
58
|
+
minitest-reporters (~> 1.3)
|
59
|
+
oauth2 (~> 1.4)
|
60
|
+
rake (~> 10.0)
|
61
|
+
|
62
|
+
BUNDLED WITH
|
63
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Chris Moore
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
# Fulfil.io Rubygem
|
2
|
+
|
3
|
+
[![CircleCI](https://circleci.com/gh/knowndecimal/fulfil.svg?style=svg&circle-token=da80ea6500af15b3a795a3913efe35742bab94c1)](https://circleci.com/gh/knowndecimal/fulfil)
|
4
|
+
|
5
|
+
[Fulfil.io](https://fulfil.io)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'fulfil'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install fulfil
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'fulfil'
|
27
|
+
|
28
|
+
FULFIL_SUBDOMAIN = 'subdomain'
|
29
|
+
FULFIL_TOKEN = 'token'
|
30
|
+
|
31
|
+
fulfil = Fulfil::Client.new(
|
32
|
+
subdomain: FULFIL_SUBDOMAIN,
|
33
|
+
token: FULFIL_TOKEN,
|
34
|
+
debug: false
|
35
|
+
)
|
36
|
+
|
37
|
+
sale_model = Fulfil::Model.new(
|
38
|
+
client: fulfil,
|
39
|
+
model_name: 'sale.sale'
|
40
|
+
)
|
41
|
+
|
42
|
+
sales = sale_model.search(
|
43
|
+
domain: [['id', '=', [10]]],
|
44
|
+
fields: ['id', 'rec_name', 'lines']
|
45
|
+
)
|
46
|
+
|
47
|
+
# -- OR --
|
48
|
+
|
49
|
+
sale_model.query(id: 100)
|
50
|
+
sale_model.query(ids: 100..150)
|
51
|
+
|
52
|
+
sale_model.fetch_associated(
|
53
|
+
models: sales,
|
54
|
+
association_name: 'sale.line',
|
55
|
+
source_key: 'lines',
|
56
|
+
fields: %w[id unit_price]
|
57
|
+
)
|
58
|
+
|
59
|
+
pp sales
|
60
|
+
|
61
|
+
# [{"id"=>10,
|
62
|
+
# "lines"=>
|
63
|
+
# [{"id"=>311, "unit_price"=>34.95},
|
64
|
+
# {"id"=>313, "unit_price"=>0.0}],
|
65
|
+
# "rec_name"=>""}]
|
66
|
+
```
|
67
|
+
|
68
|
+
## Development
|
69
|
+
|
70
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
71
|
+
`rake test` to run the tests. You can also run `bin/console` for an interactive
|
72
|
+
prompt that will allow you to experiment.
|
73
|
+
|
74
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
75
|
+
release a new version, update the version number in `version.rb`, and then run
|
76
|
+
`bundle exec rake release`, which will create a git tag for the version, push
|
77
|
+
git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
78
|
+
|
79
|
+
## Contributing
|
80
|
+
|
81
|
+
Bug reports and pull requests are welcome on GitHub at
|
82
|
+
https://github.com/knowndecimal/fulfil. This project is intended to be a safe,
|
83
|
+
welcoming space for collaboration, and contributors are expected to adhere to
|
84
|
+
the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
85
|
+
|
86
|
+
## License
|
87
|
+
|
88
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
89
|
+
|
90
|
+
## Code of Conduct
|
91
|
+
|
92
|
+
Everyone interacting in the Fulfil project’s codebases, issue trackers, chat
|
93
|
+
rooms and mailing lists is expected to follow the [code of
|
94
|
+
conduct](https://github.com/[USERNAME]/fulfil/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/authenticate
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# frozen_string_literal: true
|
4
|
+
|
5
|
+
require 'bundler'
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
|
8
|
+
require 'oauth2'
|
9
|
+
require 'fulfil'
|
10
|
+
|
11
|
+
def get_token
|
12
|
+
client_id = ENV.fetch('CLIENT_ID')
|
13
|
+
client_secret = ENV.fetch('CLIENT_SECRET')
|
14
|
+
subdomain = ENV.fetch('SUBDOMAIN')
|
15
|
+
|
16
|
+
base_url = "https://#{subdomain}.fulfil.io" # "oauth/authorize"
|
17
|
+
|
18
|
+
client = OAuth2::Client.new(client_id, client_secret, site: base_url)
|
19
|
+
authorize_url = client.auth_code.authorize_url(
|
20
|
+
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
|
21
|
+
scope: ['sale.channel:read'].join(','),
|
22
|
+
access_type: 'offline_access'
|
23
|
+
)
|
24
|
+
|
25
|
+
output 'Open this URL:'
|
26
|
+
system %x[echo "#{authorize_url.to_s}" | pbcopy]
|
27
|
+
output authorize_url.to_s
|
28
|
+
|
29
|
+
output 'Copy the generated token. Paste it here:'
|
30
|
+
|
31
|
+
auth_token = gets.chomp
|
32
|
+
output ''
|
33
|
+
token = client.auth_code.get_token(auth_token)
|
34
|
+
|
35
|
+
output 'Results:'
|
36
|
+
pp token.to_hash
|
37
|
+
output ''
|
38
|
+
|
39
|
+
output 'Offline access token:'
|
40
|
+
output token.to_hash.dig('offline_access_token')
|
41
|
+
end
|
42
|
+
|
43
|
+
def output(text, lines: 2)
|
44
|
+
puts text
|
45
|
+
puts "\n" * lines
|
46
|
+
end
|
47
|
+
|
48
|
+
def fulfil
|
49
|
+
@fulfil ||= Fulfil::Client.new(
|
50
|
+
subdomain: ENV.fetch('SUBDOMAIN'), token: ENV.fetch('TOKEN')
|
51
|
+
)
|
52
|
+
end
|
53
|
+
|
54
|
+
get_token
|
data/bin/console
ADDED
data/bin/setup
ADDED
data/fulfil.gemspec
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
require 'fulfil/version'
|
7
|
+
|
8
|
+
Gem::Specification.new do |spec|
|
9
|
+
spec.name = 'fulfil-io'
|
10
|
+
spec.version = Fulfil::VERSION
|
11
|
+
spec.authors = ['Chris Moore', 'Kat Fairbanks']
|
12
|
+
spec.email = ['chris@knowndecimal.com']
|
13
|
+
|
14
|
+
spec.summary = 'Interact with the Fulfil.io API'
|
15
|
+
spec.homepage = 'https://github.com/knowndecimal/fulfil'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
end
|
23
|
+
spec.bindir = 'exe'
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ['lib']
|
26
|
+
|
27
|
+
spec.add_dependency 'http'
|
28
|
+
|
29
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
30
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
31
|
+
spec.add_development_dependency 'minitest-ci', '~> 3.4' if ENV['CI']
|
32
|
+
spec.add_development_dependency 'minitest-reporters', '~> 1.3'
|
33
|
+
spec.add_development_dependency 'oauth2', '~> 1.4'
|
34
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
35
|
+
end
|
data/lib/fulfil.rb
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'http'
|
4
|
+
require 'logger'
|
5
|
+
require 'fulfil/response_parser'
|
6
|
+
|
7
|
+
module Fulfil
|
8
|
+
class Client
|
9
|
+
def initialize(subdomain:, token:, debug: false)
|
10
|
+
@subdomain = subdomain
|
11
|
+
@token = token
|
12
|
+
@debug = debug
|
13
|
+
end
|
14
|
+
|
15
|
+
def find(model:, ids: [], id: nil, fields: %w[id rec_name])
|
16
|
+
if ids.any?
|
17
|
+
find_many(model: model, ids: ids, fields: fields)
|
18
|
+
elsif !id.nil?
|
19
|
+
find_one(model: model, id: id)
|
20
|
+
else
|
21
|
+
raise
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def find_one(model:, id:)
|
26
|
+
raise 'missing id' if id.nil?
|
27
|
+
|
28
|
+
uri = URI("#{model_url(model: model)}/#{id}")
|
29
|
+
result = request(endpoint: uri)
|
30
|
+
parse(result: result)
|
31
|
+
end
|
32
|
+
|
33
|
+
def find_many(model:, ids:, fields: nil)
|
34
|
+
raise 'missing ids' if ids.empty?
|
35
|
+
|
36
|
+
uri = URI("#{model_url(model: model)}/read")
|
37
|
+
results = request(verb: :put, endpoint: uri, json: [ids, fields])
|
38
|
+
parse(results: results)
|
39
|
+
end
|
40
|
+
|
41
|
+
def search(
|
42
|
+
model:, domain:, offset: nil, limit: 100, sort: nil, fields: %w[id]
|
43
|
+
)
|
44
|
+
uri = URI("#{model_url(model: model)}/search_read")
|
45
|
+
body = [domain, offset, limit, sort, fields]
|
46
|
+
|
47
|
+
results = request(verb: :put, endpoint: uri, json: body)
|
48
|
+
parse(results: results)
|
49
|
+
end
|
50
|
+
|
51
|
+
def post(model:, body: {})
|
52
|
+
uri = URI(model_url(model: model))
|
53
|
+
|
54
|
+
results = request(verb: :post, endpoint: uri, json: body)
|
55
|
+
parse(results: results)
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def parse(result: nil, results: [])
|
61
|
+
results.map { |result| Fulfil::ResponseParser.parse(item: result) }
|
62
|
+
end
|
63
|
+
|
64
|
+
def base_url
|
65
|
+
"https://#{@subdomain}.fulfil.io/api/v2/model"
|
66
|
+
end
|
67
|
+
|
68
|
+
def model_url(model:)
|
69
|
+
[base_url, model].join('/')
|
70
|
+
end
|
71
|
+
|
72
|
+
def request(verb: :get, endpoint:, **args)
|
73
|
+
response = client.request(verb, endpoint, args)
|
74
|
+
|
75
|
+
if response.status.ok? || response.status.created?
|
76
|
+
response.parse
|
77
|
+
elsif response.code == 401
|
78
|
+
raise StandardError, 'Not authorized'
|
79
|
+
else
|
80
|
+
puts response.body.to_s
|
81
|
+
raise Error, 'Error encountered while processing response:'
|
82
|
+
end
|
83
|
+
rescue HTTP::Error => e
|
84
|
+
puts e
|
85
|
+
raise Error, 'Unhandled HTTP error encountered'
|
86
|
+
rescue HTTP::ConnectionError => e
|
87
|
+
puts "Couldn't connect"
|
88
|
+
raise Error, "Can't connect to #{base_url}"
|
89
|
+
rescue HTTP::ResponseError => e
|
90
|
+
raise Error, "Can't process response: #{e}"
|
91
|
+
[]
|
92
|
+
end
|
93
|
+
|
94
|
+
def client
|
95
|
+
@client ||=
|
96
|
+
HTTP.persistent(base_url).auth("Bearer #{@token}")
|
97
|
+
.use(logging: @debug ? { logger: Logger.new(STDOUT) } : {})
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
data/lib/fulfil/model.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'fulfil/query'
|
2
|
+
|
3
|
+
module Fulfil
|
4
|
+
class Model
|
5
|
+
attr_reader :model_name
|
6
|
+
|
7
|
+
def initialize(client:, model_name:)
|
8
|
+
@client = client
|
9
|
+
@model_name = model_name
|
10
|
+
@query ||= Fulfil::Query.new
|
11
|
+
end
|
12
|
+
|
13
|
+
# Delegate this to the client, including the model_name so we don't have to
|
14
|
+
# type it every time.
|
15
|
+
def find(model: model_name, id:)
|
16
|
+
@client.find(model: model, id: id)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Delegate this to the client, including the model_name so we don't have to
|
20
|
+
# type it every time.
|
21
|
+
def search(
|
22
|
+
model: model_name,
|
23
|
+
domain:,
|
24
|
+
fields: %w[id rec_name],
|
25
|
+
limit: nil,
|
26
|
+
offset: nil,
|
27
|
+
sort: nil
|
28
|
+
)
|
29
|
+
@client.search(
|
30
|
+
model: model,
|
31
|
+
domain: domain,
|
32
|
+
fields: fields,
|
33
|
+
limit: limit,
|
34
|
+
offset: offset,
|
35
|
+
sort: sort
|
36
|
+
)
|
37
|
+
end
|
38
|
+
|
39
|
+
def all
|
40
|
+
search(domain: query)
|
41
|
+
end
|
42
|
+
|
43
|
+
def query(**args)
|
44
|
+
@query.search(**args).query if args.any?
|
45
|
+
@query.query
|
46
|
+
end
|
47
|
+
|
48
|
+
def attributes
|
49
|
+
results = @client.search(model: model_name, domain: [], limit: 1)
|
50
|
+
@client.find(model: model_name, id: results.first.dig('id'))
|
51
|
+
end
|
52
|
+
|
53
|
+
def fetch_associated(models:, association_name:, source_key:, fields:)
|
54
|
+
source_keys = source_key.split('.')
|
55
|
+
associated_ids =
|
56
|
+
models.map { |model| model.dig(*source_keys) }.flatten.compact.uniq
|
57
|
+
|
58
|
+
return [] if associated_ids.none?
|
59
|
+
|
60
|
+
associated_models =
|
61
|
+
@client.find(
|
62
|
+
model: association_name, ids: associated_ids, fields: fields
|
63
|
+
)
|
64
|
+
|
65
|
+
associated_models_by_id = associated_models.map { |m| [m['id'], m] }.to_h
|
66
|
+
|
67
|
+
models.each do |model|
|
68
|
+
filtered_models =
|
69
|
+
model.dig(*source_keys).map { |id| associated_models_by_id[id] }
|
70
|
+
|
71
|
+
if source_keys.length > 1
|
72
|
+
model.dig(*source_keys[0..-2]).store(
|
73
|
+
source_keys.last,
|
74
|
+
filtered_models
|
75
|
+
)
|
76
|
+
else
|
77
|
+
model[source_keys.first] = filtered_models
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
data/lib/fulfil/query.rb
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
module Fulfil
|
2
|
+
class Query
|
3
|
+
def initialize
|
4
|
+
@matchers = []
|
5
|
+
end
|
6
|
+
|
7
|
+
def query
|
8
|
+
@matchers
|
9
|
+
end
|
10
|
+
|
11
|
+
def search(*args)
|
12
|
+
options = args.first { |arg| arg.is_a?(Hash) && arg.keys.include?(:options) }.fetch(:options, {})
|
13
|
+
|
14
|
+
args.each do |arg|
|
15
|
+
arg.each do |field, value|
|
16
|
+
next if value == options
|
17
|
+
@matchers.concat(build_search_term(field: field, value: value, options: options))
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
def exclude(*args)
|
25
|
+
options = args.first { |arg| arg.is_a?(Hash) && arg.keys.include?(:options) }.fetch(:options, {})
|
26
|
+
|
27
|
+
terms = args.flat_map do |arg|
|
28
|
+
arg.map do |field, value|
|
29
|
+
next if value == options
|
30
|
+
build_exclude_term(field: field, value: value, options: options)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
if terms.length > 1
|
35
|
+
@matchers.push(["OR"].concat(terms))
|
36
|
+
else
|
37
|
+
@matchers.concat(terms.first)
|
38
|
+
end
|
39
|
+
|
40
|
+
self
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
# Fulfil Query Syntax:
|
46
|
+
#
|
47
|
+
# Exact Match: (Integer)
|
48
|
+
# * =
|
49
|
+
#
|
50
|
+
# Case Insensitive Match: (String)
|
51
|
+
# * 'ilike'
|
52
|
+
#
|
53
|
+
# Case Sensitive Match: (String)
|
54
|
+
# * 'like'
|
55
|
+
#
|
56
|
+
# Comparison Operators: (Numeric/Currency, Integer, Float, Date, Datetime)
|
57
|
+
# * >
|
58
|
+
# * >=
|
59
|
+
# * <
|
60
|
+
# * <=
|
61
|
+
# * !=
|
62
|
+
#
|
63
|
+
# IN, NOT IN: (Array)
|
64
|
+
#
|
65
|
+
def build_search_term(prefix: nil, field:, value:, options:)
|
66
|
+
key = [prefix, field.to_s].compact.join('.')
|
67
|
+
|
68
|
+
case value.class.name
|
69
|
+
when 'Array'
|
70
|
+
[[key, 'in', value]]
|
71
|
+
when 'Integer'
|
72
|
+
[[key, '=', value]]
|
73
|
+
when 'Range'
|
74
|
+
[
|
75
|
+
[key, '>=', value.first],
|
76
|
+
[key, '<=', value.last],
|
77
|
+
]
|
78
|
+
when 'String'
|
79
|
+
if options[:case_sensitive]
|
80
|
+
[[key, 'like', value]]
|
81
|
+
else
|
82
|
+
[[key, 'ilike', value]]
|
83
|
+
end
|
84
|
+
when 'Hash'
|
85
|
+
value.flat_map { |nested_field, nested_value|
|
86
|
+
build_search_term(prefix: field, field: nested_field, value: nested_value, options: options)
|
87
|
+
}
|
88
|
+
else
|
89
|
+
raise "Unhandled value type: #{value} (#{value.class.name})"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def build_exclude_term(prefix: nil, field:, value:, options:)
|
94
|
+
key = [prefix, field.to_s].compact.join('.')
|
95
|
+
|
96
|
+
case value.class.name
|
97
|
+
when 'Array'
|
98
|
+
[[key, 'not in', value]]
|
99
|
+
when 'Integer'
|
100
|
+
[[key, '!=', value]]
|
101
|
+
when 'Range'
|
102
|
+
[
|
103
|
+
[key, '<', value.first],
|
104
|
+
[key, '>', value.last],
|
105
|
+
]
|
106
|
+
when 'Hash'
|
107
|
+
value.flat_map { |nested_field, nested_value|
|
108
|
+
build_exclude_term(prefix: field, field: nested_field, value: nested_value, options: options)
|
109
|
+
}
|
110
|
+
else
|
111
|
+
raise "Unhandled value type: #{value} (#{value.class.name})"
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Fulfil
|
2
|
+
module ResponseParser
|
3
|
+
# Handle value objects, for example:
|
4
|
+
#
|
5
|
+
# "order_date": {
|
6
|
+
# "__class__": "date",
|
7
|
+
# "iso_string": "2019-07-08"
|
8
|
+
# }
|
9
|
+
#
|
10
|
+
def self.mapped_value_field(value:)
|
11
|
+
return value unless value.is_a?(Hash) && value.dig('__class__')
|
12
|
+
|
13
|
+
json_class = value.dig('__class__')
|
14
|
+
|
15
|
+
case json_class
|
16
|
+
when 'date'
|
17
|
+
date = value.dig('iso_string')
|
18
|
+
Date.parse(date)
|
19
|
+
when 'datetime'
|
20
|
+
time = value.dig('iso_string')
|
21
|
+
DateTime.parse(time)
|
22
|
+
when 'Decimal'
|
23
|
+
value.dig('decimal').to_f
|
24
|
+
else
|
25
|
+
raise UnhandledTypeError, value
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.group(key_value_tuples)
|
30
|
+
key_value_tuples
|
31
|
+
.group_by { |kv_tuple| kv_tuple[0][0] }
|
32
|
+
.map { |group_key, kv_tuples|
|
33
|
+
if kv_tuples.length == 1
|
34
|
+
[group_key, mapped_value_field(value: kv_tuples[0][1])]
|
35
|
+
else
|
36
|
+
id = kv_tuples[0]
|
37
|
+
attrs = kv_tuples[1..-1].map { |tuple| [tuple[0][1..-1], tuple[1]] }
|
38
|
+
[group_key, [['id', id[1]]].concat(group(attrs)).to_h]
|
39
|
+
end
|
40
|
+
}
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.parse(item:)
|
44
|
+
key_value_tuples = item.to_a.map { |item_tuple| [item_tuple[0].split('.'), item_tuple[1]] }
|
45
|
+
group(key_value_tuples).to_h
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
metadata
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fulfil-io
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Chris Moore
|
8
|
+
- Kat Fairbanks
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2020-06-25 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: http
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: bundler
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '2.0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '2.0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: minitest
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '5.0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '5.0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: minitest-reporters
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - "~>"
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '1.3'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '1.3'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: oauth2
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - "~>"
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '1.4'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - "~>"
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '1.4'
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: rake
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - "~>"
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '10.0'
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - "~>"
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '10.0'
|
98
|
+
description:
|
99
|
+
email:
|
100
|
+
- chris@knowndecimal.com
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".circleci/config.yml"
|
106
|
+
- ".gitignore"
|
107
|
+
- ".travis.yml"
|
108
|
+
- CHANGELOG.md
|
109
|
+
- CODE_OF_CONDUCT.md
|
110
|
+
- Gemfile
|
111
|
+
- Gemfile.lock
|
112
|
+
- LICENSE.txt
|
113
|
+
- README.md
|
114
|
+
- Rakefile
|
115
|
+
- bin/authenticate
|
116
|
+
- bin/console
|
117
|
+
- bin/setup
|
118
|
+
- fulfil.gemspec
|
119
|
+
- lib/fulfil.rb
|
120
|
+
- lib/fulfil/client.rb
|
121
|
+
- lib/fulfil/model.rb
|
122
|
+
- lib/fulfil/query.rb
|
123
|
+
- lib/fulfil/response_parser.rb
|
124
|
+
- lib/fulfil/version.rb
|
125
|
+
homepage: https://github.com/knowndecimal/fulfil
|
126
|
+
licenses:
|
127
|
+
- MIT
|
128
|
+
metadata: {}
|
129
|
+
post_install_message:
|
130
|
+
rdoc_options: []
|
131
|
+
require_paths:
|
132
|
+
- lib
|
133
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - ">="
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '0'
|
138
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
139
|
+
requirements:
|
140
|
+
- - ">="
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: '0'
|
143
|
+
requirements: []
|
144
|
+
rubygems_version: 3.0.3
|
145
|
+
signing_key:
|
146
|
+
specification_version: 4
|
147
|
+
summary: Interact with the Fulfil.io API
|
148
|
+
test_files: []
|