artemis_api 0.2.0 → 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
- SHA1:
3
- metadata.gz: 32db9ba97ad1d05952feab8c6bd9b4ee8de457ba
4
- data.tar.gz: 51c060027ec42f376727b5306458757050fefbec
2
+ SHA256:
3
+ metadata.gz: '028bbc37f004d68e6513d33cd96c577933d05a0690de059cb737f26a19d8405c'
4
+ data.tar.gz: 5d403657733f1e76b2dd4f3416126f9f06484c402ec3fc544e33725d935200ea
5
5
  SHA512:
6
- metadata.gz: 68232ded4b5b706fd7ddcc3be017a7d054063df9af3090447449833d5eb16177da445c863eeac10153cc7b98d168fdbb7c9a23877eaf73c4cee222f38c2a8039
7
- data.tar.gz: cc72bb823a05f33d8cbda6e231464aee7ed1f3555016c60952fcaeaeb9fc8c956e0d18b840a1b79fd057be6f0884002a71be9c61dc0391dc007c90de0a0f16ae
6
+ metadata.gz: f6cb7bbf6da754d24ab0b9e5cbc3bd83391d4cdb13268a99c81c9c244ba2671dc82b7fa3bf62b91cf97c05a8b3a1f4cf5045be2854463f9e52587915e2c2f063
7
+ data.tar.gz: ba63182e17d76c7d90224a0d87a6073b8314203cbffc7422ecde68937eb11084fcd8cfb8d56058d71143c852d3d8cf6f34a0d6ac704144f4956bcaf1b002e8f8
@@ -0,0 +1 @@
1
+ 2.6.0
@@ -1,48 +1,56 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- artemis_api (0.1.0)
4
+ artemis_api (0.7.4)
5
+ oauth2
5
6
 
6
7
  GEM
7
8
  remote: https://rubygems.org/
8
9
  specs:
9
- activesupport (5.2.3)
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
- concurrent-ruby (1.1.5)
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.15.4)
22
+ faraday (1.0.1)
20
23
  multipart-post (>= 1.2, < 3)
21
24
  hashdiff (0.4.0)
22
- i18n (1.6.0)
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.13.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.1)
30
- faraday (>= 0.8, < 0.16.0)
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.0.7)
37
- rake (10.5.0)
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.5)
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
- oauth2
56
- rake (~> 10.0)
63
+ pry
64
+ rake (~> 13.0)
57
65
  webmock
58
66
 
59
67
  BUNDLED WITH
60
- 1.17.2
68
+ 1.17.3
@@ -1,5 +1,23 @@
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
+
14
+ === 0.2.1 / 2019-10-28
15
+
16
+ * Minor updates to documentation
17
+ * Fix errors with gem installation
18
+ * Don't try to delete a record if it doesn't exist
19
+ * Using URI module to generate API urls
20
+
3
21
  === 0.2.0 / 2019-10-24
4
22
 
5
23
  * 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
  ```
@@ -20,13 +26,15 @@ Or install it yourself as:
20
26
 
21
27
  ## Usage
22
28
 
29
+ #### Instantiating your Client
30
+
23
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.
24
32
 
25
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.
26
34
 
27
- (Please note that this gem doesn't currently handle OAuth. You will need to do that on your own in order to generate your access token and refresh token. We recommend using the [OAuth2 gem](https://github.com/oauth-xx/oauth2). You'll also need to pass in the `expires_at` for when your token will exipire.)
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.
28
36
 
29
- Once you have all this info, the first step to actually using this gem is to instantiate an instance of `ArtemisApi::Client` - which requires an access token, a refresh token,
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:
30
38
 
31
39
  ```ruby
32
40
  options = {app_id: 'your_artemis_application_id',
@@ -39,7 +47,7 @@ client = ArtemisApi::Client.new(access_token: 'your_access_token',
39
47
  options: options)
40
48
  ```
41
49
 
42
- Alternatively, 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']`
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']`
43
51
 
44
52
  They will be automatically detected and then you don't have to pass in any options:
45
53
  ```ruby
@@ -48,6 +56,20 @@ client = ArtemisApi::Client.new(access_token: 'your_access_token',
48
56
  expires_at: token_expires_at)
49
57
  ```
50
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
+
51
73
  Once you have a client instance, you can use it to request information from Artemis.
52
74
 
53
75
  To get user information about the Artemis User that is associated with your application ID:
@@ -146,8 +168,10 @@ ArtemisApi::Batch.find(id: 22, facility_id: 2, client: client, include: "complet
146
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.
147
169
 
148
170
  ```ruby
149
- ArtemisApi::Batch.find_all(facility_id: 2, client: client, filters: {view: 'all_batches', search: 'genovese basil'})
171
+ ArtemisApi::Batch.find_all(facility_id: 2, client: client, filters: {search: 'genovese basil'})
150
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
+
151
175
  ArtemisApi::Completion.find_all(facility_id: 2, client: client, filters: {crop_batch_ids: [5]})
152
176
  ArtemisApi::Harvest.find_all(facility_id: 2, client: client, filters: {crop_batch_ids: [5, 7]})
153
177
  ArtemisApi::Discard.find_all(facility_id: 2, client: client, filters: {crop_batch_ids: [6, 7, 9]})
@@ -157,6 +181,12 @@ ArtemisApi::Item.find_all(facility_id: 2, batch_id: 22, client: client, filters:
157
181
 
158
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.
159
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
+
160
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:
161
191
 
162
192
  ```ruby
@@ -168,6 +198,12 @@ ArtemisApi::Subscription.create(facility_id: 2,
168
198
  facility.create_subscription(subject: 'completions', destination: 'https://test-app-url.artemisag.io/v1/webhook')
169
199
  ```
170
200
 
201
+ You can also delete one of your own Subscriptions. Trying to delete a Subscription that isn't associated with your user account will fail.
202
+
203
+ ```ruby
204
+ ArtemisApi::Subscription.delete(id: 1, facility_id: 2, client: client)
205
+ ```
206
+
171
207
  ## Development
172
208
 
173
209
  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.
@@ -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,24 +5,27 @@ 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
 
16
15
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
16
  f.match(%r{^(test|spec|features)/})
17
+ f.match(/\.gem$/)
18
18
  end
19
19
  spec.bindir = "exe"
20
20
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
21
  spec.require_paths = ["lib"]
22
22
 
23
+ spec.add_dependency "oauth2"
24
+
25
+ spec.add_development_dependency "activesupport"
23
26
  spec.add_development_dependency "bundler", "~> 1.16"
24
- spec.add_development_dependency "rake", "~> 10.0"
25
27
  spec.add_development_dependency "minitest", "~> 5.0"
26
- spec.add_development_dependency "oauth2"
28
+ spec.add_development_dependency "pry"
29
+ spec.add_development_dependency "rake", "~> 13.0"
27
30
  spec.add_development_dependency "webmock"
28
- spec.add_development_dependency "activesupport"
29
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
@@ -11,8 +11,14 @@ 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/custom_fields'
21
+ require 'artemis_api/custom_data'
16
22
 
17
23
  module ArtemisApi
18
24
  # Your code goes here...
@@ -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)
@@ -1,38 +1,82 @@
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
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
- @access_token = access_token
13
- @refresh_token = refresh_token
14
- @expires_at = expires_at
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
- @oauth_token = OAuth2::AccessToken.from_hash(
18
- oauth_client,
19
- {access_token: @access_token,
20
- refresh_token: @refresh_token,
21
- expires_at: @expires_at})
22
- @objects = {}
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
- refresh if @oauth_token.expired?
68
+ auto_refresh!
69
+
70
+ path = if facility_id
71
+ "/api/v3/facilities/#{facility_id}/#{type}/#{id}"
72
+ else
73
+ "/api/v3/#{type}/#{id}"
74
+ end
29
75
 
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
76
+ query = {}
77
+ query[:include] = include if include
78
+
79
+ url = build_url(path: path, query: URI.encode_www_form(query))
36
80
 
37
81
  response = @oauth_token.get(url)
38
82
  obj = process_response(response, type) if response.status == 200
@@ -40,23 +84,24 @@ module ArtemisApi
40
84
  obj
41
85
  end
42
86
 
43
- 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)
44
88
  records = []
45
- refresh if @oauth_token.expired?
46
-
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
89
+ auto_refresh!
90
+
91
+ path = if facility_id && batch_id
92
+ "/api/v3/facilities/#{facility_id}/batches/#{batch_id}/#{type}"
93
+ elsif facility_id && batch_id.nil?
94
+ "/api/v3/facilities/#{facility_id}/#{type}"
95
+ else
96
+ "/api/v3/#{type}"
97
+ end
98
+
99
+ query = {}
100
+ query[:include] = include if include
101
+ format_filters(filters, query) if filters
102
+ format_pagination(page, query) if page
103
+
104
+ url = build_url(path: path, query: URI.encode_www_form(query))
60
105
 
61
106
  response = @oauth_token.get(url)
62
107
  if response.status == 200
@@ -65,21 +110,44 @@ module ArtemisApi
65
110
  records
66
111
  end
67
112
 
68
- def store_record(type, id, data)
113
+ def build_url(path:, query: nil)
114
+ uri = URI::Generic.build(path: path, query: query)
115
+ @options[:base_uri] + uri.to_s
116
+ end
117
+
118
+ def store_record(type, id, data, included = nil)
69
119
  @objects[type] ||= {}
70
- @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)
71
121
  end
72
122
 
73
123
  def get_record(type, id)
74
- @objects[type]&.[](id.to_i)
124
+ @objects.dig(type, id.to_i)
75
125
  end
76
126
 
77
127
  def remove_record(type, id)
78
- @objects[type].delete(id.to_i)
128
+ @objects[type]&.delete(id.to_i)
129
+ end
130
+
131
+ def auto_refresh!
132
+ refresh! if @oauth_token.expired?
79
133
  end
80
134
 
81
- def refresh
135
+ def refresh!
136
+ old_refresh_token = @oauth_token.refresh_token
137
+
82
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
83
151
  end
84
152
 
85
153
  def facilities(include: nil)
@@ -104,8 +172,8 @@ module ArtemisApi
104
172
 
105
173
  def process_response(response, type)
106
174
  json = JSON.parse(response.body)
107
- obj = store_record(type, json['data']['id'].to_i, json['data'])
108
- 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)
109
177
 
110
178
  obj
111
179
  end
@@ -118,29 +186,41 @@ module ArtemisApi
118
186
  record = store_record(type, obj['id'], obj)
119
187
  records << record
120
188
  end
189
+
121
190
  process_included_objects(json['included']) if json['included']
122
191
 
123
192
  records
124
193
  end
125
194
 
126
195
  def process_included_objects(included_array)
196
+ included = {}
127
197
  included_array.each do |included_obj|
128
- 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)
129
205
  end
206
+
207
+ included
130
208
  end
131
209
 
132
- def format_filters(filter_hash)
133
- filter_string = ''
210
+ def format_filters(filter_hash, query_hash)
134
211
  filter_hash.each do |k, v|
135
- if v.kind_of?(Array)
136
- v.each do |item|
137
- filter_string += "filter[#{k}][]=#{item}&"
138
- end
212
+ if v.is_a?(Array)
213
+ query_hash[:"filter[#{k}][]"] = v
139
214
  else
140
- filter_string += "filter[#{k}]=#{v}&"
215
+ query_hash[:"filter[#{k}]"] = v
141
216
  end
142
217
  end
143
- filter_string
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
144
224
  end
145
225
  end
146
226
  end