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:, refresh_token:, expires_at:, options: {}) ⇒ Client
constructor
A new instance of Client.
- #organization(id, include: nil, force: false) ⇒ Object
- #organizations(include: nil) ⇒ Object
- #process_response(response, type) ⇒ Object
- #record_stored?(type, id) ⇒ Boolean
- #refresh ⇒ Object
- #remove_record(type, id) ⇒ Object
- #store_record(type, id, data) ⇒ Object
Constructor Details
#initialize(access_token:, refresh_token:, expires_at:, 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 |
# File 'lib/artemis_api/client.rb', line 7 def initialize(access_token:, refresh_token:, expires_at:, options: {}) [:app_id] ||= ENV['ARTEMIS_OAUTH_APP_ID'] [:app_secret] ||= ENV['ARTEMIS_OAUTH_APP_SECRET'] [:base_uri] ||= ENV['ARTEMIS_BASE_URI'] @options = @access_token = access_token @refresh_token = refresh_token @expires_at = expires_at @oauth_client = OAuth2::Client.new(@options[:app_id], @options[:app_secret], site: @options[:base_uri]) @oauth_token = OAuth2::AccessToken.from_hash( oauth_client, {access_token: @access_token, refresh_token: @refresh_token, expires_at: @expires_at}) @objects = {} 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
72 73 74 75 |
# File 'lib/artemis_api/client.rb', line 72 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
118 119 120 |
# File 'lib/artemis_api/client.rb', line 118 def current_user(include: nil) ArtemisApi::User.get_current(client: self, include: include) end |
#facilities(include: nil) ⇒ Object
102 103 104 |
# File 'lib/artemis_api/client.rb', line 102 def facilities(include: nil) find_all('facilities', include: include) end |
#facility(id, include: nil, force: false) ⇒ Object
106 107 108 |
# File 'lib/artemis_api/client.rb', line 106 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
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/artemis_api/client.rb', line 47 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
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/artemis_api/client.rb', line 25 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
82 83 84 |
# File 'lib/artemis_api/client.rb', line 82 def get_record(type, id) @objects.dig(type, id.to_i) end |
#organization(id, include: nil, force: false) ⇒ Object
114 115 116 |
# File 'lib/artemis_api/client.rb', line 114 def organization(id, include: nil, force: false) find_one('organizations', id, include: include, force: force) end |
#organizations(include: nil) ⇒ Object
110 111 112 |
# File 'lib/artemis_api/client.rb', line 110 def organizations(include: nil) find_all('organizations', include: include) end |
#process_response(response, type) ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/artemis_api/client.rb', line 122 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 |
#record_stored?(type, id) ⇒ Boolean
90 91 92 |
# File 'lib/artemis_api/client.rb', line 90 def record_stored?(type, id) get_record(type, id).present? end |
#refresh ⇒ Object
98 99 100 |
# File 'lib/artemis_api/client.rb', line 98 def refresh @oauth_token = @oauth_token.refresh! end |
#remove_record(type, id) ⇒ Object
86 87 88 |
# File 'lib/artemis_api/client.rb', line 86 def remove_record(type, id) @objects[type].delete(id.to_i) if record_stored?(type, id) end |
#store_record(type, id, data) ⇒ Object
77 78 79 80 |
# File 'lib/artemis_api/client.rb', line 77 def store_record(type, id, data) @objects[type] ||= {} @objects[type][id.to_i] = ArtemisApi::Model.instance_for(type, data, self) end |