artemis_api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4ebdb821d7dca5b2888ded8eeef552f22486a866
4
+ data.tar.gz: f18fbf29bed0e7b67fda21717711edf85f253b53
5
+ SHA512:
6
+ metadata.gz: 4d7baf2ba2584ccd7ce8f53f84fd5e0611b5d31fa70eea2a1f8207c6bf8b614a8325cd6e39bc50530ae2a6eacc207f1f3edb92f8112a6024b99a572d750e7761
7
+ data.tar.gz: b7f58a2902751e5e86319e489fb6aece341e139c02cb441252a40b0181d37dc86d2eaf5e92c5e0c0de039a626dc8e3fad82b31e05ad909767e88af73ae98b8fa
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.16.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at jhampton@agrilyst.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in artemis_api.gemspec
6
+ gemspec
@@ -0,0 +1,60 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ artemis_api (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activesupport (5.2.3)
10
+ concurrent-ruby (~> 1.0, >= 1.0.2)
11
+ i18n (>= 0.7, < 2)
12
+ minitest (~> 5.1)
13
+ tzinfo (~> 1.1)
14
+ addressable (2.6.0)
15
+ public_suffix (>= 2.0.2, < 4.0)
16
+ concurrent-ruby (1.1.5)
17
+ crack (0.4.3)
18
+ safe_yaml (~> 1.0.0)
19
+ faraday (0.15.4)
20
+ multipart-post (>= 1.2, < 3)
21
+ hashdiff (0.4.0)
22
+ i18n (1.6.0)
23
+ concurrent-ruby (~> 1.0)
24
+ jwt (2.2.1)
25
+ minitest (5.11.3)
26
+ multi_json (1.13.1)
27
+ multi_xml (0.6.0)
28
+ multipart-post (2.1.1)
29
+ oauth2 (1.4.1)
30
+ faraday (>= 0.8, < 0.16.0)
31
+ jwt (>= 1.0, < 3.0)
32
+ multi_json (~> 1.3)
33
+ multi_xml (~> 0.5)
34
+ rack (>= 1.2, < 3)
35
+ public_suffix (3.1.1)
36
+ rack (2.0.7)
37
+ rake (10.5.0)
38
+ safe_yaml (1.0.5)
39
+ thread_safe (0.3.6)
40
+ tzinfo (1.2.5)
41
+ thread_safe (~> 0.1)
42
+ webmock (3.6.0)
43
+ addressable (>= 2.3.6)
44
+ crack (>= 0.3.2)
45
+ hashdiff (>= 0.4.0, < 2.0.0)
46
+
47
+ PLATFORMS
48
+ ruby
49
+
50
+ DEPENDENCIES
51
+ activesupport
52
+ artemis_api!
53
+ bundler (~> 1.16)
54
+ minitest (~> 5.0)
55
+ oauth2
56
+ rake (~> 10.0)
57
+ webmock
58
+
59
+ BUNDLED WITH
60
+ 1.17.2
@@ -0,0 +1,8 @@
1
+ # coding: UTF-8
2
+
3
+ === 0.1.0 / 2019-10-03
4
+
5
+ * Initial public release of the Artemis API wrapper
6
+
7
+ Includes initial support for the following Artemis classes:
8
+ User, Facility, Organization, Batch, Zone, Completion, Harvest, Discard, HarvestUnit, SeedingUnit
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Jamie Hampton
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,187 @@
1
+ # ArtemisApi
2
+
3
+ This is a simple API wrapper for the [Artemis](https://artemisag.com/) API.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'artemis_api', :git => 'https://github.com/artemis-ag/artemis_api'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install artemis_api
20
+
21
+ ## Usage
22
+
23
+ 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
+
25
+ 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
+
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.)
28
+
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,
30
+
31
+ ```ruby
32
+ options = {app_id: 'your_artemis_application_id',
33
+ app_secret: 'your_artemis_secret_id',
34
+ base_uri: 'https://portal.artemisag.com'}
35
+
36
+ client = ArtemisApi::Client.new(access_token: 'your_access_token',
37
+ refresh_token: 'your_refresh_token',
38
+ expires_at: token_expires_at,
39
+ options: options)
40
+ ```
41
+
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']`
43
+
44
+ They will be automatically detected and then you don't have to pass in any options:
45
+ ```ruby
46
+ client = ArtemisApi::Client.new(access_token: 'your_access_token',
47
+ refresh_token: 'your_refresh_token',
48
+ expires_at: token_expires_at)
49
+ ```
50
+
51
+ Once you have a client instance, you can use it to request information from Artemis.
52
+
53
+ To get user information about the Artemis User that is associated with your application ID:
54
+ ```ruby
55
+ client.current_user
56
+ ```
57
+ Or alternatively, you can make the call directly to the User class:
58
+
59
+ ```ruby
60
+ ArtemisApi::User.get_current(client: client)
61
+ ```
62
+
63
+ (Please note that we use named parameters in most of our function calls.)
64
+
65
+ You can use either method to get a list of all Artemis Organizations or Facilities that you have access to:
66
+ ```ruby
67
+ client.organizations
68
+ client.facilities
69
+
70
+ ArtemisApi::Organization.find_all(client: client)
71
+ ArtemisApi::Facility.find_all(client: client)
72
+ ```
73
+
74
+ To get facility information about a single Artemis Organization or Facility that you have access to, by its Artemis ID:
75
+ ```ruby
76
+ client.organization(1)
77
+ client.facility(2)
78
+
79
+ ArtemisApi::Organization.find(id: 1, client: client)
80
+ ArtemisApi::Facility.find(id: 2, client: client)
81
+ ```
82
+
83
+ Other models are scoped by facility, so you have to include the Facility id in the call. (You can also get information about other Artemis Users besides your own account this way.)
84
+
85
+ To get all Users associated with the Facility with an ID of 2, again there are two methods. You can call directly to the User class, or you can query through a facility.
86
+ ```ruby
87
+ client.facility(2).users
88
+
89
+ ArtemisApi::User.find_all(facility_id: 2, client: client)
90
+ ```
91
+
92
+ To get a single User, with id 12, which must also be associated with Facility 2:
93
+ ```ruby
94
+ client.facility(2).user(12)
95
+
96
+ ArtemisApi::User.find(id: 12, facility_id: 2, client: client)
97
+ ```
98
+
99
+ You can get info about Batches, Zones, SeedingUnits, HarvestUnits and Subscriptions in the same manner. Here are a couple examples, but the syntax is all the same.
100
+ ```ruby
101
+ client.facility(2).batches
102
+ client.facility(2).batch(22)
103
+
104
+ ArtemisApi::Batch.find_all(facility_id: 2, client: client)
105
+ ArtemisApi::Batch.find(id: 22, facility_id: 2, client: client)
106
+
107
+ client.facility(2).seeding_units
108
+ client.facility(2).seeding_unit(4)
109
+
110
+ ArtemisApi::SeedingUnit.find_all(facility_id: 2, client: client)
111
+ ArtemisApi::SeedingUnit.find(id: 4, facility_id: 2, client: client)
112
+ ```
113
+
114
+ Completions, Harvests and Discards can be queried through a Batch object in a similar way. Again, you can also call directly to the class. Note that in the above examples, querying through facility or the class will give you the exact same results: that isn't true in this case. Querying through the batch will return only objects associated with that batch, while doing a `find_all` on the class will give you all objects associated with the entire facility.
115
+ ```ruby
116
+ client.facility(2).batch(22).harvests
117
+ client.facility(2).batch(22).discards
118
+ client.facility(2).batch(22).completions
119
+
120
+ client.facility(2).batch(22).harvest(47)
121
+
122
+ ArtemisApi::Harvest.find_all(facility_id: 2, client: client)
123
+ ArtemisApi::Harvest.find(id: 47, facility_id: 2, client: client)
124
+ ```
125
+
126
+ Additionally, Items are scoped by both Facility and Batch, so both are required even if you call directly to the Item class. There is also an optional `seeding_unit_id` param if you query through a batch.
127
+ ```ruby
128
+ client.facility(2).batch(22).items
129
+ client.facility(2).batch(22).items(seeding_unit_id: 17)
130
+
131
+ ArtemisApi::Item.find_all(facility_id: 2, batch_id: 22, client: client)
132
+ ```
133
+
134
+ Once you have queried info about a certain object, it will be stored in a hash called `objects` that exists on your active `client` object. Then, if you have to query the same object again, it can be pulled from that hash instead of doing another actual call to the API, to speed up performance. If you need to actually hit the API again for the most updated information, you can force the query like this:
135
+ ```ruby
136
+ facility = ArtemisApi::Facility.find(id: 2, client: client, force: true)
137
+ ```
138
+
139
+ Additionally, you can optionally include other models in your call that have a relationship with the model you're querying. They will then be included in the payload and added into the objects hash for your `client` for easier querying in the future.
140
+
141
+ ```ruby
142
+ ArtemisApi::Facility.find(id: 2, client: client, include: "users")
143
+ ArtemisApi::Batch.find(id: 22, facility_id: 2, client: client, include: "completions")
144
+ ```
145
+
146
+ 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
+
148
+ ```ruby
149
+ ArtemisApi::Batch.find_all(facility_id: 2, client: client, filters: {view: 'all_batches', search: 'genovese basil'})
150
+ ArtemisApi::Batch.find_all(facility_id: 2, client: client, filters: {ids: [2, 4, 6, 11]})
151
+ ArtemisApi::Completion.find_all(facility_id: 2, client: client, filters: {crop_batch_ids: [5]})
152
+ ArtemisApi::Harvest.find_all(facility_id: 2, client: client, filters: {crop_batch_ids: [5, 7]})
153
+ ArtemisApi::Discard.find_all(facility_id: 2, client: client, filters: {crop_batch_ids: [6, 7, 9]})
154
+ ArtemisApi::Zone.find_all(facility_id: 2, client: client, filters: {seeding_unit_id: 3})
155
+ ArtemisApi::Item.find_all(facility_id: 2, batch_id: 22, client: client, filters: {seeding_unit_id: 8})
156
+ ```
157
+
158
+ 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
+
160
+ 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
+
162
+ ```ruby
163
+ ArtemisApi::Subscription.create(facility_id: 2,
164
+ subject: 'completions',
165
+ destination: 'https://test-app-url.artemisag.io/v1/webhook',
166
+ client: client)
167
+
168
+ facility.create_subscription(subject: 'completions', destination: 'https://test-app-url.artemisag.io/v1/webhook')
169
+ ```
170
+
171
+ ## Development
172
+
173
+ 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.
174
+
175
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
176
+
177
+ ## Contributing
178
+
179
+ Bug reports and pull requests are welcome on GitHub at https://github.com/artemis-ag/artemis_api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
180
+
181
+ ## License
182
+
183
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
184
+
185
+ ## Code of Conduct
186
+
187
+ Everyone interacting in the ArtemisApi project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/artemis-ag/artemis_api/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "artemis_api/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "artemis_api"
8
+ spec.version = ArtemisApi::VERSION
9
+ spec.authors = ["Jamey Hampton"]
10
+ spec.email = ["jhampton@artemisag.com"]
11
+
12
+ spec.summary = %q{An API wrapper for the ArtemisAg API}
13
+ spec.homepage = "https://github.com/artemis-ag/artemis_api/"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.16"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "minitest", "~> 5.0"
26
+ spec.add_development_dependency "oauth2"
27
+ spec.add_development_dependency "webmock"
28
+ spec.add_development_dependency "activesupport"
29
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "artemis_api"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
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__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,19 @@
1
+ require 'artemis_api/client'
2
+ require 'artemis_api/model'
3
+ require 'artemis_api/facility'
4
+ require 'artemis_api/user'
5
+ require 'artemis_api/version'
6
+ require 'artemis_api/zone'
7
+ require 'artemis_api/organization'
8
+ require 'artemis_api/batch'
9
+ require 'artemis_api/completion'
10
+ require 'artemis_api/discard'
11
+ require 'artemis_api/harvest'
12
+ require 'artemis_api/seeding_unit'
13
+ require 'artemis_api/harvest_unit'
14
+ require 'artemis_api/item'
15
+ require 'artemis_api/subscription'
16
+
17
+ module ArtemisApi
18
+ # Your code goes here...
19
+ end
@@ -0,0 +1,66 @@
1
+ module ArtemisApi
2
+ class Batch < ArtemisApi::Model
3
+ json_type 'batches'
4
+ related_to_one :zone
5
+ related_to_one :seeding_unit
6
+ related_to_one :harvest_unit
7
+
8
+ def self.find(id:, client:, facility_id:, include: nil, force: false)
9
+ client.find_one(self.json_type, id, facility_id: facility_id, include: include, force: force)
10
+ end
11
+
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
+ end
15
+
16
+ def completions(include: nil)
17
+ ArtemisApi::Completion.find_all(facility_id: facility_id,
18
+ client: client,
19
+ include: include,
20
+ filters: {crop_batch_ids: [id]})
21
+ end
22
+
23
+ def completion(completion_id, include: nil)
24
+ ArtemisApi::Completion.find(id: completion_id,
25
+ facility_id: facility_id,
26
+ client: client,
27
+ include: include)
28
+ end
29
+
30
+ def discards(include: nil)
31
+ ArtemisApi::Discard.find_all(facility_id: facility_id,
32
+ client: client,
33
+ include: include,
34
+ filters: {crop_batch_ids: [id]})
35
+ end
36
+
37
+ def discard(discard_id, include: nil)
38
+ ArtemisApi::Discard.find(id: discard_id,
39
+ facility_id: facility_id,
40
+ client: client,
41
+ include: include)
42
+ end
43
+
44
+ def harvests(include: nil)
45
+ ArtemisApi::Harvest.find_all(facility_id: facility_id,
46
+ client: client,
47
+ include: include,
48
+ filters: {crop_batch_ids: [id]})
49
+ end
50
+
51
+ def harvest(harvest_id, include: nil)
52
+ ArtemisApi::Harvest.find(id: harvest_id,
53
+ facility_id: facility_id,
54
+ client: client,
55
+ include: include)
56
+ end
57
+
58
+ def items(seeding_unit_id: nil, include: nil)
59
+ ArtemisApi::Item.find_all(facility_id: facility_id,
60
+ batch_id: id,
61
+ client: client,
62
+ include: include,
63
+ filters: {seeding_unit_id: seeding_unit_id})
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,142 @@
1
+ module ArtemisApi
2
+ class Client
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:, refresh_token:, expires_at:, options: {})
8
+ options[:app_id] ||= ENV['ARTEMIS_OAUTH_APP_ID']
9
+ options[:app_secret] ||= ENV['ARTEMIS_OAUTH_APP_SECRET']
10
+ options[:base_uri] ||= ENV['ARTEMIS_BASE_URI']
11
+ @options = options
12
+ @access_token = access_token
13
+ @refresh_token = refresh_token
14
+ @expires_at = expires_at
15
+
16
+ @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 = {}
23
+ end
24
+
25
+ def find_one(type, id, facility_id: nil, include: nil, force: false)
26
+ obj = get_record(type, id)
27
+ if !obj || force
28
+ refresh if @oauth_token.expired?
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
36
+
37
+ response = @oauth_token.get(url)
38
+ obj = process_response(response, type) if response.status == 200
39
+ end
40
+ obj
41
+ end
42
+
43
+ def find_all(type, facility_id: nil, batch_id: nil, include: nil, filters: nil)
44
+ 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
60
+
61
+ response = @oauth_token.get(url)
62
+ if response.status == 200
63
+ records = process_array(response, type, records)
64
+ end
65
+ records
66
+ end
67
+
68
+ def store_record(type, id, data)
69
+ @objects[type] ||= {}
70
+ @objects[type][id.to_i] = ArtemisApi::Model.instance_for(type, data, self)
71
+ end
72
+
73
+ def get_record(type, id)
74
+ @objects[type]&.[](id.to_i)
75
+ end
76
+
77
+ def refresh
78
+ @oauth_token = @oauth_token.refresh!
79
+ end
80
+
81
+ def facilities(include: nil)
82
+ find_all('facilities', include: include)
83
+ end
84
+
85
+ def facility(id, include: nil, force: false)
86
+ find_one('facilities', id, include: include, force: force)
87
+ end
88
+
89
+ def organizations(include: nil)
90
+ find_all('organizations', include: include)
91
+ end
92
+
93
+ def organization(id, include: nil, force: false)
94
+ find_one('organizations', id, include: include, force: force)
95
+ end
96
+
97
+ def current_user(include: nil)
98
+ ArtemisApi::User.get_current(client: self, include: include)
99
+ end
100
+
101
+ def process_response(response, type)
102
+ json = JSON.parse(response.body)
103
+ obj = store_record(type, json['data']['id'].to_i, json['data'])
104
+ process_included_objects(json['included']) if json['included']
105
+
106
+ obj
107
+ end
108
+
109
+ private
110
+
111
+ def process_array(response, type, records)
112
+ json = JSON.parse(response.body)
113
+ json['data'].each do |obj|
114
+ record = store_record(type, obj['id'], obj)
115
+ records << record
116
+ end
117
+ process_included_objects(json['included']) if json['included']
118
+
119
+ records
120
+ end
121
+
122
+ def process_included_objects(included_array)
123
+ included_array.each do |included_obj|
124
+ store_record(included_obj['type'], included_obj['id'], included_obj)
125
+ end
126
+ end
127
+
128
+ def format_filters(filter_hash)
129
+ filter_string = ''
130
+ filter_hash.each do |k, v|
131
+ if v.kind_of?(Array)
132
+ v.each do |item|
133
+ filter_string += "filter[#{k}][]=#{item}&"
134
+ end
135
+ else
136
+ filter_string += "filter[#{k}]=#{v}&"
137
+ end
138
+ end
139
+ filter_string
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,13 @@
1
+ module ArtemisApi
2
+ class Completion < ArtemisApi::Model
3
+ json_type 'completions'
4
+
5
+ def self.find(id:, facility_id:, client:, include: nil, force: false)
6
+ client.find_one(self.json_type, id, facility_id: facility_id, include: include, force: force)
7
+ end
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)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module ArtemisApi
2
+ class Discard < ArtemisApi::Model
3
+ json_type 'discards'
4
+
5
+ def self.find(id:, facility_id:, client:, include: nil, force: false)
6
+ client.find_one(self.json_type, id, facility_id: facility_id, include: include, force: force)
7
+ end
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)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,65 @@
1
+ module ArtemisApi
2
+ class Facility < ArtemisApi::Model
3
+ json_type 'facilities'
4
+
5
+ def self.find(id:, client:, include: nil, force: false)
6
+ client.find_one(self.json_type, id, include: include, force: force)
7
+ end
8
+
9
+ def self.find_all(client:, include: nil)
10
+ client.find_all(self.json_type, include: include)
11
+ end
12
+
13
+ def zones(include: nil)
14
+ ArtemisApi::Zone.find_all(facility_id: id, client: client, include: include)
15
+ end
16
+
17
+ def zone(zone_id, include: nil)
18
+ ArtemisApi::Zone.find(id: zone_id, facility_id: id, client: client, include: include)
19
+ end
20
+
21
+ def batches(include: nil)
22
+ ArtemisApi::Batch.find_all(facility_id: id, client: client, include: include)
23
+ end
24
+
25
+ def batch(batch_id, include: nil)
26
+ ArtemisApi::Batch.find(id: batch_id, facility_id: id, client: client, include: include)
27
+ end
28
+
29
+ def users(include: nil)
30
+ ArtemisApi::User.find_all(facility_id: id, client: client, include: include)
31
+ end
32
+
33
+ def user(user_id, include: nil)
34
+ ArtemisApi::User.find(id: user_id, facility_id: id, client: client, include: include)
35
+ end
36
+
37
+ def seeding_units(include: nil)
38
+ ArtemisApi::SeedingUnit.find_all(facility_id: id, client: client, include: include)
39
+ end
40
+
41
+ def seeding_unit(unit_id, include: nil)
42
+ ArtemisApi::SeedingUnit.find(id: unit_id, facility_id: id, client: client, include: include)
43
+ end
44
+
45
+ def harvest_units(include: nil)
46
+ ArtemisApi::HarvestUnit.find_all(facility_id: id, client: client, include: include)
47
+ end
48
+
49
+ def harvest_unit(unit_id, include: nil)
50
+ ArtemisApi::HarvestUnit.find(id: unit_id, facility_id: id, client: client, include: include)
51
+ end
52
+
53
+ def subscriptions
54
+ ArtemisApi::Subscription.find_all(facility_id: id, client: client)
55
+ end
56
+
57
+ def subscription(subscription_id)
58
+ ArtemisApi::Subscription.find(id: subscription_id, facility_id: id, client: client)
59
+ end
60
+
61
+ def create_subscription(subject:, destination:)
62
+ ArtemisApi::Subscription.create(facility_id: id, subject: subject, destination: destination, client: client)
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,13 @@
1
+ module ArtemisApi
2
+ class Harvest < ArtemisApi::Model
3
+ json_type 'harvests'
4
+
5
+ def self.find(id:, facility_id:, client:, include: nil, force: false)
6
+ client.find_one(self.json_type, id, facility_id: facility_id, include: include, force: force)
7
+ end
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)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module ArtemisApi
2
+ class HarvestUnit < ArtemisApi::Model
3
+ json_type 'harvest_units'
4
+
5
+ def self.find(id:, facility_id:, client:, include: nil, force: false)
6
+ client.find_one(self.json_type, id, facility_id: facility_id, include: include, force: force)
7
+ end
8
+
9
+ def self.find_all(facility_id:, client:, include: nil)
10
+ client.find_all(self.json_type, facility_id: facility_id, include: include)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ module ArtemisApi
2
+ class Item < ArtemisApi::Model
3
+ json_type 'items'
4
+
5
+ def self.find_all(facility_id:, batch_id:, client:, include: nil, filters: nil)
6
+ client.find_all(self.json_type, facility_id: facility_id, batch_id: batch_id, include: include, filters: filters)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,42 @@
1
+ module ArtemisApi
2
+ class Model
3
+ attr_reader :client, :id, :attributes, :relationships
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'])
9
+ end
10
+ end
11
+
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'])
15
+ end
16
+ end
17
+
18
+ def self.json_type(type = nil)
19
+ if type
20
+ @json_type = type
21
+ @@registered_classes ||= {}
22
+ @@registered_classes[type] = self
23
+ end
24
+ @json_type
25
+ end
26
+
27
+ def self.instance_for(type, data, client)
28
+ @@registered_classes[type]&.new(client, data)
29
+ end
30
+
31
+ def method_missing(name)
32
+ attributes[name.to_s]
33
+ end
34
+
35
+ def initialize(client, data)
36
+ @client = client
37
+ @id = data['id'].to_i
38
+ @attributes = data['attributes']
39
+ @relationships = data['relationships']
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,13 @@
1
+ module ArtemisApi
2
+ class Organization < ArtemisApi::Model
3
+ json_type 'organizations'
4
+
5
+ def self.find(id:, client:, include: nil, force: false)
6
+ client.find_one(self.json_type, id, include: include, force: force)
7
+ end
8
+
9
+ def self.find_all(client:, include: nil)
10
+ client.find_all(self.json_type, include: include)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module ArtemisApi
2
+ class SeedingUnit < ArtemisApi::Model
3
+ json_type 'seeding_units'
4
+
5
+ def self.find(id:, facility_id:, client:, include: nil, force: false)
6
+ client.find_one(self.json_type, id, facility_id: facility_id, include: include, force: force)
7
+ end
8
+
9
+ def self.find_all(facility_id:, client:, include: nil)
10
+ client.find_all(self.json_type, facility_id: facility_id, include: include)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,24 @@
1
+ module ArtemisApi
2
+ class Subscription < ArtemisApi::Model
3
+ json_type 'subscriptions'
4
+
5
+ def self.find(id:, facility_id:, client:, include: nil, force: false)
6
+ client.find_one(self.json_type, id, facility_id: facility_id, include: include, force: force)
7
+ end
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)
11
+ end
12
+
13
+ def self.create(facility_id:, subject:, destination:, client:)
14
+ client.oauth_token.refresh! if client.oauth_token.expired?
15
+
16
+ url = "#{client.options[:base_uri]}/api/v3/facilities/#{facility_id}/subscriptions"
17
+ params = { body: { subscription: { subject: subject, destination: destination } } }
18
+
19
+ response = client.oauth_token.post(url, params)
20
+
21
+ response.status == 200 ? client.process_response(response, 'subscriptions') : false
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,27 @@
1
+ module ArtemisApi
2
+ class User < ArtemisApi::Model
3
+ json_type 'users'
4
+ related_to_many :facilities
5
+
6
+ def self.get_current(client:, include: nil)
7
+ self.json_type
8
+ client.refresh if client.oauth_token.expired?
9
+ url = "#{client.options[:base_uri]}/api/v3/user"
10
+ url = "#{url}?include=#{include}" if include
11
+ response = client.oauth_token.get(url)
12
+ if response.status == 200
13
+ json = JSON.parse(response.body)
14
+ obj = client.store_record('users', json['data']['id'], json['data'])
15
+ end
16
+ obj
17
+ end
18
+
19
+ def self.find(id:, facility_id:, client:, include: nil, force: false)
20
+ client.find_one(self.json_type, id, facility_id: facility_id, include: include, force: force)
21
+ end
22
+
23
+ def self.find_all(facility_id:, client:, include: nil)
24
+ client.find_all(self.json_type, facility_id: facility_id, include: include)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module ArtemisApi
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,13 @@
1
+ module ArtemisApi
2
+ class Zone < ArtemisApi::Model
3
+ json_type 'zones'
4
+
5
+ def self.find(id:, facility_id:, client:, include: nil, force: false)
6
+ client.find_one(self.json_type, id, facility_id: facility_id, include: include, force: force)
7
+ end
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)
11
+ end
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: artemis_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jamey Hampton
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-10-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: oauth2
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activesupport
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - jhampton@artemisag.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".travis.yml"
106
+ - CODE_OF_CONDUCT.md
107
+ - Gemfile
108
+ - Gemfile.lock
109
+ - History.txt
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - artemis_api.gemspec
114
+ - bin/console
115
+ - bin/setup
116
+ - lib/artemis_api.rb
117
+ - lib/artemis_api/batch.rb
118
+ - lib/artemis_api/client.rb
119
+ - lib/artemis_api/completion.rb
120
+ - lib/artemis_api/discard.rb
121
+ - lib/artemis_api/facility.rb
122
+ - lib/artemis_api/harvest.rb
123
+ - lib/artemis_api/harvest_unit.rb
124
+ - lib/artemis_api/item.rb
125
+ - lib/artemis_api/model.rb
126
+ - lib/artemis_api/organization.rb
127
+ - lib/artemis_api/seeding_unit.rb
128
+ - lib/artemis_api/subscription.rb
129
+ - lib/artemis_api/user.rb
130
+ - lib/artemis_api/version.rb
131
+ - lib/artemis_api/zone.rb
132
+ homepage: https://github.com/artemis-ag/artemis_api/
133
+ licenses:
134
+ - MIT
135
+ metadata: {}
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubyforge_project:
152
+ rubygems_version: 2.5.1
153
+ signing_key:
154
+ specification_version: 4
155
+ summary: An API wrapper for the ArtemisAg API
156
+ test_files: []