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.
-
#on_token_failed ⇒ Object
readonly
Returns the value of attribute on_token_failed.
-
#on_token_refreshed ⇒ Object
readonly
Returns the value of attribute on_token_refreshed.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#refresh_token ⇒ Object
readonly
Returns the value of attribute refresh_token.
Instance Method Summary collapse
- #auto_refresh! ⇒ Object
- #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, page: 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, on_token_refreshed: nil, on_token_failed: 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
- #set_oauth_token_from_parts(access_token:, refresh_token:, expires_at:) ⇒ Object
- #store_record(type, id, data, included = nil) ⇒ Object
Constructor Details
#initialize(access_token: nil, refresh_token: nil, expires_at: nil, auth_code: nil, redirect_uri: nil, on_token_refreshed: nil, on_token_failed: nil, options: {}) ⇒ Client
Returns a new instance of Client.
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 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/artemis_api/client.rb', line 9 def initialize( access_token: nil, refresh_token: nil, expires_at: nil, auth_code: nil, redirect_uri: nil, on_token_refreshed: nil, on_token_failed: 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'] = @objects = {} @on_token_refreshed = on_token_refreshed @on_token_failed = on_token_failed @oauth_client = OAuth2::Client.new([:app_id], [:app_secret], site: [:base_uri]) if 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 else set_oauth_token_from_parts( access_token: access_token, refresh_token: refresh_token, expires_at: 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 |
#on_token_failed ⇒ Object (readonly)
Returns the value of attribute on_token_failed.
4 5 6 |
# File 'lib/artemis_api/client.rb', line 4 def on_token_failed @on_token_failed end |
#on_token_refreshed ⇒ Object (readonly)
Returns the value of attribute on_token_refreshed.
4 5 6 |
# File 'lib/artemis_api/client.rb', line 4 def on_token_refreshed @on_token_refreshed end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/artemis_api/client.rb', line 4 def 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
#auto_refresh! ⇒ Object
131 132 133 |
# File 'lib/artemis_api/client.rb', line 131 def auto_refresh! refresh! if @oauth_token.expired? end |
#build_url(path:, query: nil) ⇒ Object
113 114 115 116 |
# File 'lib/artemis_api/client.rb', line 113 def build_url(path:, query: nil) uri = URI::Generic.build(path: path, query: query) [:base_uri] + uri.to_s end |
#current_user(include: nil) ⇒ Object
169 170 171 |
# File 'lib/artemis_api/client.rb', line 169 def current_user(include: nil) ArtemisApi::User.get_current(client: self, include: include) end |
#facilities(include: nil) ⇒ Object
153 154 155 |
# File 'lib/artemis_api/client.rb', line 153 def facilities(include: nil) find_all('facilities', include: include) end |
#facility(id, include: nil, force: false) ⇒ Object
157 158 159 |
# File 'lib/artemis_api/client.rb', line 157 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, page: nil) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/artemis_api/client.rb', line 87 def find_all(type, facility_id: nil, batch_id: nil, include: nil, filters: nil, page: nil) records = [] auto_refresh! 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 format_pagination(page, query) if page 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
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/artemis_api/client.rb', line 65 def find_one(type, id, facility_id: nil, include: nil, force: false) obj = get_record(type, id) if !obj || force auto_refresh! 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
123 124 125 |
# File 'lib/artemis_api/client.rb', line 123 def get_record(type, id) @objects.dig(type, id.to_i) end |
#organization(id, include: nil, force: false) ⇒ Object
165 166 167 |
# File 'lib/artemis_api/client.rb', line 165 def organization(id, include: nil, force: false) find_one('organizations', id, include: include, force: force) end |
#organizations(include: nil) ⇒ Object
161 162 163 |
# File 'lib/artemis_api/client.rb', line 161 def organizations(include: nil) find_all('organizations', include: include) end |
#process_response(response, type) ⇒ Object
173 174 175 176 177 178 179 |
# File 'lib/artemis_api/client.rb', line 173 def process_response(response, type) json = JSON.parse(response.body) included = process_included_objects(json['included']) if json['included'] obj = store_record(type, json['data']['id'].to_i, json['data'], included) obj end |
#refresh! ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/artemis_api/client.rb', line 135 def refresh! old_refresh_token = @oauth_token.refresh_token @oauth_token = @oauth_token.refresh! if old_refresh_token != @oauth_token.refresh_token on_token_refreshed && on_token_refreshed.call(self, @oauth_token, old_refresh_token) end @oauth_token rescue OAuth2::Error => err if on_token_failed on_token_failed.call(self, @oauth_token, err) @oauth_token else raise err end end |
#remove_record(type, id) ⇒ Object
127 128 129 |
# File 'lib/artemis_api/client.rb', line 127 def remove_record(type, id) @objects[type]&.delete(id.to_i) end |
#set_oauth_token_from_parts(access_token:, refresh_token:, expires_at:) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/artemis_api/client.rb', line 51 def set_oauth_token_from_parts(access_token:, refresh_token:, expires_at:) 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}) end end |
#store_record(type, id, data, included = nil) ⇒ Object
118 119 120 121 |
# File 'lib/artemis_api/client.rb', line 118 def store_record(type, id, data, included = nil) @objects[type] ||= {} @objects[type][id.to_i] = ArtemisApi::Model.instance_for(type, data, included, self) end |