artemis_api 0.2.1 → 0.3.0

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: 77fb353bfa893d3ff210292444e20d71aa7dd5cf
4
- data.tar.gz: 8aaa44cae9c5eec62ff7dfd524e5576ba7d79ce9
3
+ metadata.gz: 0e8888de9d53b67758011986d95da48078a539d1
4
+ data.tar.gz: 70e7223008c998a11c2e8c195bb9ab2e44048167
5
5
  SHA512:
6
- metadata.gz: ea1ad95a03f6f486bc66c629300f4726b912ce7213f54e7d26c2231a30a197688dac2dd93f08038b6d950b9e753f501e4b9f3225e8d1457900f63ad928393b10
7
- data.tar.gz: 9d9b6ba166a362890551ae8c8b0d3f489d5b71c32e47f9851175f519ce216fec3702f703401a924673b4d97b32abd1698f816ff44f56b6d7b121e107d755b264
6
+ metadata.gz: 2f0afda52acc388593a3c704fea8593e09af8713c8d9cfa64c7ec6a9b7e99960465a732202c2b69358371f8f77f5fd59ae48033737e609428073a1d7e41bbd97
7
+ data.tar.gz: 77da6377c501a342397d87438d1fa41f9d44d21dfd67e434b188bb147d50e5e1768ed8b9070dafc9f3a4c1f1fc38620a834bbf1a4c5c4dc2771820c83191debe
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- artemis_api (0.1.0)
4
+ artemis_api (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/History.txt CHANGED
@@ -1,5 +1,10 @@
1
1
  # coding: UTF-8
2
2
 
3
+ === 0.3.0 / 2019-11-04
4
+
5
+ * Added an alternative OAuth flow that can accept an authorization code
6
+ directly and handle the creation of the OAuth client and token
7
+
3
8
  === 0.2.1 / 2019-10-28
4
9
 
5
10
  * Minor updates to documentation
data/README.md CHANGED
@@ -26,13 +26,15 @@ Or install it yourself as:
26
26
 
27
27
  ## Usage
28
28
 
29
+ #### Instantiating your Client
30
+
29
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.
30
32
 
31
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.
32
34
 
33
- (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.
34
36
 
35
- 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:
36
38
 
37
39
  ```ruby
38
40
  options = {app_id: 'your_artemis_application_id',
@@ -45,7 +47,7 @@ client = ArtemisApi::Client.new(access_token: 'your_access_token',
45
47
  options: options)
46
48
  ```
47
49
 
48
- 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']`
49
51
 
50
52
  They will be automatically detected and then you don't have to pass in any options:
51
53
  ```ruby
@@ -54,6 +56,18 @@ client = ArtemisApi::Client.new(access_token: 'your_access_token',
54
56
  expires_at: token_expires_at)
55
57
  ```
56
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 pass it in while instantiating your client. 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 beside it on your application show page will 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.)
60
+
61
+ ```ruby
62
+ client = ArtemisApi::Client.new(auth_code: 'your_generated_authorization_code')
63
+ ```
64
+
65
+ This can also take an optional `redirect_uri` named param, which you can pass through if you have another callback url you're using. Otherwise if you don't pass anything through that param, it will default to the `urn:ietf:wg:oauth:2.0:oob` address.
66
+
67
+ Providing your authorization code like this will do the authorization through OAuth for you and create and store your access tokens for you so you don't have to worry about them.
68
+
69
+ #### Requesting data from Artemis
70
+
57
71
  Once you have a client instance, you can use it to request information from Artemis.
58
72
 
59
73
  To get user information about the Artemis User that is associated with your application ID:
@@ -4,22 +4,38 @@ module ArtemisApi
4
4
  attr_reader :options, :objects, :access_token, :refresh_token, :oauth_client,
5
5
  :oauth_token, :expires_at
6
6
 
7
- def initialize(access_token:, refresh_token:, expires_at:, options: {})
7
+ def initialize(access_token: nil, refresh_token: nil, expires_at: nil, auth_code: nil, redirect_uri: nil, options: {})
8
+ unless (access_token && refresh_token && expires_at) || auth_code
9
+ raise ArgumentError.new('You must either provide your access token, refresh token & expires at time, or an authorization code.')
10
+ end
11
+
8
12
  options[:app_id] ||= ENV['ARTEMIS_OAUTH_APP_ID']
9
13
  options[:app_secret] ||= ENV['ARTEMIS_OAUTH_APP_SECRET']
10
14
  options[:base_uri] ||= ENV['ARTEMIS_BASE_URI']
11
15
  @options = options
12
- @access_token = access_token
13
- @refresh_token = refresh_token
14
- @expires_at = expires_at
16
+ @objects = {}
15
17
 
16
18
  @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 = {}
19
+
20
+ if access_token && refresh_token && expires_at
21
+ @access_token = access_token
22
+ @refresh_token = refresh_token
23
+ @expires_at = expires_at.to_i
24
+
25
+ @oauth_token = OAuth2::AccessToken.from_hash(
26
+ oauth_client,
27
+ {access_token: @access_token,
28
+ refresh_token: @refresh_token,
29
+ expires_at: @expires_at})
30
+ elsif auth_code
31
+ redirect_uri ||= 'urn:ietf:wg:oauth:2.0:oob'
32
+
33
+ @oauth_token = @oauth_client.auth_code.get_token(auth_code, redirect_uri: redirect_uri)
34
+
35
+ @access_token = @oauth_token.token
36
+ @refresh_token = @oauth_token.refresh_token
37
+ @expires_at = @oauth_token.expires_at
38
+ end
23
39
  end
24
40
 
25
41
  def find_one(type, id, facility_id: nil, include: nil, force: false)
@@ -84,15 +100,7 @@ module ArtemisApi
84
100
  end
85
101
 
86
102
  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?
92
- end
93
-
94
- def remove_record(type, id)
95
- @objects[type].delete(id.to_i)
103
+ @objects[type]&.delete(id.to_i)
96
104
  end
97
105
 
98
106
  def refresh
@@ -1,3 +1,3 @@
1
1
  module ArtemisApi
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
data/test/client_test.rb CHANGED
@@ -15,6 +15,29 @@ class ClientTest < Minitest::Test
15
15
  assert_equal false, @client.oauth_token.expired?
16
16
  end
17
17
 
18
+ def test_creating_a_client_instance_with_auth_code
19
+ stub_request(:post, 'http://localhost:3000/oauth/token')
20
+ .with(body: {"client_id"=>"12345",
21
+ "client_secret"=>"67890",
22
+ "code"=>"aJ7xsj7",
23
+ "grant_type"=>"authorization_code",
24
+ "redirect_uri"=>"urn:ietf:wg:oauth:2.0:oob"})
25
+ .to_return(status: 200, body: {access_token: 'ya29', refresh_token: 'eyJh', expires_in: 7200, token_type: 'Bearer', created_at: Time.now.to_i}.to_json, headers: { 'Content-Type'=> 'application/json;charset=UTF-8'})
26
+
27
+ options = {app_id: '12345',
28
+ app_secret: '67890',
29
+ base_uri: 'http://localhost:3000'}
30
+ @client = ArtemisApi::Client.new(auth_code: 'aJ7xsj7', options: options)
31
+
32
+ assert_equal '12345', @client.oauth_client.id
33
+ assert_equal 'ya29', @client.oauth_token.token
34
+ assert_equal false, @client.oauth_token.expired?
35
+ end
36
+
37
+ def test_creating_an_invalid_client_instance
38
+ assert_raises(ArgumentError) { ArtemisApi::Client.new(access_token: 'ya29') }
39
+ end
40
+
18
41
  def test_current_user
19
42
  get_client
20
43
  stub_user
@@ -22,4 +45,22 @@ class ClientTest < Minitest::Test
22
45
  user = @client.current_user
23
46
  assert_equal user.class, ArtemisApi::User
24
47
  end
48
+
49
+ def test_remove_record
50
+ get_client
51
+
52
+ type = 'subscriptions'
53
+
54
+ # model type added once, removed twice
55
+ @client.store_record(type, 1, 'id' => 1)
56
+ assert_equal 1, @client.get_record(type, 1).id
57
+ @client.remove_record(type, 1)
58
+ assert_nil @client.get_record(type, 1)
59
+ assert_nothing_raised { @client.remove_record(type, 1) }
60
+
61
+ # model type not added, attempt to remove one
62
+ undefined_type = 'something'
63
+ assert_nil @client.instance_variable_get(:"@objects")[undefined_type]
64
+ assert_nothing_raised { @client.remove_record(undefined_type, 1) }
65
+ end
25
66
  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.1
4
+ version: 0.3.0
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-28 00:00:00.000000000 Z
11
+ date: 2019-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler