artemis_api 0.2.0 → 0.2.1

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
  SHA1:
3
- metadata.gz: 32db9ba97ad1d05952feab8c6bd9b4ee8de457ba
4
- data.tar.gz: 51c060027ec42f376727b5306458757050fefbec
3
+ metadata.gz: 77fb353bfa893d3ff210292444e20d71aa7dd5cf
4
+ data.tar.gz: 8aaa44cae9c5eec62ff7dfd524e5576ba7d79ce9
5
5
  SHA512:
6
- metadata.gz: 68232ded4b5b706fd7ddcc3be017a7d054063df9af3090447449833d5eb16177da445c863eeac10153cc7b98d168fdbb7c9a23877eaf73c4cee222f38c2a8039
7
- data.tar.gz: cc72bb823a05f33d8cbda6e231464aee7ed1f3555016c60952fcaeaeb9fc8c956e0d18b840a1b79fd057be6f0884002a71be9c61dc0391dc007c90de0a0f16ae
6
+ metadata.gz: ea1ad95a03f6f486bc66c629300f4726b912ce7213f54e7d26c2231a30a197688dac2dd93f08038b6d950b9e753f501e4b9f3225e8d1457900f63ad928393b10
7
+ data.tar.gz: 9d9b6ba166a362890551ae8c8b0d3f489d5b71c32e47f9851175f519ce216fec3702f703401a924673b4d97b32abd1698f816ff44f56b6d7b121e107d755b264
data/History.txt CHANGED
@@ -1,5 +1,12 @@
1
1
  # coding: UTF-8
2
2
 
3
+ === 0.2.1 / 2019-10-28
4
+
5
+ * Minor updates to documentation
6
+ * Fix errors with gem installation
7
+ * Don't try to delete a record if it doesn't exist
8
+ * Using URI module to generate API urls
9
+
3
10
  === 0.2.0 / 2019-10-24
4
11
 
5
12
  * Added the ability to delete Subscriptions
data/README.md CHANGED
@@ -6,6 +6,12 @@ This is a simple API wrapper for the [Artemis](https://artemisag.com/) API.
6
6
 
7
7
  Add this line to your application's Gemfile:
8
8
 
9
+ ```ruby
10
+ gem 'artemis_api'
11
+ ```
12
+
13
+ If you want to ensure the most up to date version, you can also use the gem straight from the repository:
14
+
9
15
  ```ruby
10
16
  gem 'artemis_api', :git => 'https://github.com/artemis-ag/artemis_api'
11
17
  ```
@@ -168,6 +174,12 @@ ArtemisApi::Subscription.create(facility_id: 2,
168
174
  facility.create_subscription(subject: 'completions', destination: 'https://test-app-url.artemisag.io/v1/webhook')
169
175
  ```
170
176
 
177
+ You can also delete one of your own Subscriptions. Trying to delete a Subscription that isn't associated with your user account will fail.
178
+
179
+ ```ruby
180
+ ArtemisApi::Subscription.delete(id: 1, facility_id: 2, client: client)
181
+ ```
182
+
171
183
  ## Development
172
184
 
173
185
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/artemis_api.gemspec CHANGED
@@ -15,6 +15,7 @@ Gem::Specification.new do |spec|
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
17
  f.match(%r{^(test|spec|features)/})
18
+ f.match(/\.gem$/)
18
19
  end
19
20
  spec.bindir = "exe"
20
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
@@ -27,12 +27,16 @@ module ArtemisApi
27
27
  if !obj || force
28
28
  refresh if @oauth_token.expired?
29
29
 
30
- url = if facility_id
31
- "#{@options[:base_uri]}/api/v3/facilities/#{facility_id}/#{type}/#{id}"
32
- else
33
- "#{@options[:base_uri]}/api/v3/#{type}/#{id}"
34
- end
35
- url = "#{url}?include=#{include}" if include
30
+ path = if facility_id
31
+ "/api/v3/facilities/#{facility_id}/#{type}/#{id}"
32
+ else
33
+ "/api/v3/#{type}/#{id}"
34
+ end
35
+
36
+ query = {}
37
+ query[:include] = include if include
38
+
39
+ url = build_url(path: path, query: URI.encode_www_form(query))
36
40
 
37
41
  response = @oauth_token.get(url)
38
42
  obj = process_response(response, type) if response.status == 200
@@ -44,19 +48,19 @@ module ArtemisApi
44
48
  records = []
45
49
  refresh if @oauth_token.expired?
46
50
 
47
- url = if facility_id && batch_id
48
- "#{@options[:base_uri]}/api/v3/facilities/#{facility_id}/batches/#{batch_id}/#{type}"
49
- elsif facility_id && batch_id.nil?
50
- "#{@options[:base_uri]}/api/v3/facilities/#{facility_id}/#{type}"
51
- else
52
- "#{@options[:base_uri]}/api/v3/#{type}"
53
- end
54
-
55
- url = "#{url}?include=#{include}" if include
56
- if filters
57
- formatted_filters = format_filters(filters)
58
- url = (include) ? "#{url}&#{formatted_filters}" : "#{url}?#{formatted_filters}"
59
- end
51
+ path = if facility_id && batch_id
52
+ "/api/v3/facilities/#{facility_id}/batches/#{batch_id}/#{type}"
53
+ elsif facility_id && batch_id.nil?
54
+ "/api/v3/facilities/#{facility_id}/#{type}"
55
+ else
56
+ "/api/v3/#{type}"
57
+ end
58
+
59
+ query = {}
60
+ query[:include] = include if include
61
+ format_filters(filters, query) if filters
62
+
63
+ url = build_url(path: path, query: URI.encode_www_form(query))
60
64
 
61
65
  response = @oauth_token.get(url)
62
66
  if response.status == 200
@@ -65,13 +69,26 @@ module ArtemisApi
65
69
  records
66
70
  end
67
71
 
72
+ def build_url(path:, query: nil)
73
+ uri = URI::Generic.build(path: path, query: query)
74
+ @options[:base_uri] + uri.to_s
75
+ end
76
+
68
77
  def store_record(type, id, data)
69
78
  @objects[type] ||= {}
70
79
  @objects[type][id.to_i] = ArtemisApi::Model.instance_for(type, data, self)
71
80
  end
72
81
 
73
82
  def get_record(type, id)
74
- @objects[type]&.[](id.to_i)
83
+ @objects.dig(type, id.to_i)
84
+ end
85
+
86
+ def remove_record(type, id)
87
+ @objects[type].delete(id.to_i) if record_stored?(type, id)
88
+ end
89
+
90
+ def record_stored?(type, id)
91
+ get_record(type, id).present?
75
92
  end
76
93
 
77
94
  def remove_record(type, id)
@@ -129,18 +146,14 @@ module ArtemisApi
129
146
  end
130
147
  end
131
148
 
132
- def format_filters(filter_hash)
133
- filter_string = ''
149
+ def format_filters(filter_hash, query_hash)
134
150
  filter_hash.each do |k, v|
135
151
  if v.kind_of?(Array)
136
- v.each do |item|
137
- filter_string += "filter[#{k}][]=#{item}&"
138
- end
152
+ query_hash[:"filter[#{k}][]"] = v
139
153
  else
140
- filter_string += "filter[#{k}]=#{v}&"
154
+ query_hash[:"filter[#{k}]"] = v
141
155
  end
142
156
  end
143
- filter_string
144
157
  end
145
158
  end
146
159
  end
@@ -1,3 +1,3 @@
1
1
  module ArtemisApi
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -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,58 @@
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
+ end
23
+
24
+ def test_finding_all_batches
25
+ batches = ArtemisApi::Batch.find_all(facility_id: @facility.id, client: @client)
26
+ assert_equal 2, batches.count
27
+ end
28
+
29
+ def test_finding_all_batches_through_facility
30
+ batches = @facility.batches
31
+ assert_equal 2, batches.count
32
+ end
33
+
34
+ def test_finding_a_specific_batch
35
+ batch = ArtemisApi::Batch.find(id: 2, facility_id: @facility.id, client: @client)
36
+ assert_equal 'Jun19-Bok-Cho', batch.arbitrary_id
37
+ end
38
+
39
+ def test_finding_a_specific_batch_through_facility
40
+ batch = @facility.batch(2)
41
+ assert_equal 'Jun19-Bok-Cho', batch.arbitrary_id
42
+ end
43
+
44
+ def test_finding_a_batch_with_zone_included
45
+ batch = ArtemisApi::Batch.find(id: 2, facility_id: @facility.id, client: @client, include: "zone")
46
+ assert_equal 'Jun19-Bok-Cho', batch.arbitrary_id
47
+ assert_equal @client.objects['zones'].count, 1
48
+
49
+ zone = ArtemisApi::Zone.find(id: 1, facility_id: @facility.id, client: @client)
50
+ assert_equal zone.name, 'Germination'
51
+ end
52
+
53
+ def test_related_to_one_zone
54
+ batch = ArtemisApi::Batch.find(id: 2, facility_id: @facility.id, client: @client, include: "zone")
55
+ zone = batch.zone
56
+ assert_equal zone.name, 'Germination'
57
+ end
58
+ end
@@ -0,0 +1,25 @@
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_current_user
19
+ get_client
20
+ stub_user
21
+
22
+ user = @client.current_user
23
+ assert_equal user.class, ArtemisApi::User
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ require "test_helper"
2
+
3
+ class CompletionTest < Minitest::Test
4
+ def setup
5
+ get_client
6
+ get_facility
7
+
8
+ stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/completions")
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
+
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)
13
+ end
14
+
15
+ def test_finding_all_completions
16
+ completions = ArtemisApi::Completion.find_all(facility_id: @facility.id, client: @client)
17
+ assert_equal 2, completions.count
18
+ end
19
+
20
+ def test_finding_a_specific_completion
21
+ completion = ArtemisApi::Completion.find(id: 2, facility_id: @facility.id, client: @client)
22
+ assert_equal 'move', completion.action_type
23
+ end
24
+ end
@@ -0,0 +1,85 @@
1
+ require "test_helper"
2
+
3
+ class DiscardTest < Minitest::Test
4
+ def setup
5
+ get_client
6
+ get_facility
7
+
8
+ stub_request(:get, "http://localhost:3000/api/v3/facilities/2/discards/2")
9
+ .to_return(body: {
10
+ data: {
11
+ id: '2',
12
+ type: 'discards',
13
+ attributes: {
14
+ id: 2,
15
+ quantity: 20,
16
+ reason_type: 'disease',
17
+ reason_description: ''
18
+ },
19
+ relationships: {
20
+ batch: {
21
+ meta: {
22
+ included: false
23
+ }
24
+ },
25
+ completion: {
26
+ meta: {
27
+ included: false
28
+ }
29
+ }
30
+ }
31
+ }
32
+ }.to_json)
33
+
34
+ stub_request(:get, "http://localhost:3000/api/v3/facilities/2/discards/2?include=batch")
35
+ .to_return(body: {
36
+ data: {
37
+ id: '2',
38
+ type: 'discards',
39
+ attributes: {
40
+ id: 2,
41
+ quantity: 20,
42
+ reason_type: 'disease',
43
+ reason_description: ''
44
+ },
45
+ relationships: {
46
+ batch: {
47
+ data: {
48
+ id: '156',
49
+ type: 'batches'
50
+ }
51
+ },
52
+ completion: {
53
+ meta: {
54
+ included: false
55
+ }
56
+ }
57
+ }
58
+ }
59
+ }.to_json)
60
+
61
+ stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/discards")
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
+ end
64
+
65
+ def test_finding_a_specific_discard
66
+ discard = ArtemisApi::Discard.find(id: 2, facility_id: @facility.id, client: @client)
67
+ assert_equal 20, discard.quantity
68
+ assert_equal 'disease', discard.reason_type
69
+ assert_equal false, discard.relationships['batch']['meta']['included']
70
+ assert discard.relationships['batch']['data'].nil?
71
+ end
72
+
73
+ def test_finding_a_specific_discard_with_batch_included
74
+ discard = ArtemisApi::Discard.find(id: 2, facility_id: @facility.id, client: @client, include: 'batch')
75
+ assert_equal 20, discard.quantity
76
+ assert_equal 'disease', discard.reason_type
77
+ assert_equal '156', discard.relationships['batch']['data']['id']
78
+ assert discard.relationships['batch']['meta'].nil?
79
+ end
80
+
81
+ def test_finding_all_discards
82
+ discards = ArtemisApi::Discard.find_all(facility_id: @facility.id, client: @client)
83
+ assert_equal 2, discards.count
84
+ end
85
+ end
@@ -0,0 +1,42 @@
1
+ require "test_helper"
2
+
3
+ class FacilityTest < Minitest::Test
4
+ def setup
5
+ get_client
6
+
7
+ stub_request(:get, 'http://localhost:3000/api/v3/facilities')
8
+ .to_return(body: {data: [{id: '1', type: 'facilities', attributes: {id: 1, name: 'Sky Fresh'}}, {id: '2', type: 'facilities', attributes: {id: 2, name: 'Rare Dankness'}}]}.to_json)
9
+ end
10
+
11
+ def test_finding_all_facilities
12
+ facilities = ArtemisApi::Facility.find_all(client: @client)
13
+ assert_equal 2, facilities.count
14
+ end
15
+
16
+ def test_finding_all_facilities_through_client
17
+ facilities = @client.facilities
18
+ assert_equal 2, facilities.count
19
+ end
20
+
21
+ def test_finding_a_specific_facility
22
+ stub_request(:get, 'http://localhost:3000/api/v3/facilities/2')
23
+ .to_return(body: {data: {id: '2', type: 'facilities', attributes: {id: 2, name: 'Rare Dankness'}}}.to_json)
24
+
25
+ facility = ArtemisApi::Facility.find(id: 2, client: @client)
26
+ assert_equal 'Rare Dankness', facility.name
27
+ end
28
+
29
+ def test_finding_a_specific_facility_through_client
30
+ stub_request(:get, 'http://localhost:3000/api/v3/facilities/2')
31
+ .to_return(body: {data: {id: '2', type: 'facilities', attributes: {id: 2, name: 'Rare Dankness'}}}.to_json)
32
+
33
+ facility = @client.facility(2)
34
+ assert_equal 'Rare Dankness', facility.name
35
+ end
36
+
37
+ def test_finding_a_specific_facility_thats_already_in_memory
38
+ ArtemisApi::Facility.find_all(client: @client)
39
+ facility = ArtemisApi::Facility.find(id: 2, client: @client)
40
+ assert_equal 'Rare Dankness', facility.name
41
+ end
42
+ end
@@ -0,0 +1,33 @@
1
+ require "test_helper"
2
+
3
+ class HarvestTest < Minitest::Test
4
+ def setup
5
+ get_client
6
+ get_facility
7
+
8
+ stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/harvests")
9
+ .to_return(body: {data: [{id: '1', type: 'harvests', attributes: {id: 1, quantity: 4, harvest_type: 'complete'}}, {id: '2', type: 'harvests', attributes: {id: 2, quantity: 1, harvest_type: 'partial'}}]}.to_json)
10
+
11
+ stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/harvests/2")
12
+ .to_return(body: {data: {id: '2', type: 'harvests', attributes: {id: 2, quantity: 1, harvest_type: 'partial'}}}.to_json)
13
+
14
+ stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/harvests?filter[crop_batch_ids][]=2&filter[crop_batch_ids][]=3")
15
+ .to_return(body: {data: [{id: '1', type: 'harvests', attributes: {id: 1, quantity: 4, harvest_type: 'complete'}}]}.to_json)
16
+ end
17
+
18
+ def test_finding_all_harvests
19
+ harvests = ArtemisApi::Harvest.find_all(facility_id: @facility.id, client: @client)
20
+ assert_equal 2, harvests.count
21
+ end
22
+
23
+ def test_finding_a_specific_harvest
24
+ harvest = ArtemisApi::Harvest.find(id: 2, facility_id: @facility.id, client: @client)
25
+ assert_equal 'partial', harvest.harvest_type
26
+ assert_equal 1, harvest.quantity
27
+ end
28
+
29
+ def test_filtering_harvest_by_crop_batch_ids
30
+ harvests = ArtemisApi::Harvest.find_all(facility_id: @facility.id, client: @client, filters: {crop_batch_ids: [2, 3]})
31
+ assert_equal 1, harvests.count
32
+ end
33
+ end
@@ -0,0 +1,25 @@
1
+ require "test_helper"
2
+
3
+ class HarvestUnitTest < Minitest::Test
4
+ def setup
5
+ get_client
6
+ get_facility
7
+
8
+ stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/harvest_units")
9
+ .to_return(body: {data: [{id: '1', type: 'harvest_units', attributes: {id: 1, name: 'pound', weight: 1}}, {id: '2', type: 'harvest_units', attributes: {id: 2, name: 'kilogram', weight: 2.2}}]}.to_json)
10
+
11
+ stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/harvest_units/2")
12
+ .to_return(body: {data: {id: '2', type: 'harvest_units', attributes: {id: 2, name: 'kilogram', weight: 2.2}}}.to_json)
13
+ end
14
+
15
+ def test_finding_all_harvest_units
16
+ harvest_units = ArtemisApi::HarvestUnit.find_all(facility_id: @facility.id, client: @client)
17
+ assert_equal 2, harvest_units.count
18
+ end
19
+
20
+ def test_finding_a_specific_harvest_unit
21
+ harvest_unit = ArtemisApi::HarvestUnit.find(id: 2, facility_id: @facility.id, client: @client)
22
+ assert_equal 'kilogram', harvest_unit.name
23
+ assert_equal 2.2, harvest_unit.weight
24
+ end
25
+ end
data/test/item_test.rb ADDED
@@ -0,0 +1,38 @@
1
+ require "test_helper"
2
+
3
+ class ItemTest < Minitest::Test
4
+ def setup
5
+ get_client
6
+
7
+ stub_request(:get, 'http://localhost:3000/api/v3/facilities/2')
8
+ .to_return(body: {data: {id: '2', type: 'facilities', attributes: {id: 2, name: 'Rare Dankness'}}}.to_json)
9
+ @facility = ArtemisApi::Facility.find(id: 2, client: @client)
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'}, relationships: {seeding_unit: {data: {id: '3479', type: 'seeding_units'}}}}}.to_json)
13
+ @batch = ArtemisApi::Batch.find(id: 2, facility_id: @facility.id, client: @client)
14
+
15
+ stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/batches/2/items")
16
+ .to_return(body: {data: [{id: '326515', type: 'items', attributes: {id: 326515, status: 'active'}, relationships: {barcode: {data: {id: '1A4FF0200000022000000207', type: 'barcodes'}}}}]}.to_json)
17
+
18
+ stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/batches/2/items?filter[seeding_unit_id]=100")
19
+ .to_return(body: {data: [{id: '326515', type: 'items', attributes: {id: 326515, status: 'active'}, relationships: {barcode: {data: {id: '1A4FF0200000022000000207', type: 'barcodes'}}, seeding_unit: {data: {id: '100', type: 'seeding_units'}}}}]}.to_json)
20
+ end
21
+
22
+ def test_finding_all_items_of_facility_and_batch
23
+ items = ArtemisApi::Item.find_all(facility_id: @facility.id, batch_id: @batch.id, client: @client)
24
+ assert_equal 1, items.count
25
+ assert_equal 326_515, items.first.id
26
+ assert_equal '1A4FF0200000022000000207', items.first.relationships.dig('barcode', 'data', 'id')
27
+ end
28
+
29
+ def test_finding_all_items_with_seeding_unit
30
+ seeding_unit_id = 100
31
+ items = ArtemisApi::Item.find_all(facility_id: @facility.id,
32
+ batch_id: @batch.id,
33
+ client: @client,
34
+ filters: {seeding_unit_id: seeding_unit_id})
35
+ assert_equal 1, items.count
36
+ assert_equal seeding_unit_id.to_s, items.first.relationships.dig('seeding_unit', 'data', 'id')
37
+ end
38
+ end
@@ -0,0 +1,39 @@
1
+ require "test_helper"
2
+
3
+ class OrganizationTest < Minitest::Test
4
+ def setup
5
+ get_client
6
+
7
+ stub_request(:get, 'http://localhost:3000/api/v3/organizations')
8
+ .to_return(body: {data: [{id: '1', type: 'organizations', attributes: {id: 1, name: 'Sky Fresh'}}, {id: '2', type: 'organizations', attributes: {id: 2, name: 'Rare Dankness'}}]}.to_json)
9
+ end
10
+
11
+ def test_finding_all_organizations
12
+ organizations = ArtemisApi::Organization.find_all(client: @client)
13
+ assert_equal 2, organizations.count
14
+ end
15
+
16
+ def test_finding_a_specific_organization
17
+ stub_request(:get, 'http://localhost:3000/api/v3/organizations/2')
18
+ .to_return(body: {data: {id: '2', type: 'organizations', attributes: {id: 2, name: 'Rare Dankness'}}}.to_json)
19
+
20
+ org = ArtemisApi::Organization.find(id: 2, client: @client)
21
+ assert_equal 'Rare Dankness', org.name
22
+ end
23
+
24
+ def test_finding_a_specific_org_thats_already_in_memory
25
+ ArtemisApi::Organization.find_all(client: @client)
26
+ org = ArtemisApi::Organization.find(id: 2, client: @client)
27
+ assert_equal 'Rare Dankness', org.name
28
+ end
29
+
30
+ def test_getting_orgs_with_facilities_included
31
+ stub_request(:get, 'http://localhost:3000/api/v3/organizations/2?include=facilities')
32
+ .to_return(body: {data: {id: '2', type: 'organizations', attributes: {id: 2, name: 'Rare Dankness'}}, included: [{id: 1, type: "facilities", attributes: {id: 1, name: 'Sky Fresh'}}]}.to_json)
33
+
34
+ org = ArtemisApi::Organization.find(id: 2, client: @client, include: 'facilities')
35
+ facility = ArtemisApi::Facility.find(id: 1, client: @client)
36
+ assert_equal 'Rare Dankness', org.name
37
+ assert_equal 'Sky Fresh', facility.name
38
+ end
39
+ end
@@ -0,0 +1,24 @@
1
+ require "test_helper"
2
+
3
+ class SeedingUnitTest < Minitest::Test
4
+ def setup
5
+ get_client
6
+ get_facility
7
+
8
+ stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/seeding_units")
9
+ .to_return(body: {data: [{id: '1', type: 'seeding_units', attributes: {id: 1, name: 'board'}}, {id: '2', type: 'seeding_units', attributes: {id: 2, name: 'raft'}}]}.to_json)
10
+
11
+ stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/seeding_units/2")
12
+ .to_return(body: {data: {id: '2', type: 'seeding_units', attributes: {id: 2, name: 'raft'}}}.to_json)
13
+ end
14
+
15
+ def test_finding_all_seeding_units
16
+ seeding_units = ArtemisApi::SeedingUnit.find_all(facility_id: @facility.id, client: @client)
17
+ assert_equal 2, seeding_units.count
18
+ end
19
+
20
+ def test_finding_a_specific_seeding_unit
21
+ seeding_unit = ArtemisApi::SeedingUnit.find(id: 2, facility_id: @facility.id, client: @client)
22
+ assert_equal 'raft', seeding_unit.name
23
+ end
24
+ end
@@ -0,0 +1,63 @@
1
+ require "test_helper"
2
+
3
+ class SubscriptionTest < Minitest::Test
4
+ def setup
5
+ get_client
6
+ get_facility
7
+
8
+ stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/subscriptions")
9
+ .to_return(body: {data: [{id: '1', type: 'subscriptions', attributes: {id: 1, subject: 'completions', destination: 'http://localhost:8080/a/fake/url'}}, {id: '2', type: 'subscriptions', attributes: {id: 2, subject: 'batches', destination: 'http://localhost:8080/another/fake/url'}}]}.to_json)
10
+
11
+ stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/subscriptions/2")
12
+ .to_return(body: {data: {id: '2', type: 'subscriptions', attributes: {id: 2, subject: 'batches', destination: 'http://localhost:8080/another/fake/url'}}}.to_json)
13
+
14
+ stub_request(:post, "http://localhost:3000/api/v3/facilities/#{@facility.id}/subscriptions")
15
+ .with(body: {"subscription"=>{"destination"=>"http://localhost:8080/a/fake/url", "subject"=>"completions"}})
16
+ .to_return(status: 200, body: {data: {id: '3', type: 'subscriptions', attributes: {id: 3, subject: 'completions', destination: 'http://localhost:8080/a/fake/url'}}}.to_json)
17
+
18
+ stub_request(:delete, "http://localhost:3000/api/v3/facilities/#{@facility.id}/subscriptions/2")
19
+ .to_return(status: 204, body: {}.to_json)
20
+ end
21
+
22
+ def test_finding_all_subscriptions
23
+ subscriptions = ArtemisApi::Subscription.find_all(facility_id: @facility.id, client: @client)
24
+ assert_equal 2, subscriptions.count
25
+ end
26
+
27
+ def test_finding_all_subscriptions_through_facility
28
+ subscriptions = @facility.subscriptions
29
+ assert_equal 2, subscriptions.count
30
+ end
31
+
32
+ def test_finding_a_specific_subscription
33
+ subscription = ArtemisApi::Subscription.find(id: 2, facility_id: @facility.id, client: @client)
34
+ assert_equal 'batches', subscription.subject
35
+ assert_equal 'http://localhost:8080/another/fake/url', subscription.destination
36
+ end
37
+
38
+ def test_finding_a_specific_subscription_through_facility
39
+ subscription = @facility.subscription(2)
40
+ assert_equal 'batches', subscription.subject
41
+ assert_equal 'http://localhost:8080/another/fake/url', subscription.destination
42
+ end
43
+
44
+ def test_creating_a_new_subscription
45
+ subscription = ArtemisApi::Subscription.create(facility_id: @facility.id,
46
+ subject: 'completions',
47
+ destination: 'http://localhost:8080/a/fake/url',
48
+ client: @client)
49
+ assert_equal 'completions', subscription.subject
50
+ assert_equal 'http://localhost:8080/a/fake/url', subscription.destination
51
+ assert_equal 1, @client.objects['subscriptions'].count
52
+ end
53
+
54
+ def test_deleting_a_subscription
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)
57
+ assert_equal 1, @client.objects['subscriptions'].count
58
+
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)
61
+ assert_equal 0, @client.objects['subscriptions'].count
62
+ end
63
+ end
@@ -0,0 +1,26 @@
1
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2
+ require "artemis_api"
3
+
4
+ require "minitest/autorun"
5
+ require "webmock/minitest"
6
+ require "active_support"
7
+ require "active_support/core_ext/numeric"
8
+ require 'active_support/testing/assertions'
9
+ include ActiveSupport::Testing::Assertions
10
+
11
+ def get_client
12
+ options = {app_id: '12345',
13
+ app_secret: '67890',
14
+ base_uri: 'http://localhost:3000'}
15
+ @expires_at = 1.days.from_now
16
+ @client = ArtemisApi::Client.new(access_token: 'ya29',
17
+ refresh_token: 'eyJh',
18
+ expires_at: @expires_at,
19
+ options: options)
20
+ end
21
+
22
+ def get_facility
23
+ stub_request(:get, 'http://localhost:3000/api/v3/facilities/2')
24
+ .to_return(body: {data: {id: '2', type: 'facilities', attributes: {id: 2, name: 'Rare Dankness'}}}.to_json)
25
+ @facility = ArtemisApi::Facility.find(id: 2, client: @client)
26
+ end
data/test/user_test.rb ADDED
@@ -0,0 +1,66 @@
1
+ require "test_helper"
2
+
3
+ class UserTest < Minitest::Test
4
+ def setup
5
+ get_client
6
+ get_facility
7
+
8
+ stub_request(:get, "http://localhost:3000/api/v3/user")
9
+ .to_return(body: {data: {id: '41', type: 'users', attributes: {id: 41, full_name: 'Jamey Hampton', email: 'jhampton@artemisag.com'}}}.to_json)
10
+
11
+ stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/users")
12
+ .to_return(body: {data: [{id: '41', type: 'users', attributes: {id: 41, full_name: 'Jamey Hampton', email: 'jhampton@artemisag.com'}}, {id: '42', type: 'users', attributes: {id: 42, full_name: 'Developer', email: 'developer@artemisag.com'}}]}.to_json)
13
+
14
+ stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/users/42")
15
+ .to_return(body: {data: {id: '42', type: 'users', attributes: {id: 42, full_name: 'Developer', email: 'developer@artemisag.com'}}}.to_json)
16
+ end
17
+
18
+ def test_getting_current_user
19
+ user = ArtemisApi::User.get_current(client: @client)
20
+ assert_equal user.id, 41
21
+ assert_equal user.full_name, 'Jamey Hampton'
22
+ assert_equal user.email, 'jhampton@artemisag.com'
23
+ end
24
+
25
+ def test_finding_all_users
26
+ users = ArtemisApi::User.find_all(facility_id: @facility.id, client: @client)
27
+ assert_equal 2, users.count
28
+ assert_equal 2, @client.objects['users'].count
29
+ end
30
+
31
+ def test_finding_all_users_through_facility
32
+ users = @facility.users
33
+ assert_equal 2, users.count
34
+ end
35
+
36
+ def test_finding_a_specific_user
37
+ user = ArtemisApi::User.find(id: 42, facility_id: @facility.id, client: @client)
38
+ assert_equal user.full_name, 'Developer'
39
+ assert_equal user.email, 'developer@artemisag.com'
40
+ end
41
+
42
+ def test_finding_a_specific_user_through_facility
43
+ user = @facility.user(42)
44
+ assert_equal user.full_name, 'Developer'
45
+ assert_equal user.email, 'developer@artemisag.com'
46
+ end
47
+
48
+ def test_finding_a_user_with_included_organizations
49
+ stub_request(:get, 'http://localhost:3000/api/v3/facilities/2/users/42?include=organizations')
50
+ .to_return(body: {data:
51
+ {id: '42',
52
+ type: 'users',
53
+ attributes: {id: 42, full_name: 'Developer', email: 'developer@artemisag.com'}},
54
+ included: [{id: '1', type: 'organizations', attributes: {id: 1, name: 'Vegetable Sky'}}]}.to_json)
55
+
56
+ user = ArtemisApi::User.find(id: 42, facility_id: @facility.id, client: @client, include: "organizations")
57
+ assert_equal user.full_name, 'Developer'
58
+ assert_equal user.email, 'developer@artemisag.com'
59
+ assert_equal @client.objects['organizations'].count, 1
60
+
61
+ # Since the organization has already been included and stored in the client objects array,
62
+ # this call doesn't actually hit the API and consdoesn't need to be stubbed.
63
+ organization = ArtemisApi::Organization.find(id: 1, client: @client)
64
+ assert_equal organization.name, 'Vegetable Sky'
65
+ end
66
+ end
data/test/zone_test.rb ADDED
@@ -0,0 +1,34 @@
1
+ require "test_helper"
2
+
3
+ class ZoneTest < Minitest::Test
4
+ def setup
5
+ get_client
6
+ get_facility
7
+
8
+ stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/zones")
9
+ .to_return(body: {data: [{id: '1', type: 'zones', attributes: {id: 1, name: 'Germination'}}, {id: '2', type: 'zones', attributes: {id: 2, name: 'Propagation'}}]}.to_json)
10
+
11
+ stub_request(:get, "http://localhost:3000/api/v3/facilities/#{@facility.id}/zones/2")
12
+ .to_return(body: {data: {id: '2', type: 'zones', attributes: {id: 2, name: 'Propagation'}}}.to_json)
13
+ end
14
+
15
+ def test_finding_all_zones
16
+ zones = ArtemisApi::Zone.find_all(facility_id: @facility.id, client: @client)
17
+ assert_equal 2, zones.count
18
+ end
19
+
20
+ def test_finding_all_zones_through_facility
21
+ zones = @facility.zones
22
+ assert_equal 2, zones.count
23
+ end
24
+
25
+ def test_finding_a_specific_zone
26
+ zone = ArtemisApi::Zone.find(id: 2, facility_id: @facility.id, client: @client)
27
+ assert_equal 'Propagation', zone.name
28
+ end
29
+
30
+ def test_finding_a_specific_zone_through_facility
31
+ zone = @facility.zone(2)
32
+ assert_equal 'Propagation', zone.name
33
+ end
34
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: artemis_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jamey Hampton
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-10-24 00:00:00.000000000 Z
11
+ date: 2019-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -110,7 +110,6 @@ files:
110
110
  - LICENSE.txt
111
111
  - README.md
112
112
  - Rakefile
113
- - artemis_api-0.1.0.gem
114
113
  - artemis_api.gemspec
115
114
  - bin/console
116
115
  - bin/setup
@@ -130,6 +129,21 @@ files:
130
129
  - lib/artemis_api/user.rb
131
130
  - lib/artemis_api/version.rb
132
131
  - lib/artemis_api/zone.rb
132
+ - test/artemis_api_test.rb
133
+ - test/batch_test.rb
134
+ - test/client_test.rb
135
+ - test/completion_test.rb
136
+ - test/discard_test.rb
137
+ - test/facility_test.rb
138
+ - test/harvest_test.rb
139
+ - test/harvest_unit_test.rb
140
+ - test/item_test.rb
141
+ - test/organization_test.rb
142
+ - test/seeding_unit_test.rb
143
+ - test/subscription_test.rb
144
+ - test/test_helper.rb
145
+ - test/user_test.rb
146
+ - test/zone_test.rb
133
147
  homepage: https://github.com/artemis-ag/artemis_api/
134
148
  licenses:
135
149
  - MIT
Binary file