artemis_api 0.2.1 → 0.7.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.ruby-version +1 -0
- data/Gemfile.lock +22 -14
- data/History.txt +11 -0
- data/README.md +28 -4
- data/artemis_api.gemspec +8 -7
- data/bin/console +2 -6
- data/lib/artemis_api.rb +7 -0
- data/lib/artemis_api/batch.rb +6 -4
- data/lib/artemis_api/client.rb +95 -28
- data/lib/artemis_api/completion.rb +1 -0
- data/lib/artemis_api/crop_batch_state.rb +5 -0
- data/lib/artemis_api/crop_variety.rb +5 -0
- data/lib/artemis_api/custom_data.rb +5 -0
- data/lib/artemis_api/custom_fields.rb +5 -0
- data/lib/artemis_api/facility.rb +26 -10
- data/lib/artemis_api/model.rb +64 -21
- data/lib/artemis_api/resource_unit.rb +14 -0
- data/lib/artemis_api/stage.rb +15 -0
- data/lib/artemis_api/sub_stage.rb +5 -0
- data/lib/artemis_api/subscription.rb +4 -4
- data/lib/artemis_api/user.rb +1 -1
- data/lib/artemis_api/version.rb +1 -1
- data/lib/artemis_api/zone.rb +1 -0
- data/test/batch_test.rb +36 -0
- data/test/client_test.rb +41 -0
- data/test/completion_test.rb +15 -1
- data/test/discard_test.rb +8 -0
- data/test/resource_unit_test.rb +33 -0
- data/test/stage_test.rb +39 -0
- data/test/subscription_test.rb +2 -2
- data/test/zone_test.rb +8 -0
- metadata +44 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: bf8193d6392796c8274e2acb172f6de84afa46ae87bda869c4bf8878225e3b61
|
4
|
+
data.tar.gz: c2b81d0f22021e493588bad0796f4864fa3e09eb77afa098e7e572c974fc2641
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 49e421a1abfdedd4381fbb71caceb12deac62eb1ce8ddb43eb0facdda3cdf0a19581c76e5228a58782aca0ee7bbccc7b8d51da843575dbb45859fbf5d1cdd240
|
7
|
+
data.tar.gz: 3e160124c0562942f0c993f979a21e539ef686a9aa3d99b3c0b756e775dd2349bf2955ba14ea6fb3e0e2128dc4982c4085595336ad77adfa2247882793e2feef
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.6.0
|
data/Gemfile.lock
CHANGED
@@ -1,48 +1,56 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
artemis_api (0.
|
4
|
+
artemis_api (0.7.5)
|
5
|
+
oauth2
|
5
6
|
|
6
7
|
GEM
|
7
8
|
remote: https://rubygems.org/
|
8
9
|
specs:
|
9
|
-
activesupport (
|
10
|
+
activesupport (6.0.3.1)
|
10
11
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
11
12
|
i18n (>= 0.7, < 2)
|
12
13
|
minitest (~> 5.1)
|
13
14
|
tzinfo (~> 1.1)
|
15
|
+
zeitwerk (~> 2.2, >= 2.2.2)
|
14
16
|
addressable (2.6.0)
|
15
17
|
public_suffix (>= 2.0.2, < 4.0)
|
16
|
-
|
18
|
+
coderay (1.1.2)
|
19
|
+
concurrent-ruby (1.1.6)
|
17
20
|
crack (0.4.3)
|
18
21
|
safe_yaml (~> 1.0.0)
|
19
|
-
faraday (0.
|
22
|
+
faraday (1.0.1)
|
20
23
|
multipart-post (>= 1.2, < 3)
|
21
24
|
hashdiff (0.4.0)
|
22
|
-
i18n (1.
|
25
|
+
i18n (1.8.2)
|
23
26
|
concurrent-ruby (~> 1.0)
|
24
27
|
jwt (2.2.1)
|
28
|
+
method_source (1.0.0)
|
25
29
|
minitest (5.11.3)
|
26
|
-
multi_json (1.
|
30
|
+
multi_json (1.14.1)
|
27
31
|
multi_xml (0.6.0)
|
28
32
|
multipart-post (2.1.1)
|
29
|
-
oauth2 (1.4.
|
30
|
-
faraday (>= 0.8, <
|
33
|
+
oauth2 (1.4.4)
|
34
|
+
faraday (>= 0.8, < 2.0)
|
31
35
|
jwt (>= 1.0, < 3.0)
|
32
36
|
multi_json (~> 1.3)
|
33
37
|
multi_xml (~> 0.5)
|
34
38
|
rack (>= 1.2, < 3)
|
39
|
+
pry (0.13.1)
|
40
|
+
coderay (~> 1.1)
|
41
|
+
method_source (~> 1.0)
|
35
42
|
public_suffix (3.1.1)
|
36
|
-
rack (2.
|
37
|
-
rake (
|
43
|
+
rack (2.2.3)
|
44
|
+
rake (13.0.1)
|
38
45
|
safe_yaml (1.0.5)
|
39
46
|
thread_safe (0.3.6)
|
40
|
-
tzinfo (1.2.
|
47
|
+
tzinfo (1.2.7)
|
41
48
|
thread_safe (~> 0.1)
|
42
49
|
webmock (3.6.0)
|
43
50
|
addressable (>= 2.3.6)
|
44
51
|
crack (>= 0.3.2)
|
45
52
|
hashdiff (>= 0.4.0, < 2.0.0)
|
53
|
+
zeitwerk (2.3.0)
|
46
54
|
|
47
55
|
PLATFORMS
|
48
56
|
ruby
|
@@ -52,9 +60,9 @@ DEPENDENCIES
|
|
52
60
|
artemis_api!
|
53
61
|
bundler (~> 1.16)
|
54
62
|
minitest (~> 5.0)
|
55
|
-
|
56
|
-
rake (~>
|
63
|
+
pry
|
64
|
+
rake (~> 13.0)
|
57
65
|
webmock
|
58
66
|
|
59
67
|
BUNDLED WITH
|
60
|
-
1.17.
|
68
|
+
1.17.3
|
data/History.txt
CHANGED
@@ -1,5 +1,16 @@
|
|
1
1
|
# coding: UTF-8
|
2
2
|
|
3
|
+
=== 0.4.0 / 2019-11-14
|
4
|
+
|
5
|
+
* Added pagination functionality to Batch queries
|
6
|
+
* Cleaned up filtering and added tests
|
7
|
+
* Documentation updates
|
8
|
+
|
9
|
+
=== 0.3.0 / 2019-11-04
|
10
|
+
|
11
|
+
* Added an alternative OAuth flow that can accept an authorization code
|
12
|
+
directly and handle the creation of the OAuth client and token
|
13
|
+
|
3
14
|
=== 0.2.1 / 2019-10-28
|
4
15
|
|
5
16
|
* Minor updates to documentation
|
data/README.md
CHANGED
@@ -26,13 +26,15 @@ Or install it yourself as:
|
|
26
26
|
|
27
27
|
## Usage
|
28
28
|
|
29
|
+
#### Instantiating your Client
|
30
|
+
|
29
31
|
In order to use this gem, you will need to be set up as a developer in the Artemis Portal. If you're not, please contact Artemis CS in order to get that settled.
|
30
32
|
|
31
33
|
Once you have developer access, go to Settings and choose "OAuth 2.0 Applications" at the bottom of the sidebar to set up an application by entering the name and redirect URI you wish to use. You will then be provided with an application ID and a secret ID, which you will need in order to authenticate with Artemis.
|
32
34
|
|
33
|
-
|
35
|
+
The first step to actually using this gem is to instantiate an instance of `ArtemisApi::Client` and there are two different ways to do so depending on if you're handling OAuth in your own app or not.
|
34
36
|
|
35
|
-
|
37
|
+
If you intend to do a full OAuth flow, we recommend using the [OAuth2 gem](https://github.com/oauth-xx/oauth2). After authenticating with OAuth and generating your access token and refresh token, you can use them to instantiate your Client instance. You'll also need to pass in the `expires_at` for when your token will expire. That looks like this:
|
36
38
|
|
37
39
|
```ruby
|
38
40
|
options = {app_id: 'your_artemis_application_id',
|
@@ -45,7 +47,7 @@ client = ArtemisApi::Client.new(access_token: 'your_access_token',
|
|
45
47
|
options: options)
|
46
48
|
```
|
47
49
|
|
48
|
-
|
50
|
+
Instead of passing in options, you can set those values as ENV variables called `ENV['ARTEMIS_OAUTH_APP_ID']`, `ENV['ARTEMIS_OAUTH_APP_SECRET']` and `ENV['ARTEMIS_BASE_URI']`
|
49
51
|
|
50
52
|
They will be automatically detected and then you don't have to pass in any options:
|
51
53
|
```ruby
|
@@ -54,6 +56,20 @@ client = ArtemisApi::Client.new(access_token: 'your_access_token',
|
|
54
56
|
expires_at: token_expires_at)
|
55
57
|
```
|
56
58
|
|
59
|
+
Alternatively, if you're working on a command line tool or otherwise not intending to implement a full OAuth flow, you can generate an authorization code directly and use this code to obtain an access token and a refresh token.
|
60
|
+
Do this by creating an OAuth application in your Artemis settings with `urn:ietf:wg:oauth:2.0:oob` as the callback url. Then, clicking the Authorize button directly on Artemis Portal beside it on your application show page will
|
61
|
+
provide you directly with an authorization code. Pass it into the Client instantiator like this. (The same rules apply about either passing in options or setting your ENV variables.)
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
client = ArtemisApi::Client.new(auth_code: 'your_generated_authorization_code')
|
65
|
+
```
|
66
|
+
|
67
|
+
This authorization code is only valid for one use. If you want to keep connecting using the same authorization grant, you
|
68
|
+
need to save the values of `client.access_token`, `client.refresh_token` and `client.expires_at` and use them when
|
69
|
+
reinstantiating `client` in the future.
|
70
|
+
|
71
|
+
#### Requesting data from Artemis
|
72
|
+
|
57
73
|
Once you have a client instance, you can use it to request information from Artemis.
|
58
74
|
|
59
75
|
To get user information about the Artemis User that is associated with your application ID:
|
@@ -152,8 +168,10 @@ ArtemisApi::Batch.find(id: 22, facility_id: 2, client: client, include: "complet
|
|
152
168
|
We also support filtering on several models: Batch, Completion, Discard, Harvest, Zone, Item. It is another optional param and it expects a hash. Here's what that should look like.
|
153
169
|
|
154
170
|
```ruby
|
155
|
-
ArtemisApi::Batch.find_all(facility_id: 2, client: client, filters: {
|
171
|
+
ArtemisApi::Batch.find_all(facility_id: 2, client: client, filters: {search: 'genovese basil'})
|
156
172
|
ArtemisApi::Batch.find_all(facility_id: 2, client: client, filters: {ids: [2, 4, 6, 11]})
|
173
|
+
ArtemisApi::Batch.find_all(facility_id: 2, client: client, filters: {date_type: 'seeded_at', date_window: "2019-10-31 17:06:42 -0400,2019-11-15 17:06:42 -0500"})
|
174
|
+
|
157
175
|
ArtemisApi::Completion.find_all(facility_id: 2, client: client, filters: {crop_batch_ids: [5]})
|
158
176
|
ArtemisApi::Harvest.find_all(facility_id: 2, client: client, filters: {crop_batch_ids: [5, 7]})
|
159
177
|
ArtemisApi::Discard.find_all(facility_id: 2, client: client, filters: {crop_batch_ids: [6, 7, 9]})
|
@@ -163,6 +181,12 @@ ArtemisApi::Item.find_all(facility_id: 2, batch_id: 22, client: client, filters:
|
|
163
181
|
|
164
182
|
Note that when you filter by ids or crop_batch_ids, you must pass in an array even if it only has one element.
|
165
183
|
|
184
|
+
Pagination is also supported on batches, and is also an optional param that expects a hash. We use the limit/offset method of pagination.
|
185
|
+
|
186
|
+
```ruby
|
187
|
+
ArtemisApi::Batch.find_all(facility_id: 2, client: client, page: {limit: 10, offset: 60})
|
188
|
+
```
|
189
|
+
|
166
190
|
The Artemis API is currently mainly read only, but we do support the creation of Subscriptions. These are used to set up webhooks that will make a callback to you whenever a Completion or Batch gets created or updated in the given facility. They require a `subject`, which can currently be either `completions` or `batches`, and a `destination`, which is the url that you want the callback to hit. There are two ways to make that call:
|
167
191
|
|
168
192
|
```ruby
|
data/artemis_api.gemspec
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
|
2
1
|
lib = File.expand_path("../lib", __FILE__)
|
3
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
3
|
require "artemis_api/version"
|
@@ -6,10 +5,10 @@ require "artemis_api/version"
|
|
6
5
|
Gem::Specification.new do |spec|
|
7
6
|
spec.name = "artemis_api"
|
8
7
|
spec.version = ArtemisApi::VERSION
|
9
|
-
spec.authors = ["Jamey Hampton"]
|
10
|
-
spec.email = ["jhampton@artemisag.com"]
|
8
|
+
spec.authors = ["Jamey Hampton", "Carlos Betancourt Carrero"]
|
9
|
+
spec.email = ["jhampton@artemisag.com", "cbetancourt@artemisag.com"]
|
11
10
|
|
12
|
-
spec.summary =
|
11
|
+
spec.summary = "An API wrapper for the ArtemisAg API"
|
13
12
|
spec.homepage = "https://github.com/artemis-ag/artemis_api/"
|
14
13
|
spec.license = "MIT"
|
15
14
|
|
@@ -21,10 +20,12 @@ Gem::Specification.new do |spec|
|
|
21
20
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
21
|
spec.require_paths = ["lib"]
|
23
22
|
|
23
|
+
spec.add_dependency "oauth2"
|
24
|
+
|
25
|
+
spec.add_development_dependency "activesupport"
|
24
26
|
spec.add_development_dependency "bundler", "~> 1.16"
|
25
|
-
spec.add_development_dependency "rake", "~> 10.0"
|
26
27
|
spec.add_development_dependency "minitest", "~> 5.0"
|
27
|
-
spec.add_development_dependency "
|
28
|
+
spec.add_development_dependency "pry"
|
29
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
28
30
|
spec.add_development_dependency "webmock"
|
29
|
-
spec.add_development_dependency "activesupport"
|
30
31
|
end
|
data/bin/console
CHANGED
@@ -6,9 +6,5 @@ require "artemis_api"
|
|
6
6
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
# Pry.start
|
12
|
-
|
13
|
-
require "irb"
|
14
|
-
IRB.start(__FILE__)
|
9
|
+
require 'pry'
|
10
|
+
Pry.start
|
data/lib/artemis_api.rb
CHANGED
@@ -11,8 +11,15 @@ require 'artemis_api/discard'
|
|
11
11
|
require 'artemis_api/harvest'
|
12
12
|
require 'artemis_api/seeding_unit'
|
13
13
|
require 'artemis_api/harvest_unit'
|
14
|
+
require 'artemis_api/resource_unit'
|
15
|
+
require 'artemis_api/crop_variety'
|
14
16
|
require 'artemis_api/item'
|
15
17
|
require 'artemis_api/subscription'
|
18
|
+
require 'artemis_api/stage'
|
19
|
+
require 'artemis_api/sub_stage'
|
20
|
+
require 'artemis_api/crop_batch_state'
|
21
|
+
require 'artemis_api/custom_fields'
|
22
|
+
require 'artemis_api/custom_data'
|
16
23
|
|
17
24
|
module ArtemisApi
|
18
25
|
# Your code goes here...
|
data/lib/artemis_api/batch.rb
CHANGED
@@ -4,20 +4,22 @@ module ArtemisApi
|
|
4
4
|
related_to_one :zone
|
5
5
|
related_to_one :seeding_unit
|
6
6
|
related_to_one :harvest_unit
|
7
|
+
related_to_many :custom_data
|
8
|
+
related_to_many :custom_fields
|
7
9
|
|
8
10
|
def self.find(id:, client:, facility_id:, include: nil, force: false)
|
9
11
|
client.find_one(self.json_type, id, facility_id: facility_id, include: include, force: force)
|
10
12
|
end
|
11
13
|
|
12
|
-
def self.find_all(client:, facility_id:, include: nil, filters: nil)
|
13
|
-
client.find_all(self.json_type, facility_id: facility_id, include: include, filters: filters)
|
14
|
+
def self.find_all(client:, facility_id:, include: nil, filters: nil, page: nil)
|
15
|
+
client.find_all(self.json_type, facility_id: facility_id, include: include, filters: filters, page: page)
|
14
16
|
end
|
15
17
|
|
16
|
-
def completions(include: nil)
|
18
|
+
def completions(include: nil, filters: {})
|
17
19
|
ArtemisApi::Completion.find_all(facility_id: facility_id,
|
18
20
|
client: client,
|
19
21
|
include: include,
|
20
|
-
filters: {crop_batch_ids: [id]})
|
22
|
+
filters: {crop_batch_ids: [id]}.with_indifferent_access.merge(filters))
|
21
23
|
end
|
22
24
|
|
23
25
|
def completion(completion_id, include: nil)
|
data/lib/artemis_api/client.rb
CHANGED
@@ -1,31 +1,71 @@
|
|
1
1
|
module ArtemisApi
|
2
2
|
class Client
|
3
3
|
require 'oauth2'
|
4
|
-
attr_reader :options, :objects,
|
5
|
-
:
|
4
|
+
attr_reader :options, :objects,
|
5
|
+
:oauth_client, :oauth_token,
|
6
|
+
:access_token, :refresh_token, :expires_at,
|
7
|
+
:on_token_refreshed, :on_token_failed
|
8
|
+
|
9
|
+
def initialize(
|
10
|
+
access_token: nil,
|
11
|
+
refresh_token: nil,
|
12
|
+
expires_at: nil,
|
13
|
+
auth_code: nil,
|
14
|
+
redirect_uri: nil,
|
15
|
+
on_token_refreshed: nil,
|
16
|
+
on_token_failed: nil,
|
17
|
+
options: {}
|
18
|
+
)
|
19
|
+
unless (access_token && refresh_token && expires_at) || auth_code
|
20
|
+
raise ArgumentError.new('You must either provide your access token, refresh token & expires at time, or an authorization code.')
|
21
|
+
end
|
6
22
|
|
7
|
-
def initialize(access_token:, refresh_token:, expires_at:, options: {})
|
8
23
|
options[:app_id] ||= ENV['ARTEMIS_OAUTH_APP_ID']
|
9
24
|
options[:app_secret] ||= ENV['ARTEMIS_OAUTH_APP_SECRET']
|
10
25
|
options[:base_uri] ||= ENV['ARTEMIS_BASE_URI']
|
11
26
|
@options = options
|
12
|
-
@
|
13
|
-
|
14
|
-
@
|
27
|
+
@objects = {}
|
28
|
+
|
29
|
+
@on_token_refreshed = on_token_refreshed
|
30
|
+
@on_token_failed = on_token_failed
|
15
31
|
|
16
32
|
@oauth_client = OAuth2::Client.new(@options[:app_id], @options[:app_secret], site: @options[:base_uri])
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
33
|
+
|
34
|
+
if auth_code
|
35
|
+
redirect_uri ||= 'urn:ietf:wg:oauth:2.0:oob'
|
36
|
+
|
37
|
+
@oauth_token = @oauth_client.auth_code.get_token(auth_code, redirect_uri: redirect_uri)
|
38
|
+
|
39
|
+
@access_token = @oauth_token.token
|
40
|
+
@refresh_token = @oauth_token.refresh_token
|
41
|
+
@expires_at = @oauth_token.expires_at
|
42
|
+
else
|
43
|
+
set_oauth_token_from_parts(
|
44
|
+
access_token: access_token,
|
45
|
+
refresh_token: refresh_token,
|
46
|
+
expires_at: expires_at
|
47
|
+
)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def set_oauth_token_from_parts(access_token:, refresh_token:, expires_at:)
|
52
|
+
if access_token && refresh_token && expires_at
|
53
|
+
@access_token = access_token
|
54
|
+
@refresh_token = refresh_token
|
55
|
+
@expires_at = expires_at.to_i
|
56
|
+
|
57
|
+
@oauth_token = OAuth2::AccessToken.from_hash(
|
58
|
+
oauth_client,
|
59
|
+
{access_token: @access_token,
|
60
|
+
refresh_token: @refresh_token,
|
61
|
+
expires_at: @expires_at})
|
62
|
+
end
|
23
63
|
end
|
24
64
|
|
25
65
|
def find_one(type, id, facility_id: nil, include: nil, force: false)
|
26
66
|
obj = get_record(type, id)
|
27
67
|
if !obj || force
|
28
|
-
|
68
|
+
auto_refresh!
|
29
69
|
|
30
70
|
path = if facility_id
|
31
71
|
"/api/v3/facilities/#{facility_id}/#{type}/#{id}"
|
@@ -44,9 +84,9 @@ module ArtemisApi
|
|
44
84
|
obj
|
45
85
|
end
|
46
86
|
|
47
|
-
def find_all(type, facility_id: nil, batch_id: nil, include: nil, filters: nil)
|
87
|
+
def find_all(type, facility_id: nil, batch_id: nil, include: nil, filters: nil, page: nil)
|
48
88
|
records = []
|
49
|
-
|
89
|
+
auto_refresh!
|
50
90
|
|
51
91
|
path = if facility_id && batch_id
|
52
92
|
"/api/v3/facilities/#{facility_id}/batches/#{batch_id}/#{type}"
|
@@ -59,6 +99,7 @@ module ArtemisApi
|
|
59
99
|
query = {}
|
60
100
|
query[:include] = include if include
|
61
101
|
format_filters(filters, query) if filters
|
102
|
+
format_pagination(page, query) if page
|
62
103
|
|
63
104
|
url = build_url(path: path, query: URI.encode_www_form(query))
|
64
105
|
|
@@ -74,9 +115,9 @@ module ArtemisApi
|
|
74
115
|
@options[:base_uri] + uri.to_s
|
75
116
|
end
|
76
117
|
|
77
|
-
def store_record(type, id, data)
|
118
|
+
def store_record(type, id, data, included = nil)
|
78
119
|
@objects[type] ||= {}
|
79
|
-
@objects[type][id.to_i] = ArtemisApi::Model.instance_for(type, data, self)
|
120
|
+
@objects[type][id.to_i] = ArtemisApi::Model.instance_for(type, data, included, self)
|
80
121
|
end
|
81
122
|
|
82
123
|
def get_record(type, id)
|
@@ -84,19 +125,29 @@ module ArtemisApi
|
|
84
125
|
end
|
85
126
|
|
86
127
|
def remove_record(type, id)
|
87
|
-
@objects[type]
|
128
|
+
@objects[type]&.delete(id.to_i)
|
88
129
|
end
|
89
130
|
|
90
|
-
def
|
91
|
-
|
131
|
+
def auto_refresh!
|
132
|
+
refresh! if @oauth_token.expired?
|
92
133
|
end
|
93
134
|
|
94
|
-
def
|
95
|
-
@
|
96
|
-
end
|
135
|
+
def refresh!
|
136
|
+
old_refresh_token = @oauth_token.refresh_token
|
97
137
|
|
98
|
-
def refresh
|
99
138
|
@oauth_token = @oauth_token.refresh!
|
139
|
+
|
140
|
+
if old_refresh_token != @oauth_token.refresh_token
|
141
|
+
on_token_refreshed && on_token_refreshed.call(self, @oauth_token, old_refresh_token)
|
142
|
+
end
|
143
|
+
@oauth_token
|
144
|
+
rescue OAuth2::Error => err
|
145
|
+
if on_token_failed
|
146
|
+
on_token_failed.call(self, @oauth_token, err)
|
147
|
+
@oauth_token
|
148
|
+
else
|
149
|
+
raise err
|
150
|
+
end
|
100
151
|
end
|
101
152
|
|
102
153
|
def facilities(include: nil)
|
@@ -121,8 +172,8 @@ module ArtemisApi
|
|
121
172
|
|
122
173
|
def process_response(response, type)
|
123
174
|
json = JSON.parse(response.body)
|
124
|
-
|
125
|
-
|
175
|
+
included = process_included_objects(json['included']) if json['included']
|
176
|
+
obj = store_record(type, json['data']['id'].to_i, json['data'], included)
|
126
177
|
|
127
178
|
obj
|
128
179
|
end
|
@@ -135,25 +186,41 @@ module ArtemisApi
|
|
135
186
|
record = store_record(type, obj['id'], obj)
|
136
187
|
records << record
|
137
188
|
end
|
189
|
+
|
138
190
|
process_included_objects(json['included']) if json['included']
|
139
191
|
|
140
192
|
records
|
141
193
|
end
|
142
194
|
|
143
195
|
def process_included_objects(included_array)
|
196
|
+
included = {}
|
144
197
|
included_array.each do |included_obj|
|
145
|
-
|
198
|
+
type = included_obj['type']
|
199
|
+
obj = store_record(type, included_obj['id'], included_obj)
|
200
|
+
|
201
|
+
next unless obj
|
202
|
+
|
203
|
+
included[type.to_sym] = [] unless included.key?(type.to_sym)
|
204
|
+
included[type.to_sym].push(obj)
|
146
205
|
end
|
206
|
+
|
207
|
+
included
|
147
208
|
end
|
148
209
|
|
149
210
|
def format_filters(filter_hash, query_hash)
|
150
211
|
filter_hash.each do |k, v|
|
151
|
-
if v.
|
212
|
+
if v.is_a?(Array)
|
152
213
|
query_hash[:"filter[#{k}][]"] = v
|
153
214
|
else
|
154
215
|
query_hash[:"filter[#{k}]"] = v
|
155
216
|
end
|
156
217
|
end
|
157
218
|
end
|
219
|
+
|
220
|
+
def format_pagination(page_hash, query_hash)
|
221
|
+
page_hash.each do |k, v|
|
222
|
+
query_hash[:"page[#{k}]"] = v
|
223
|
+
end
|
224
|
+
end
|
158
225
|
end
|
159
226
|
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
module ArtemisApi
|
2
2
|
class Completion < ArtemisApi::Model
|
3
3
|
json_type 'completions'
|
4
|
+
related_to_one :crop_batch_state
|
4
5
|
|
5
6
|
def self.find(id:, facility_id:, client:, include: nil, force: false)
|
6
7
|
client.find_one(self.json_type, id, facility_id: facility_id, include: include, force: force)
|
data/lib/artemis_api/facility.rb
CHANGED
@@ -14,40 +14,48 @@ module ArtemisApi
|
|
14
14
|
ArtemisApi::Zone.find_all(facility_id: id, client: client, include: include)
|
15
15
|
end
|
16
16
|
|
17
|
-
def zone(zone_id, include: nil)
|
18
|
-
ArtemisApi::Zone.find(id: zone_id, facility_id: id, client: client, include: include)
|
17
|
+
def zone(zone_id, include: nil, force: false)
|
18
|
+
ArtemisApi::Zone.find(id: zone_id, facility_id: id, client: client, include: include, force: force)
|
19
19
|
end
|
20
20
|
|
21
21
|
def batches(include: nil)
|
22
22
|
ArtemisApi::Batch.find_all(facility_id: id, client: client, include: include)
|
23
23
|
end
|
24
24
|
|
25
|
-
def batch(batch_id, include: nil)
|
26
|
-
ArtemisApi::Batch.find(id: batch_id, facility_id: id, client: client, include: include)
|
25
|
+
def batch(batch_id, include: nil, force: false)
|
26
|
+
ArtemisApi::Batch.find(id: batch_id, facility_id: id, client: client, include: include, force: force)
|
27
27
|
end
|
28
28
|
|
29
29
|
def users(include: nil)
|
30
30
|
ArtemisApi::User.find_all(facility_id: id, client: client, include: include)
|
31
31
|
end
|
32
32
|
|
33
|
-
def user(user_id, include: nil)
|
34
|
-
ArtemisApi::User.find(id: user_id, facility_id: id, client: client, include: include)
|
33
|
+
def user(user_id, include: nil, force: false)
|
34
|
+
ArtemisApi::User.find(id: user_id, facility_id: id, client: client, include: include, force: force)
|
35
35
|
end
|
36
36
|
|
37
37
|
def seeding_units(include: nil)
|
38
38
|
ArtemisApi::SeedingUnit.find_all(facility_id: id, client: client, include: include)
|
39
39
|
end
|
40
40
|
|
41
|
-
def seeding_unit(unit_id, include: nil)
|
42
|
-
ArtemisApi::SeedingUnit.find(id: unit_id, facility_id: id, client: client, include: include)
|
41
|
+
def seeding_unit(unit_id, include: nil, force: false)
|
42
|
+
ArtemisApi::SeedingUnit.find(id: unit_id, facility_id: id, client: client, include: include, force: force)
|
43
|
+
end
|
44
|
+
|
45
|
+
def resource_units(include: nil)
|
46
|
+
ArtemisApi::ResourceUnit.find_all(facility_id: id, client: client, include: include)
|
47
|
+
end
|
48
|
+
|
49
|
+
def resource_unit(unit_id, include: nil, force: false)
|
50
|
+
ArtemisApi::ResourceUnit.find(id: unit_id, facility_id: id, client: client, include: include, force: force)
|
43
51
|
end
|
44
52
|
|
45
53
|
def harvest_units(include: nil)
|
46
54
|
ArtemisApi::HarvestUnit.find_all(facility_id: id, client: client, include: include)
|
47
55
|
end
|
48
56
|
|
49
|
-
def harvest_unit(unit_id, include: nil)
|
50
|
-
ArtemisApi::HarvestUnit.find(id: unit_id, facility_id: id, client: client, include: include)
|
57
|
+
def harvest_unit(unit_id, include: nil, force: false)
|
58
|
+
ArtemisApi::HarvestUnit.find(id: unit_id, facility_id: id, client: client, include: include, force: force)
|
51
59
|
end
|
52
60
|
|
53
61
|
def subscriptions
|
@@ -58,6 +66,14 @@ module ArtemisApi
|
|
58
66
|
ArtemisApi::Subscription.find(id: subscription_id, facility_id: id, client: client)
|
59
67
|
end
|
60
68
|
|
69
|
+
def stages
|
70
|
+
ArtemisApi::Stage.find_all(id, client)
|
71
|
+
end
|
72
|
+
|
73
|
+
def stage(stage_id)
|
74
|
+
ArtemisApi::Stage.find(stage_id, id, client)
|
75
|
+
end
|
76
|
+
|
61
77
|
def create_subscription(subject:, destination:)
|
62
78
|
ArtemisApi::Subscription.create(facility_id: id, subject: subject, destination: destination, client: client)
|
63
79
|
end
|
data/lib/artemis_api/model.rb
CHANGED
@@ -1,42 +1,85 @@
|
|
1
1
|
module ArtemisApi
|
2
2
|
class Model
|
3
|
-
attr_reader :client, :id, :attributes, :relationships
|
3
|
+
attr_reader :client, :id, :attributes, :relationships, :included
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
class << self
|
6
|
+
def related_to_one(name)
|
7
|
+
register_relationship(name)
|
8
|
+
|
9
|
+
send(:define_method, name.to_sym) do
|
10
|
+
related_id = relationships.dig(name.to_s, 'data', 'id')
|
11
|
+
included = client.get_record(name.to_s, related_id)
|
12
|
+
|
13
|
+
return included if included&.present?
|
14
|
+
|
15
|
+
relationship = relationships.dig(name.to_s, 'data')
|
16
|
+
return if relationship.nil?
|
17
|
+
|
18
|
+
@client.find_one(relationship['type'], relationship['id']) unless relationship['id'].to_s.empty? || relationship['id'].nil?
|
19
|
+
end
|
9
20
|
end
|
10
|
-
end
|
11
21
|
|
12
|
-
|
13
|
-
|
14
|
-
|
22
|
+
def related_to_many(name)
|
23
|
+
register_relationship(name)
|
24
|
+
|
25
|
+
send(:define_method, name.to_sym) do
|
26
|
+
included = relationships.dig(name.to_s, 'data')&.map do |related|
|
27
|
+
client.get_record(name.to_s, related['id'])
|
28
|
+
end
|
29
|
+
|
30
|
+
return included if included&.present?
|
31
|
+
|
32
|
+
@client.find_all(
|
33
|
+
relationships.dig(name.to_s, 'data', 0, 'type') || name.to_s,
|
34
|
+
filters: { name => id }
|
35
|
+
)
|
36
|
+
end
|
15
37
|
end
|
16
|
-
end
|
17
38
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
39
|
+
def json_type(type = nil)
|
40
|
+
if type
|
41
|
+
@json_type = type
|
42
|
+
@@registered_classes ||= {}
|
43
|
+
@@registered_classes[type] = self
|
44
|
+
end
|
45
|
+
@json_type
|
23
46
|
end
|
24
|
-
@json_type
|
25
|
-
end
|
26
47
|
|
27
|
-
|
28
|
-
|
48
|
+
def instance_for(type, data, included, client)
|
49
|
+
@@registered_classes[type]&.new(client, data, included)
|
50
|
+
end
|
51
|
+
|
52
|
+
def relationships
|
53
|
+
@relationships ||= []
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def register_relationship(name)
|
59
|
+
relationships << name.to_sym
|
60
|
+
end
|
29
61
|
end
|
30
62
|
|
31
63
|
def method_missing(name)
|
32
|
-
attributes[name.to_s]
|
64
|
+
respond_to_missing?(name) ? attributes[name.to_s] : super
|
65
|
+
end
|
66
|
+
|
67
|
+
def respond_to_missing?(name)
|
68
|
+
attributes.key?(name.to_s)
|
33
69
|
end
|
34
70
|
|
35
|
-
def initialize(client, data)
|
71
|
+
def initialize(client, data, included = nil)
|
36
72
|
@client = client
|
37
73
|
@id = data['id'].to_i
|
38
74
|
@attributes = data['attributes']
|
39
75
|
@relationships = data['relationships']
|
76
|
+
@included = included
|
77
|
+
end
|
78
|
+
|
79
|
+
def inspect
|
80
|
+
vars = %i[id attributes].map { |v| "#{v}=#{send(v).inspect}" }.join(', ')
|
81
|
+
vars << ", relationships={#{self.class.relationships.join(', ')}}"
|
82
|
+
"<#{self.class}: #{vars}>"
|
40
83
|
end
|
41
84
|
end
|
42
85
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module ArtemisApi
|
2
|
+
class ResourceUnit < ArtemisApi::Model
|
3
|
+
json_type 'resource_units'
|
4
|
+
related_to_one :crop_variety
|
5
|
+
|
6
|
+
def self.find(id:, facility_id:, client:, include: nil, force: false)
|
7
|
+
client.find_one(self.json_type, id, facility_id: facility_id, include: include, force: force)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.find_all(facility_id:, client:, include: nil)
|
11
|
+
client.find_all(self.json_type, facility_id: facility_id, include: include)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ArtemisApi
|
2
|
+
class Stage < ArtemisApi::Model
|
3
|
+
json_type 'stages'
|
4
|
+
related_to_many :sub_stages
|
5
|
+
related_to_many :zones
|
6
|
+
|
7
|
+
def self.find(id, facility_id, client, include: 'sub_stages,zones', force: false)
|
8
|
+
client.find_one(self.json_type, id, facility_id: facility_id, include: include, force: force)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.find_all(facility_id, client, include: 'sub_stages,zones')
|
12
|
+
client.find_all(self.json_type, facility_id: facility_id, include: include)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -6,12 +6,12 @@ module ArtemisApi
|
|
6
6
|
client.find_one(self.json_type, id, facility_id: facility_id, include: include, force: force)
|
7
7
|
end
|
8
8
|
|
9
|
-
def self.find_all(facility_id:, client:, include: nil
|
10
|
-
client.find_all(self.json_type, facility_id: facility_id, include: include
|
9
|
+
def self.find_all(facility_id:, client:, include: nil)
|
10
|
+
client.find_all(self.json_type, facility_id: facility_id, include: include)
|
11
11
|
end
|
12
12
|
|
13
13
|
def self.create(facility_id:, subject:, destination:, client:)
|
14
|
-
client.
|
14
|
+
client.auto_refresh!
|
15
15
|
|
16
16
|
url = "#{client.options[:base_uri]}/api/v3/facilities/#{facility_id}/subscriptions"
|
17
17
|
params = { body: { subscription: { subject: subject, destination: destination } } }
|
@@ -22,7 +22,7 @@ module ArtemisApi
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def self.delete(id:, facility_id:, client:)
|
25
|
-
client.
|
25
|
+
client.auto_refresh!
|
26
26
|
|
27
27
|
url = "#{client.options[:base_uri]}/api/v3/facilities/#{facility_id}/subscriptions/#{id}"
|
28
28
|
|
data/lib/artemis_api/user.rb
CHANGED
@@ -5,7 +5,7 @@ module ArtemisApi
|
|
5
5
|
|
6
6
|
def self.get_current(client:, include: nil)
|
7
7
|
self.json_type
|
8
|
-
client.
|
8
|
+
client.auto_refresh!
|
9
9
|
url = "#{client.options[:base_uri]}/api/v3/user"
|
10
10
|
url = "#{url}?include=#{include}" if include
|
11
11
|
response = client.oauth_token.get(url)
|
data/lib/artemis_api/version.rb
CHANGED
data/lib/artemis_api/zone.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
module ArtemisApi
|
2
2
|
class Zone < ArtemisApi::Model
|
3
3
|
json_type 'zones'
|
4
|
+
related_to_one 'sub_stage'
|
4
5
|
|
5
6
|
def self.find(id:, facility_id:, client:, include: nil, force: false)
|
6
7
|
client.find_one(self.json_type, id, facility_id: facility_id, include: include, force: force)
|
data/test/batch_test.rb
CHANGED
@@ -19,6 +19,15 @@ class BatchTest < Minitest::Test
|
|
19
19
|
relationships: {zone: {data: {id: 1, type: 'zones'}}}
|
20
20
|
},
|
21
21
|
included: [{id: '1', type: 'zones', attributes: {id: 1, name: 'Germination'}}]}.to_json)
|
22
|
+
|
23
|
+
stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/batches?filter[search]=oct")
|
24
|
+
.to_return(body: {data: [{id: '1', type: 'batches', attributes: {id: 1, arbitrary_id: 'Oct16-Gem-Let'}}, {id: '2', type: 'batches', attributes: {id: 2, name: 'Oct19-Bok-Cho'}}]}.to_json)
|
25
|
+
|
26
|
+
stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/batches?filter[ids][]=2&filter[ids][]=3")
|
27
|
+
.to_return(body: {data: [{id: '2', type: 'batches', attributes: {id: 2, name: 'Oct19-Bok-Cho'}}]}.to_json)
|
28
|
+
|
29
|
+
stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/batches?page[limit]=1&page[offset]=4")
|
30
|
+
.to_return(body: {data: [{id: '2', type: 'batches', attributes: {id: 2, name: 'Oct19-Bok-Cho'}}]}.to_json)
|
22
31
|
end
|
23
32
|
|
24
33
|
def test_finding_all_batches
|
@@ -55,4 +64,31 @@ class BatchTest < Minitest::Test
|
|
55
64
|
zone = batch.zone
|
56
65
|
assert_equal zone.name, 'Germination'
|
57
66
|
end
|
67
|
+
|
68
|
+
def test_filtering_by_search_term
|
69
|
+
batches = ArtemisApi::Batch.find_all(facility_id: @facility.id, client: @client, filters: {search: "oct"})
|
70
|
+
assert_equal 2, batches.count
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_filtering_by_ids
|
74
|
+
batches = ArtemisApi::Batch.find_all(facility_id: @facility.id, client: @client, filters: {ids: [2, 3]})
|
75
|
+
assert_equal 1, batches.count
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_filtering_by_date_window
|
79
|
+
ending = Time.now + 2.day
|
80
|
+
beginning = ending - 15.days
|
81
|
+
date_window = [beginning, ending].join(',')
|
82
|
+
|
83
|
+
stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/batches?filter[date_type]=seeded_at&filter[date_window]=#{CGI.escape(date_window)}")
|
84
|
+
.to_return(body: {data: [{id: 'd1', type: 'batches', attributes: {id: 1, arbitrary_id: 'Oct16-Gem-Let'}}, {id: '2', type: 'batches', attributes: {id: 2, name: 'Oct19-Bok-Cho'}}]}.to_json)
|
85
|
+
|
86
|
+
batches = ArtemisApi::Batch.find_all(facility_id: @facility.id, client: @client, filters: {date_type: "seeded_at", date_window: date_window})
|
87
|
+
assert_equal 2, batches.count
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_pagination
|
91
|
+
batches = ArtemisApi::Batch.find_all(facility_id: @facility.id, client: @client, page: {limit: 1, offset: 4})
|
92
|
+
assert_equal 1, batches.count
|
93
|
+
end
|
58
94
|
end
|
data/test/client_test.rb
CHANGED
@@ -15,6 +15,29 @@ class ClientTest < Minitest::Test
|
|
15
15
|
assert_equal false, @client.oauth_token.expired?
|
16
16
|
end
|
17
17
|
|
18
|
+
def test_creating_a_client_instance_with_auth_code
|
19
|
+
stub_request(:post, 'http://localhost:3000/oauth/token')
|
20
|
+
.with(body: {"client_id"=>"12345",
|
21
|
+
"client_secret"=>"67890",
|
22
|
+
"code"=>"aJ7xsj7",
|
23
|
+
"grant_type"=>"authorization_code",
|
24
|
+
"redirect_uri"=>"urn:ietf:wg:oauth:2.0:oob"})
|
25
|
+
.to_return(status: 200, body: {access_token: 'ya29', refresh_token: 'eyJh', expires_in: 7200, token_type: 'Bearer', created_at: Time.now.to_i}.to_json, headers: { 'Content-Type'=> 'application/json;charset=UTF-8'})
|
26
|
+
|
27
|
+
options = {app_id: '12345',
|
28
|
+
app_secret: '67890',
|
29
|
+
base_uri: 'http://localhost:3000'}
|
30
|
+
@client = ArtemisApi::Client.new(auth_code: 'aJ7xsj7', options: options)
|
31
|
+
|
32
|
+
assert_equal '12345', @client.oauth_client.id
|
33
|
+
assert_equal 'ya29', @client.oauth_token.token
|
34
|
+
assert_equal false, @client.oauth_token.expired?
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_creating_an_invalid_client_instance
|
38
|
+
assert_raises(ArgumentError) { ArtemisApi::Client.new(access_token: 'ya29') }
|
39
|
+
end
|
40
|
+
|
18
41
|
def test_current_user
|
19
42
|
get_client
|
20
43
|
stub_user
|
@@ -22,4 +45,22 @@ class ClientTest < Minitest::Test
|
|
22
45
|
user = @client.current_user
|
23
46
|
assert_equal user.class, ArtemisApi::User
|
24
47
|
end
|
48
|
+
|
49
|
+
def test_remove_record
|
50
|
+
get_client
|
51
|
+
|
52
|
+
type = 'subscriptions'
|
53
|
+
|
54
|
+
# model type added once, removed twice
|
55
|
+
@client.store_record(type, 1, 'id' => 1)
|
56
|
+
assert_equal 1, @client.get_record(type, 1).id
|
57
|
+
@client.remove_record(type, 1)
|
58
|
+
assert_nil @client.get_record(type, 1)
|
59
|
+
assert_nothing_raised { @client.remove_record(type, 1) }
|
60
|
+
|
61
|
+
# model type not added, attempt to remove one
|
62
|
+
undefined_type = 'something'
|
63
|
+
assert_nil @client.instance_variable_get(:"@objects")[undefined_type]
|
64
|
+
assert_nothing_raised { @client.remove_record(undefined_type, 1) }
|
65
|
+
end
|
25
66
|
end
|
data/test/completion_test.rb
CHANGED
@@ -9,7 +9,16 @@ class CompletionTest < Minitest::Test
|
|
9
9
|
.to_return(body: {data: [{id: '1', type: 'completions', attributes: {id: 1, action_type: 'start'}}, {id: '2', type: 'completions', attributes: {id: 2, action_type: 'move'}}]}.to_json)
|
10
10
|
|
11
11
|
stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/completions/2")
|
12
|
-
.to_return(body: {data: {id: '2', type: 'completions', attributes: {id: 2, action_type: 'move'}}}.to_json)
|
12
|
+
.to_return(body: {data: {id: '2', type: 'completions', attributes: {id: 2, action_type: 'move'}, relationships: { batch: { data: { id: 1, type: 'batches' }}}}}.to_json)
|
13
|
+
|
14
|
+
stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/completions/2")
|
15
|
+
.to_return(body: {data: {id: '2', type: 'completions', attributes: {id: 2, action_type: 'move'}, relationships: { batch: { data: { id: 1, type: 'batches' }}}}}.to_json)
|
16
|
+
|
17
|
+
stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/completions?filter[crop_batch_ids][]=2&filter[crop_batch_ids][]=3")
|
18
|
+
.to_return(body: {data: [{id: '1', type: 'completions', attributes: {id: 1, action_type: 'start'}}]}.to_json)
|
19
|
+
|
20
|
+
stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/completions?filter[parent_id]=2")
|
21
|
+
.to_return(body: {data: [{id: '3', type: 'completions', attributes: {id: 3, action_type: 'process'}}, {id: '4', type: 'completions', attributes: {id: 4, action_type: 'process'}}]}.to_json)
|
13
22
|
end
|
14
23
|
|
15
24
|
def test_finding_all_completions
|
@@ -21,4 +30,9 @@ class CompletionTest < Minitest::Test
|
|
21
30
|
completion = ArtemisApi::Completion.find(id: 2, facility_id: @facility.id, client: @client)
|
22
31
|
assert_equal 'move', completion.action_type
|
23
32
|
end
|
33
|
+
|
34
|
+
def test_filtering_completion_by_crop_batch_ids
|
35
|
+
completions = ArtemisApi::Completion.find_all(facility_id: @facility.id, client: @client, filters: {crop_batch_ids: [2, 3]})
|
36
|
+
assert_equal 1, completions.count
|
37
|
+
end
|
24
38
|
end
|
data/test/discard_test.rb
CHANGED
@@ -60,6 +60,9 @@ class DiscardTest < Minitest::Test
|
|
60
60
|
|
61
61
|
stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/discards")
|
62
62
|
.to_return(body: {data: [{id: '1', type: 'discards', attributes: {id: 1, reason_type: 'disease', quantity: 20}}, {id: '2', type: 'discards', attributes: {id: 2, reason_type: 'pests', quantity: 5}}]}.to_json)
|
63
|
+
|
64
|
+
stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/discards?filter[crop_batch_ids][]=2&filter[crop_batch_ids][]=3")
|
65
|
+
.to_return(body: {data: [{id: '1', type: 'discards', attributes: {id: 1, quantity: 4, reason_type: 'pests'}}]}.to_json)
|
63
66
|
end
|
64
67
|
|
65
68
|
def test_finding_a_specific_discard
|
@@ -82,4 +85,9 @@ class DiscardTest < Minitest::Test
|
|
82
85
|
discards = ArtemisApi::Discard.find_all(facility_id: @facility.id, client: @client)
|
83
86
|
assert_equal 2, discards.count
|
84
87
|
end
|
88
|
+
|
89
|
+
def test_filtering_discard_by_crop_batch_ids
|
90
|
+
discards = ArtemisApi::Discard.find_all(facility_id: @facility.id, client: @client, filters: {crop_batch_ids: [2, 3]})
|
91
|
+
assert_equal 1, discards.count
|
92
|
+
end
|
85
93
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class ResourceUnitTest < Minitest::Test
|
4
|
+
def setup
|
5
|
+
get_client
|
6
|
+
get_facility
|
7
|
+
|
8
|
+
stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/resource_units?include=crop_variety")
|
9
|
+
.to_return(body: {data: [{id: '1', type: 'resource_units', attributes: {id: 1, name: 'Bunch', kind: 'count', conversion_si: 1.0}}, {id: '2', type: 'resource_units', attributes: {id: 2, name: 'grams', kind: 'weight', conversion_si: 1.0}, relationships: { crop_variety: { data: { type: 'crop_varieties', id: 1 }}}}], included: [{ type: 'crop_varieties', id: 1, attributes: { name: '5th Element' } }]}.to_json)
|
10
|
+
|
11
|
+
stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/resource_units/2")
|
12
|
+
.to_return(body: {data: {id: '2', type: 'resource_units', attributes: {id: 2, name: 'grams', kind: 'weight', conversion_is: 1.0}}}.to_json)
|
13
|
+
|
14
|
+
stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/resource_units/3?include=crop_variety")
|
15
|
+
.to_return(body: {data: {id: '2', type: 'resource_units', attributes: {id: 3, name: 'grams', kind: 'weight', conversion_is: 1.0}, relationships: { crop_variety: { data: { type: 'crop_varieties', id: 2 } } }}, included: [{ type: 'crop_varieties', id: 2, attributes: { name: 'Boss Hog' } }]}.to_json)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_finding_all_resource_units
|
19
|
+
resource_units = ArtemisApi::ResourceUnit.find_all(facility_id: @facility.id, client: @client, include: 'crop_variety')
|
20
|
+
assert_equal 2, resource_units.count
|
21
|
+
assert_equal '5th Element', resource_units.last.crop_variety.name
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_finding_a_specific_resource_unit
|
25
|
+
resource_unit = ArtemisApi::ResourceUnit.find(id: 2, facility_id: @facility.id, client: @client)
|
26
|
+
assert_equal 'grams', resource_unit.name
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_resource_unit_has_crop_variety
|
30
|
+
resource_unit = ArtemisApi::ResourceUnit.find(id: 3, facility_id: @facility.id, client: @client, include: 'crop_variety')
|
31
|
+
assert_equal 'Boss Hog', resource_unit.crop_variety.name
|
32
|
+
end
|
33
|
+
end
|
data/test/stage_test.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class StageTest < Minitest::Test
|
4
|
+
def setup
|
5
|
+
get_client
|
6
|
+
get_facility
|
7
|
+
|
8
|
+
stub_request(:get, 'http://localhost:3000/api/v3/facilities/2')
|
9
|
+
.to_return(body: {data: {id: '2', type: 'facilities', attributes: {id: 2, name: 'Rare Dankness'}}}.to_json)
|
10
|
+
|
11
|
+
stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/stages?include=sub_stages,zones")
|
12
|
+
.to_return(body: {data: [{id: '1', type: 'stages', attributes: {id: 1, name: 'Growth', stage_type: 'growth'}}, {id: '2', type: 'stages', attributes: {id: 2, name: 'Stage 2', stage_type: 'stage_2'}}]}.to_json)
|
13
|
+
|
14
|
+
stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/stages/1?include=sub_stages,zones")
|
15
|
+
.to_return(body: {data: {id: '1', type: 'stages', attributes: {id: 1, name: 'Growth', stage_type: 'growth'}}}.to_json)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_finding_all_stages
|
19
|
+
stages = ArtemisApi::Stage.find_all(@facility.id, @client)
|
20
|
+
assert_equal 2, stages.count
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_finding_all_stages_through_facility
|
24
|
+
stages = @facility.stages
|
25
|
+
assert_equal 2, stages.count
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_finding_a_specific_stage
|
29
|
+
stage = ArtemisApi::Stage.find(1, @facility.id, @client)
|
30
|
+
assert_equal 'Growth', stage.name
|
31
|
+
assert_equal 'growth', stage.stage_type
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_finding_a_specific_stage_through_facility
|
35
|
+
stage = @facility.stage(1)
|
36
|
+
assert_equal 'Growth', stage.name
|
37
|
+
assert_equal 'growth', stage.stage_type
|
38
|
+
end
|
39
|
+
end
|
data/test/subscription_test.rb
CHANGED
@@ -53,11 +53,11 @@ class SubscriptionTest < Minitest::Test
|
|
53
53
|
|
54
54
|
def test_deleting_a_subscription
|
55
55
|
# find the subscription first to ensure it's in the objects hash
|
56
|
-
|
56
|
+
ArtemisApi::Subscription.find(id: 2, facility_id: @facility.id, client: @client)
|
57
57
|
assert_equal 1, @client.objects['subscriptions'].count
|
58
58
|
|
59
59
|
# then delete it and ensure it has been removed from the objects hash
|
60
|
-
|
60
|
+
ArtemisApi::Subscription.delete(id: 2, facility_id: @facility.id, client: @client)
|
61
61
|
assert_equal 0, @client.objects['subscriptions'].count
|
62
62
|
end
|
63
63
|
end
|
data/test/zone_test.rb
CHANGED
@@ -10,6 +10,9 @@ class ZoneTest < Minitest::Test
|
|
10
10
|
|
11
11
|
stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/zones/2")
|
12
12
|
.to_return(body: {data: {id: '2', type: 'zones', attributes: {id: 2, name: 'Propagation'}}}.to_json)
|
13
|
+
|
14
|
+
stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/zones?filter[seeding_unit_id]=100")
|
15
|
+
.to_return(body: {data: [{id: '2', type: 'zones', attributes: {id: 2, name: 'Propagation'}}]}.to_json)
|
13
16
|
end
|
14
17
|
|
15
18
|
def test_finding_all_zones
|
@@ -31,4 +34,9 @@ class ZoneTest < Minitest::Test
|
|
31
34
|
zone = @facility.zone(2)
|
32
35
|
assert_equal 'Propagation', zone.name
|
33
36
|
end
|
37
|
+
|
38
|
+
def test_filtering_zones_by_seeding_unit_id
|
39
|
+
zones = ArtemisApi::Zone.find_all(facility_id: @facility.id, client: @client, filters: {seeding_unit_id: 100})
|
40
|
+
assert_equal 1, zones.count
|
41
|
+
end
|
34
42
|
end
|
metadata
CHANGED
@@ -1,43 +1,58 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: artemis_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jamey Hampton
|
8
|
+
- Carlos Betancourt Carrero
|
8
9
|
autorequire:
|
9
10
|
bindir: exe
|
10
11
|
cert_chain: []
|
11
|
-
date:
|
12
|
+
date: 2020-11-23 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
15
|
+
name: oauth2
|
15
16
|
requirement: !ruby/object:Gem::Requirement
|
16
17
|
requirements:
|
17
|
-
- - "
|
18
|
+
- - ">="
|
18
19
|
- !ruby/object:Gem::Version
|
19
|
-
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: activesupport
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
20
35
|
type: :development
|
21
36
|
prerelease: false
|
22
37
|
version_requirements: !ruby/object:Gem::Requirement
|
23
38
|
requirements:
|
24
|
-
- - "
|
39
|
+
- - ">="
|
25
40
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
41
|
+
version: '0'
|
27
42
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
43
|
+
name: bundler
|
29
44
|
requirement: !ruby/object:Gem::Requirement
|
30
45
|
requirements:
|
31
46
|
- - "~>"
|
32
47
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
48
|
+
version: '1.16'
|
34
49
|
type: :development
|
35
50
|
prerelease: false
|
36
51
|
version_requirements: !ruby/object:Gem::Requirement
|
37
52
|
requirements:
|
38
53
|
- - "~>"
|
39
54
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
55
|
+
version: '1.16'
|
41
56
|
- !ruby/object:Gem::Dependency
|
42
57
|
name: minitest
|
43
58
|
requirement: !ruby/object:Gem::Requirement
|
@@ -53,7 +68,7 @@ dependencies:
|
|
53
68
|
- !ruby/object:Gem::Version
|
54
69
|
version: '5.0'
|
55
70
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
71
|
+
name: pry
|
57
72
|
requirement: !ruby/object:Gem::Requirement
|
58
73
|
requirements:
|
59
74
|
- - ">="
|
@@ -67,21 +82,21 @@ dependencies:
|
|
67
82
|
- !ruby/object:Gem::Version
|
68
83
|
version: '0'
|
69
84
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
85
|
+
name: rake
|
71
86
|
requirement: !ruby/object:Gem::Requirement
|
72
87
|
requirements:
|
73
|
-
- - "
|
88
|
+
- - "~>"
|
74
89
|
- !ruby/object:Gem::Version
|
75
|
-
version: '0'
|
90
|
+
version: '13.0'
|
76
91
|
type: :development
|
77
92
|
prerelease: false
|
78
93
|
version_requirements: !ruby/object:Gem::Requirement
|
79
94
|
requirements:
|
80
|
-
- - "
|
95
|
+
- - "~>"
|
81
96
|
- !ruby/object:Gem::Version
|
82
|
-
version: '0'
|
97
|
+
version: '13.0'
|
83
98
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
99
|
+
name: webmock
|
85
100
|
requirement: !ruby/object:Gem::Requirement
|
86
101
|
requirements:
|
87
102
|
- - ">="
|
@@ -97,11 +112,13 @@ dependencies:
|
|
97
112
|
description:
|
98
113
|
email:
|
99
114
|
- jhampton@artemisag.com
|
115
|
+
- cbetancourt@artemisag.com
|
100
116
|
executables: []
|
101
117
|
extensions: []
|
102
118
|
extra_rdoc_files: []
|
103
119
|
files:
|
104
120
|
- ".gitignore"
|
121
|
+
- ".ruby-version"
|
105
122
|
- ".travis.yml"
|
106
123
|
- CODE_OF_CONDUCT.md
|
107
124
|
- Gemfile
|
@@ -117,6 +134,10 @@ files:
|
|
117
134
|
- lib/artemis_api/batch.rb
|
118
135
|
- lib/artemis_api/client.rb
|
119
136
|
- lib/artemis_api/completion.rb
|
137
|
+
- lib/artemis_api/crop_batch_state.rb
|
138
|
+
- lib/artemis_api/crop_variety.rb
|
139
|
+
- lib/artemis_api/custom_data.rb
|
140
|
+
- lib/artemis_api/custom_fields.rb
|
120
141
|
- lib/artemis_api/discard.rb
|
121
142
|
- lib/artemis_api/facility.rb
|
122
143
|
- lib/artemis_api/harvest.rb
|
@@ -124,7 +145,10 @@ files:
|
|
124
145
|
- lib/artemis_api/item.rb
|
125
146
|
- lib/artemis_api/model.rb
|
126
147
|
- lib/artemis_api/organization.rb
|
148
|
+
- lib/artemis_api/resource_unit.rb
|
127
149
|
- lib/artemis_api/seeding_unit.rb
|
150
|
+
- lib/artemis_api/stage.rb
|
151
|
+
- lib/artemis_api/sub_stage.rb
|
128
152
|
- lib/artemis_api/subscription.rb
|
129
153
|
- lib/artemis_api/user.rb
|
130
154
|
- lib/artemis_api/version.rb
|
@@ -139,7 +163,9 @@ files:
|
|
139
163
|
- test/harvest_unit_test.rb
|
140
164
|
- test/item_test.rb
|
141
165
|
- test/organization_test.rb
|
166
|
+
- test/resource_unit_test.rb
|
142
167
|
- test/seeding_unit_test.rb
|
168
|
+
- test/stage_test.rb
|
143
169
|
- test/subscription_test.rb
|
144
170
|
- test/test_helper.rb
|
145
171
|
- test/user_test.rb
|
@@ -163,8 +189,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
163
189
|
- !ruby/object:Gem::Version
|
164
190
|
version: '0'
|
165
191
|
requirements: []
|
166
|
-
|
167
|
-
rubygems_version: 2.5.1
|
192
|
+
rubygems_version: 3.0.1
|
168
193
|
signing_key:
|
169
194
|
specification_version: 4
|
170
195
|
summary: An API wrapper for the ArtemisAg API
|