Class: ArtemisApi::Client
- Inherits:
-
Object
- Object
- ArtemisApi::Client
- Defined in:
- lib/artemis_api/client.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#expires_at ⇒ Object
readonly
Returns the value of attribute expires_at.
-
#oauth_client ⇒ Object
readonly
Returns the value of attribute oauth_client.
-
#oauth_token ⇒ Object
readonly
Returns the value of attribute oauth_token.
-
#objects ⇒ Object
readonly
Returns the value of attribute objects.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#refresh_token ⇒ Object
readonly
Returns the value of attribute refresh_token.
Instance Method Summary collapse
- #build_url(path:, query: nil) ⇒ Object
- #current_user(include: nil) ⇒ Object
- #facilities(include: nil) ⇒ Object
- #facility(id, include: nil, force: false) ⇒ Object
- #find_all(type, facility_id: nil, batch_id: nil, include: nil, filters: nil) ⇒ Object
- #find_one(type, id, facility_id: nil, include: nil, force: false) ⇒ Object
- #get_record(type, id) ⇒ Object
-
#initialize(access_token: nil, refresh_token: nil, expires_at: nil, auth_code: nil, redirect_uri: nil, options: {}) ⇒ Client
constructor
A new instance of Client.
- #organization(id, include: nil, force: false) ⇒ Object
- #organizations(include: nil) ⇒ Object
- #process_response(response, type) ⇒ Object
- #refresh ⇒ Object
- #remove_record(type, id) ⇒ Object
- #store_record(type, id, data) ⇒ Object
Constructor Details
#initialize(access_token: nil, refresh_token: nil, expires_at: nil, auth_code: nil, redirect_uri: nil, options: {}) ⇒ Client
Returns a new instance of Client.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/artemis_api/client.rb', line 7 def initialize(access_token: nil, refresh_token: nil, expires_at: nil, auth_code: nil, redirect_uri: nil, options: {}) unless (access_token && refresh_token && expires_at) || auth_code raise ArgumentError.new('You must either provide your access token, refresh token & expires at time, or an authorization code.') end [:app_id] ||= ENV['ARTEMIS_OAUTH_APP_ID'] [:app_secret] ||= ENV['ARTEMIS_OAUTH_APP_SECRET'] [:base_uri] ||= ENV['ARTEMIS_BASE_URI'] @options = @objects = {} @oauth_client = OAuth2::Client.new(@options[:app_id], @options[:app_secret], site: @options[:base_uri]) if access_token && refresh_token && expires_at @access_token = access_token @refresh_token = refresh_token @expires_at = expires_at.to_i @oauth_token = OAuth2::AccessToken.from_hash( oauth_client, {access_token: @access_token, refresh_token: @refresh_token, expires_at: @expires_at}) elsif auth_code redirect_uri ||= 'urn:ietf:wg:oauth:2.0:oob' @oauth_token = @oauth_client.auth_code.get_token(auth_code, redirect_uri: redirect_uri) @access_token = @oauth_token.token @refresh_token = @oauth_token.refresh_token @expires_at = @oauth_token.expires_at end end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
4 5 6 |
# File 'lib/artemis_api/client.rb', line 4 def access_token @access_token end |
#expires_at ⇒ Object (readonly)
Returns the value of attribute expires_at.
4 5 6 |
# File 'lib/artemis_api/client.rb', line 4 def expires_at @expires_at end |
#oauth_client ⇒ Object (readonly)
Returns the value of attribute oauth_client.
4 5 6 |
# File 'lib/artemis_api/client.rb', line 4 def oauth_client @oauth_client end |
#oauth_token ⇒ Object (readonly)
Returns the value of attribute oauth_token.
4 5 6 |
# File 'lib/artemis_api/client.rb', line 4 def oauth_token @oauth_token end |
#objects ⇒ Object (readonly)
Returns the value of attribute objects.
4 5 6 |
# File 'lib/artemis_api/client.rb', line 4 def objects @objects end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/artemis_api/client.rb', line 4 def @options end |
#refresh_token ⇒ Object (readonly)
Returns the value of attribute refresh_token.
4 5 6 |
# File 'lib/artemis_api/client.rb', line 4 def refresh_token @refresh_token end |
Instance Method Details
#build_url(path:, query: nil) ⇒ Object
88 89 90 91 |
# File 'lib/artemis_api/client.rb', line 88 def build_url(path:, query: nil) uri = URI::Generic.build(path: path, query: query) @options[:base_uri] + uri.to_s end |
#current_user(include: nil) ⇒ Object
126 127 128 |
# File 'lib/artemis_api/client.rb', line 126 def current_user(include: nil) ArtemisApi::User.get_current(client: self, include: include) end |
#facilities(include: nil) ⇒ Object
110 111 112 |
# File 'lib/artemis_api/client.rb', line 110 def facilities(include: nil) find_all('facilities', include: include) end |
#facility(id, include: nil, force: false) ⇒ Object
114 115 116 |
# File 'lib/artemis_api/client.rb', line 114 def facility(id, include: nil, force: false) find_one('facilities', id, include: include, force: force) end |
#find_all(type, facility_id: nil, batch_id: nil, include: nil, filters: nil) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/artemis_api/client.rb', line 63 def find_all(type, facility_id: nil, batch_id: nil, include: nil, filters: nil) records = [] refresh if @oauth_token.expired? path = if facility_id && batch_id "/api/v3/facilities/#{facility_id}/batches/#{batch_id}/#{type}" elsif facility_id && batch_id.nil? "/api/v3/facilities/#{facility_id}/#{type}" else "/api/v3/#{type}" end query = {} query[:include] = include if include format_filters(filters, query) if filters url = build_url(path: path, query: URI.encode_www_form(query)) response = @oauth_token.get(url) if response.status == 200 records = process_array(response, type, records) end records end |
#find_one(type, id, facility_id: nil, include: nil, force: false) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/artemis_api/client.rb', line 41 def find_one(type, id, facility_id: nil, include: nil, force: false) obj = get_record(type, id) if !obj || force refresh if @oauth_token.expired? path = if facility_id "/api/v3/facilities/#{facility_id}/#{type}/#{id}" else "/api/v3/#{type}/#{id}" end query = {} query[:include] = include if include url = build_url(path: path, query: URI.encode_www_form(query)) response = @oauth_token.get(url) obj = process_response(response, type) if response.status == 200 end obj end |
#get_record(type, id) ⇒ Object
98 99 100 |
# File 'lib/artemis_api/client.rb', line 98 def get_record(type, id) @objects.dig(type, id.to_i) end |
#organization(id, include: nil, force: false) ⇒ Object
122 123 124 |
# File 'lib/artemis_api/client.rb', line 122 def organization(id, include: nil, force: false) find_one('organizations', id, include: include, force: force) end |
#organizations(include: nil) ⇒ Object
118 119 120 |
# File 'lib/artemis_api/client.rb', line 118 def organizations(include: nil) find_all('organizations', include: include) end |
#process_response(response, type) ⇒ Object
130 131 132 133 134 135 136 |
# File 'lib/artemis_api/client.rb', line 130 def process_response(response, type) json = JSON.parse(response.body) obj = store_record(type, json['data']['id'].to_i, json['data']) process_included_objects(json['included']) if json['included'] obj end |
#refresh ⇒ Object
106 107 108 |
# File 'lib/artemis_api/client.rb', line 106 def refresh @oauth_token = @oauth_token.refresh! end |
#remove_record(type, id) ⇒ Object
102 103 104 |
# File 'lib/artemis_api/client.rb', line 102 def remove_record(type, id) @objects[type]&.delete(id.to_i) end |
#store_record(type, id, data) ⇒ Object
93 94 95 96 |
# File 'lib/artemis_api/client.rb', line 93 def store_record(type, id, data) @objects[type] ||= {} @objects[type][id.to_i] = ArtemisApi::Model.instance_for(type, data, self) end |