artemis_api 0.5.1 → 0.7.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d26e2f48257f9de7ae7ce3d4b23ea245fdc7d3408a1467bd09b279275240523d
4
- data.tar.gz: 673cd71c200ec4f0bdf793b70df3e5075b16a0b37e1ad8f3d008ba3d6deed0a3
3
+ metadata.gz: '028bbc37f004d68e6513d33cd96c577933d05a0690de059cb737f26a19d8405c'
4
+ data.tar.gz: 5d403657733f1e76b2dd4f3416126f9f06484c402ec3fc544e33725d935200ea
5
5
  SHA512:
6
- metadata.gz: 914e649b5638e024bff44c404daf3c19c0fe4b02e17ec2829b9bbe2057c8e396f48c2a4923af5774cdaacd5473e3c85f16cb88414f6e5fa8ed9a543089d16598
7
- data.tar.gz: 78e811dc747f089a520f6cf39b4c8fe080a035fcb285e9b048538b614bbbce3a9099fd98763694f8946b7318896a6ade38408ab171da34a1323cbf2424f6c399
6
+ metadata.gz: f6cb7bbf6da754d24ab0b9e5cbc3bd83391d4cdb13268a99c81c9c244ba2671dc82b7fa3bf62b91cf97c05a8b3a1f4cf5045be2854463f9e52587915e2c2f063
7
+ data.tar.gz: ba63182e17d76c7d90224a0d87a6073b8314203cbffc7422ecde68937eb11084fcd8cfb8d56058d71143c852d3d8cf6f34a0d6ac704144f4956bcaf1b002e8f8
@@ -0,0 +1 @@
1
+ 2.6.0
@@ -1,28 +1,31 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- artemis_api (0.5.1)
4
+ artemis_api (0.7.4)
5
5
  oauth2
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- activesupport (5.2.3)
10
+ activesupport (6.0.3.1)
11
11
  concurrent-ruby (~> 1.0, >= 1.0.2)
12
12
  i18n (>= 0.7, < 2)
13
13
  minitest (~> 5.1)
14
14
  tzinfo (~> 1.1)
15
+ zeitwerk (~> 2.2, >= 2.2.2)
15
16
  addressable (2.6.0)
16
17
  public_suffix (>= 2.0.2, < 4.0)
17
- concurrent-ruby (1.1.5)
18
+ coderay (1.1.2)
19
+ concurrent-ruby (1.1.6)
18
20
  crack (0.4.3)
19
21
  safe_yaml (~> 1.0.0)
20
22
  faraday (1.0.1)
21
23
  multipart-post (>= 1.2, < 3)
22
24
  hashdiff (0.4.0)
23
- i18n (1.6.0)
25
+ i18n (1.8.2)
24
26
  concurrent-ruby (~> 1.0)
25
27
  jwt (2.2.1)
28
+ method_source (1.0.0)
26
29
  minitest (5.11.3)
27
30
  multi_json (1.14.1)
28
31
  multi_xml (0.6.0)
@@ -33,17 +36,21 @@ GEM
33
36
  multi_json (~> 1.3)
34
37
  multi_xml (~> 0.5)
35
38
  rack (>= 1.2, < 3)
39
+ pry (0.13.1)
40
+ coderay (~> 1.1)
41
+ method_source (~> 1.0)
36
42
  public_suffix (3.1.1)
37
- rack (2.2.2)
43
+ rack (2.2.3)
38
44
  rake (13.0.1)
39
45
  safe_yaml (1.0.5)
40
46
  thread_safe (0.3.6)
41
- tzinfo (1.2.5)
47
+ tzinfo (1.2.7)
42
48
  thread_safe (~> 0.1)
43
49
  webmock (3.6.0)
44
50
  addressable (>= 2.3.6)
45
51
  crack (>= 0.3.2)
46
52
  hashdiff (>= 0.4.0, < 2.0.0)
53
+ zeitwerk (2.3.0)
47
54
 
48
55
  PLATFORMS
49
56
  ruby
@@ -53,8 +60,9 @@ DEPENDENCIES
53
60
  artemis_api!
54
61
  bundler (~> 1.16)
55
62
  minitest (~> 5.0)
63
+ pry
56
64
  rake (~> 13.0)
57
65
  webmock
58
66
 
59
67
  BUNDLED WITH
60
- 1.17.2
68
+ 1.17.3
@@ -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 = %q{An API wrapper for the ArtemisAg API}
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
 
@@ -23,9 +22,10 @@ Gem::Specification.new do |spec|
23
22
 
24
23
  spec.add_dependency "oauth2"
25
24
 
25
+ spec.add_development_dependency "activesupport"
26
26
  spec.add_development_dependency "bundler", "~> 1.16"
27
- spec.add_development_dependency "rake", "~> 13.0"
28
27
  spec.add_development_dependency "minitest", "~> 5.0"
28
+ spec.add_development_dependency "pry"
29
+ spec.add_development_dependency "rake", "~> 13.0"
29
30
  spec.add_development_dependency "webmock"
30
- spec.add_development_dependency "activesupport"
31
31
  end
@@ -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
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start(__FILE__)
9
+ require 'pry'
10
+ Pry.start
@@ -12,8 +12,13 @@ require 'artemis_api/harvest'
12
12
  require 'artemis_api/seeding_unit'
13
13
  require 'artemis_api/harvest_unit'
14
14
  require 'artemis_api/resource_unit'
15
+ require 'artemis_api/crop_variety'
15
16
  require 'artemis_api/item'
16
17
  require 'artemis_api/subscription'
18
+ require 'artemis_api/stage'
19
+ require 'artemis_api/sub_stage'
20
+ require 'artemis_api/custom_fields'
21
+ require 'artemis_api/custom_data'
17
22
 
18
23
  module ArtemisApi
19
24
  # Your code goes here...
@@ -4,6 +4,8 @@ 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)
@@ -1,10 +1,21 @@
1
1
  module ArtemisApi
2
2
  class Client
3
3
  require 'oauth2'
4
- attr_reader :options, :objects, :access_token, :refresh_token, :oauth_client,
5
- :oauth_token, :expires_at
6
-
7
- def initialize(access_token: nil, refresh_token: nil, expires_at: nil, auth_code: nil, redirect_uri: nil, options: {})
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
+ )
8
19
  unless (access_token && refresh_token && expires_at) || auth_code
9
20
  raise ArgumentError.new('You must either provide your access token, refresh token & expires at time, or an authorization code.')
10
21
  end
@@ -15,8 +26,29 @@ module ArtemisApi
15
26
  @options = options
16
27
  @objects = {}
17
28
 
29
+ @on_token_refreshed = on_token_refreshed
30
+ @on_token_failed = on_token_failed
31
+
18
32
  @oauth_client = OAuth2::Client.new(@options[:app_id], @options[:app_secret], site: @options[:base_uri])
19
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:)
20
52
  if access_token && refresh_token && expires_at
21
53
  @access_token = access_token
22
54
  @refresh_token = refresh_token
@@ -25,23 +57,15 @@ module ArtemisApi
25
57
  @oauth_token = OAuth2::AccessToken.from_hash(
26
58
  oauth_client,
27
59
  {access_token: @access_token,
28
- refresh_token: @refresh_token,
29
- expires_at: @expires_at})
30
- elsif auth_code
31
- redirect_uri ||= 'urn:ietf:wg:oauth:2.0:oob'
32
-
33
- @oauth_token = @oauth_client.auth_code.get_token(auth_code, redirect_uri: redirect_uri)
34
-
35
- @access_token = @oauth_token.token
36
- @refresh_token = @oauth_token.refresh_token
37
- @expires_at = @oauth_token.expires_at
60
+ refresh_token: @refresh_token,
61
+ expires_at: @expires_at})
38
62
  end
39
63
  end
40
64
 
41
65
  def find_one(type, id, facility_id: nil, include: nil, force: false)
42
66
  obj = get_record(type, id)
43
67
  if !obj || force
44
- refresh if @oauth_token.expired?
68
+ auto_refresh!
45
69
 
46
70
  path = if facility_id
47
71
  "/api/v3/facilities/#{facility_id}/#{type}/#{id}"
@@ -62,7 +86,7 @@ module ArtemisApi
62
86
 
63
87
  def find_all(type, facility_id: nil, batch_id: nil, include: nil, filters: nil, page: nil)
64
88
  records = []
65
- refresh if @oauth_token.expired?
89
+ auto_refresh!
66
90
 
67
91
  path = if facility_id && batch_id
68
92
  "/api/v3/facilities/#{facility_id}/batches/#{batch_id}/#{type}"
@@ -91,9 +115,9 @@ module ArtemisApi
91
115
  @options[:base_uri] + uri.to_s
92
116
  end
93
117
 
94
- def store_record(type, id, data)
118
+ def store_record(type, id, data, included = nil)
95
119
  @objects[type] ||= {}
96
- @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)
97
121
  end
98
122
 
99
123
  def get_record(type, id)
@@ -104,8 +128,26 @@ module ArtemisApi
104
128
  @objects[type]&.delete(id.to_i)
105
129
  end
106
130
 
107
- def refresh
131
+ def auto_refresh!
132
+ refresh! if @oauth_token.expired?
133
+ end
134
+
135
+ def refresh!
136
+ old_refresh_token = @oauth_token.refresh_token
137
+
108
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
109
151
  end
110
152
 
111
153
  def facilities(include: nil)
@@ -130,8 +172,8 @@ module ArtemisApi
130
172
 
131
173
  def process_response(response, type)
132
174
  json = JSON.parse(response.body)
133
- obj = store_record(type, json['data']['id'].to_i, json['data'])
134
- process_included_objects(json['included']) if json['included']
175
+ included = process_included_objects(json['included']) if json['included']
176
+ obj = store_record(type, json['data']['id'].to_i, json['data'], included)
135
177
 
136
178
  obj
137
179
  end
@@ -144,20 +186,30 @@ module ArtemisApi
144
186
  record = store_record(type, obj['id'], obj)
145
187
  records << record
146
188
  end
189
+
147
190
  process_included_objects(json['included']) if json['included']
148
191
 
149
192
  records
150
193
  end
151
194
 
152
195
  def process_included_objects(included_array)
196
+ included = {}
153
197
  included_array.each do |included_obj|
154
- store_record(included_obj['type'], included_obj['id'], included_obj)
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)
155
205
  end
206
+
207
+ included
156
208
  end
157
209
 
158
210
  def format_filters(filter_hash, query_hash)
159
211
  filter_hash.each do |k, v|
160
- if v.kind_of?(Array)
212
+ if v.is_a?(Array)
161
213
  query_hash[:"filter[#{k}][]"] = v
162
214
  else
163
215
  query_hash[:"filter[#{k}]"] = v
@@ -0,0 +1,5 @@
1
+ module ArtemisApi
2
+ class CropVariety < ArtemisApi::Model
3
+ json_type 'crop_varieties'
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module ArtemisApi
2
+ class CustomData < ArtemisApi::Model
3
+ json_type 'custom_data'
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module ArtemisApi
2
+ class CustomFields < ArtemisApi::Model
3
+ json_type 'custom_fields'
4
+ end
5
+ end
@@ -14,48 +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
43
  end
44
44
 
45
45
  def resource_units(include: nil)
46
46
  ArtemisApi::ResourceUnit.find_all(facility_id: id, client: client, include: include)
47
47
  end
48
48
 
49
- def resource_unit(unit_id, include: nil)
50
- ArtemisApi::ResourceUnit.find(id: unit_id, facility_id: id, client: client, include: include)
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)
51
51
  end
52
52
 
53
53
  def harvest_units(include: nil)
54
54
  ArtemisApi::HarvestUnit.find_all(facility_id: id, client: client, include: include)
55
55
  end
56
56
 
57
- def harvest_unit(unit_id, include: nil)
58
- 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)
59
59
  end
60
60
 
61
61
  def subscriptions
@@ -66,6 +66,14 @@ module ArtemisApi
66
66
  ArtemisApi::Subscription.find(id: subscription_id, facility_id: id, client: client)
67
67
  end
68
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
+
69
77
  def create_subscription(subject:, destination:)
70
78
  ArtemisApi::Subscription.create(facility_id: id, subject: subject, destination: destination, client: client)
71
79
  end
@@ -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
- def self.related_to_one(name)
6
- self.send(:define_method, name.to_sym) do
7
- relationship = relationships[name.to_s]['data']
8
- @client.find_one(relationship['type'], relationship['id'])
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
- def self.related_to_many(name)
13
- self.send(:define_method, name.to_sym) do
14
- @client.find_all(self.relationships[name.to_s]['data']['type'])
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
- def self.json_type(type = nil)
19
- if type
20
- @json_type = type
21
- @@registered_classes ||= {}
22
- @@registered_classes[type] = self
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
- def self.instance_for(type, data, client)
28
- @@registered_classes[type]&.new(client, data)
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
@@ -1,6 +1,7 @@
1
1
  module ArtemisApi
2
2
  class ResourceUnit < ArtemisApi::Model
3
3
  json_type 'resource_units'
4
+ related_to_one :crop_variety
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)
@@ -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
@@ -0,0 +1,5 @@
1
+ module ArtemisApi
2
+ class SubStage < ArtemisApi::Model
3
+ json_type 'sub_stages'
4
+ end
5
+ end
@@ -11,7 +11,7 @@ module ArtemisApi
11
11
  end
12
12
 
13
13
  def self.create(facility_id:, subject:, destination:, client:)
14
- client.oauth_token.refresh! if client.oauth_token.expired?
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.oauth_token.refresh! if client.oauth_token.expired?
25
+ client.auto_refresh!
26
26
 
27
27
  url = "#{client.options[:base_uri]}/api/v3/facilities/#{facility_id}/subscriptions/#{id}"
28
28
 
@@ -5,7 +5,7 @@ module ArtemisApi
5
5
 
6
6
  def self.get_current(client:, include: nil)
7
7
  self.json_type
8
- client.refresh if client.oauth_token.expired?
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)
@@ -1,3 +1,3 @@
1
1
  module ArtemisApi
2
- VERSION = "0.5.1"
2
+ VERSION = '0.7.4'.freeze
3
3
  end
@@ -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)
@@ -5,20 +5,29 @@ class ResourceUnitTest < Minitest::Test
5
5
  get_client
6
6
  get_facility
7
7
 
8
- stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/resource_units")
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}}]}.to_json)
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
10
 
11
11
  stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/resource_units/2")
12
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)
13
16
  end
14
17
 
15
18
  def test_finding_all_resource_units
16
- resource_units = ArtemisApi::ResourceUnit.find_all(facility_id: @facility.id, client: @client)
19
+ resource_units = ArtemisApi::ResourceUnit.find_all(facility_id: @facility.id, client: @client, include: 'crop_variety')
17
20
  assert_equal 2, resource_units.count
21
+ assert_equal '5th Element', resource_units.last.crop_variety.name
18
22
  end
19
23
 
20
24
  def test_finding_a_specific_resource_unit
21
25
  resource_unit = ArtemisApi::ResourceUnit.find(id: 2, facility_id: @facility.id, client: @client)
22
26
  assert_equal 'grams', resource_unit.name
23
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
24
33
  end
@@ -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
@@ -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
- subscription = ArtemisApi::Subscription.find(id: 2, facility_id: @facility.id, client: @client)
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
- result = ArtemisApi::Subscription.delete(id: 2, facility_id: @facility.id, client: @client)
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
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: artemis_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.7.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jamey Hampton
8
- autorequire:
8
+ - Carlos Betancourt Carrero
9
+ autorequire:
9
10
  bindir: exe
10
11
  cert_chain: []
11
- date: 2020-04-02 00:00:00.000000000 Z
12
+ date: 2020-08-14 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: oauth2
@@ -25,33 +26,33 @@ dependencies:
25
26
  - !ruby/object:Gem::Version
26
27
  version: '0'
27
28
  - !ruby/object:Gem::Dependency
28
- name: bundler
29
+ name: activesupport
29
30
  requirement: !ruby/object:Gem::Requirement
30
31
  requirements:
31
- - - "~>"
32
+ - - ">="
32
33
  - !ruby/object:Gem::Version
33
- version: '1.16'
34
+ version: '0'
34
35
  type: :development
35
36
  prerelease: false
36
37
  version_requirements: !ruby/object:Gem::Requirement
37
38
  requirements:
38
- - - "~>"
39
+ - - ">="
39
40
  - !ruby/object:Gem::Version
40
- version: '1.16'
41
+ version: '0'
41
42
  - !ruby/object:Gem::Dependency
42
- name: rake
43
+ name: bundler
43
44
  requirement: !ruby/object:Gem::Requirement
44
45
  requirements:
45
46
  - - "~>"
46
47
  - !ruby/object:Gem::Version
47
- version: '13.0'
48
+ version: '1.16'
48
49
  type: :development
49
50
  prerelease: false
50
51
  version_requirements: !ruby/object:Gem::Requirement
51
52
  requirements:
52
53
  - - "~>"
53
54
  - !ruby/object:Gem::Version
54
- version: '13.0'
55
+ version: '1.16'
55
56
  - !ruby/object:Gem::Dependency
56
57
  name: minitest
57
58
  requirement: !ruby/object:Gem::Requirement
@@ -67,7 +68,7 @@ dependencies:
67
68
  - !ruby/object:Gem::Version
68
69
  version: '5.0'
69
70
  - !ruby/object:Gem::Dependency
70
- name: webmock
71
+ name: pry
71
72
  requirement: !ruby/object:Gem::Requirement
72
73
  requirements:
73
74
  - - ">="
@@ -81,7 +82,21 @@ dependencies:
81
82
  - !ruby/object:Gem::Version
82
83
  version: '0'
83
84
  - !ruby/object:Gem::Dependency
84
- name: activesupport
85
+ name: rake
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '13.0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '13.0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: webmock
85
100
  requirement: !ruby/object:Gem::Requirement
86
101
  requirements:
87
102
  - - ">="
@@ -94,14 +109,16 @@ dependencies:
94
109
  - - ">="
95
110
  - !ruby/object:Gem::Version
96
111
  version: '0'
97
- description:
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,9 @@ 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_variety.rb
138
+ - lib/artemis_api/custom_data.rb
139
+ - lib/artemis_api/custom_fields.rb
120
140
  - lib/artemis_api/discard.rb
121
141
  - lib/artemis_api/facility.rb
122
142
  - lib/artemis_api/harvest.rb
@@ -126,6 +146,8 @@ files:
126
146
  - lib/artemis_api/organization.rb
127
147
  - lib/artemis_api/resource_unit.rb
128
148
  - lib/artemis_api/seeding_unit.rb
149
+ - lib/artemis_api/stage.rb
150
+ - lib/artemis_api/sub_stage.rb
129
151
  - lib/artemis_api/subscription.rb
130
152
  - lib/artemis_api/user.rb
131
153
  - lib/artemis_api/version.rb
@@ -142,6 +164,7 @@ files:
142
164
  - test/organization_test.rb
143
165
  - test/resource_unit_test.rb
144
166
  - test/seeding_unit_test.rb
167
+ - test/stage_test.rb
145
168
  - test/subscription_test.rb
146
169
  - test/test_helper.rb
147
170
  - test/user_test.rb
@@ -150,7 +173,7 @@ homepage: https://github.com/artemis-ag/artemis_api/
150
173
  licenses:
151
174
  - MIT
152
175
  metadata: {}
153
- post_install_message:
176
+ post_install_message:
154
177
  rdoc_options: []
155
178
  require_paths:
156
179
  - lib
@@ -165,8 +188,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
165
188
  - !ruby/object:Gem::Version
166
189
  version: '0'
167
190
  requirements: []
168
- rubygems_version: 3.0.3
169
- signing_key:
191
+ rubygems_version: 3.0.8
192
+ signing_key:
170
193
  specification_version: 4
171
194
  summary: An API wrapper for the ArtemisAg API
172
195
  test_files: []