artemis_api 0.2.0 → 0.7.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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,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
@@ -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
@@ -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
@@ -0,0 +1,5 @@
1
+ module ArtemisApi
2
+ class SubStage < ArtemisApi::Model
3
+ json_type 'sub_stages'
4
+ end
5
+ 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, filters: nil)
10
- client.find_all(self.json_type, facility_id: facility_id, include: include, filters: filters)
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.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.2.0"
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)
@@ -0,0 +1,7 @@
1
+ require "test_helper"
2
+
3
+ class ArtemisApiTest < Minitest::Test
4
+ def test_that_it_has_a_version_number
5
+ refute_nil ::ArtemisApi::VERSION
6
+ end
7
+ end
@@ -0,0 +1,94 @@
1
+ require "test_helper"
2
+
3
+ class BatchTest < Minitest::Test
4
+ def setup
5
+ get_client
6
+ get_facility
7
+
8
+ stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/batches")
9
+ .to_return(body: {data: [{id: '1', type: 'batches', attributes: {id: 1, arbitrary_id: 'Jun16-Gem-Let'}}, {id: '2', type: 'batches', attributes: {id: 2, name: 'Jun19-Bok-Cho'}}]}.to_json)
10
+
11
+ stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/batches/2")
12
+ .to_return(body: {data: {id: '2', type: 'batches', attributes: {id: 2, arbitrary_id: 'Jun19-Bok-Cho'}}}.to_json)
13
+
14
+ stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/batches/2?include=zone")
15
+ .to_return(body: {data:
16
+ {id: '2',
17
+ type: 'batches',
18
+ attributes: {id: 2, arbitrary_id: 'Jun19-Bok-Cho'},
19
+ relationships: {zone: {data: {id: 1, type: 'zones'}}}
20
+ },
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)
31
+ end
32
+
33
+ def test_finding_all_batches
34
+ batches = ArtemisApi::Batch.find_all(facility_id: @facility.id, client: @client)
35
+ assert_equal 2, batches.count
36
+ end
37
+
38
+ def test_finding_all_batches_through_facility
39
+ batches = @facility.batches
40
+ assert_equal 2, batches.count
41
+ end
42
+
43
+ def test_finding_a_specific_batch
44
+ batch = ArtemisApi::Batch.find(id: 2, facility_id: @facility.id, client: @client)
45
+ assert_equal 'Jun19-Bok-Cho', batch.arbitrary_id
46
+ end
47
+
48
+ def test_finding_a_specific_batch_through_facility
49
+ batch = @facility.batch(2)
50
+ assert_equal 'Jun19-Bok-Cho', batch.arbitrary_id
51
+ end
52
+
53
+ def test_finding_a_batch_with_zone_included
54
+ batch = ArtemisApi::Batch.find(id: 2, facility_id: @facility.id, client: @client, include: "zone")
55
+ assert_equal 'Jun19-Bok-Cho', batch.arbitrary_id
56
+ assert_equal @client.objects['zones'].count, 1
57
+
58
+ zone = ArtemisApi::Zone.find(id: 1, facility_id: @facility.id, client: @client)
59
+ assert_equal zone.name, 'Germination'
60
+ end
61
+
62
+ def test_related_to_one_zone
63
+ batch = ArtemisApi::Batch.find(id: 2, facility_id: @facility.id, client: @client, include: "zone")
64
+ zone = batch.zone
65
+ assert_equal zone.name, 'Germination'
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
94
+ end
@@ -0,0 +1,66 @@
1
+ require "test_helper"
2
+
3
+ class ClientTest < Minitest::Test
4
+ def stub_user
5
+ stub_request(:get, "http://localhost:3000/api/v3/user")
6
+ .to_return(body: {data: {id: '41', type: 'users', attributes: {id: 41, full_name: 'Jamey Hampton', email: 'jhampton@artemisag.com'}}}.to_json)
7
+ end
8
+
9
+ def test_creating_a_client_instance
10
+ get_client
11
+
12
+ assert_equal '12345', @client.oauth_client.id
13
+ assert_equal 'ya29', @client.oauth_token.token
14
+ assert_equal @expires_at.to_i, @client.oauth_token.expires_at
15
+ assert_equal false, @client.oauth_token.expired?
16
+ end
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
+
41
+ def test_current_user
42
+ get_client
43
+ stub_user
44
+
45
+ user = @client.current_user
46
+ assert_equal user.class, ArtemisApi::User
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
66
+ end