Class: Lattice::Client
- Inherits:
-
Object
- Object
- Lattice::Client
- Defined in:
- lib/lattice/client.rb
Direct Known Subclasses
Constant Summary collapse
- TOKEN_URL =
"https://api-auth.latticehq.com/oauth2/token"- BASE_URL =
"https://api.latticehq.com/v2"
Class Method Summary collapse
- .access_token ⇒ Object
- .build_url(path, params) ⇒ Object
- .get(path, params = {}) ⇒ Object
- .headers ⇒ Object
-
.paginated_get(path, params = {}) ⇒ Array<Hash>
Retrieves all paginated results from the specified API endpoint.
Class Method Details
.access_token ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/lattice/client.rb', line 57 def self.access_token params = { grant_type: "client_credentials", client_id: Lattice.client_id, client_secret: Lattice.client_secret, scope: Lattice.scope } response = RestClient.post(TOKEN_URL, params, { content_type: "application/x-www-form-urlencoded" }) JSON.parse(response.body)["access_token"] end |
.build_url(path, params) ⇒ Object
44 45 46 47 48 |
# File 'lib/lattice/client.rb', line 44 def self.build_url(path, params) url = "#{BASE_URL}#{path}" url += "?#{URI.encode_www_form(params)}" unless params.empty? url end |
.get(path, params = {}) ⇒ Object
11 12 13 14 15 |
# File 'lib/lattice/client.rb', line 11 def self.get(path, params = {}) url = build_url(path, params) response = RestClient.get(url, headers) JSON.parse(response.body) end |
.headers ⇒ Object
50 51 52 53 54 55 |
# File 'lib/lattice/client.rb', line 50 def self.headers { Accept: "application/json", Authorization: "Bearer #{access_token}" } end |
.paginated_get(path, params = {}) ⇒ Array<Hash>
Note:
The API response is expected to include:
-
‘data` [Array<Hash>] The current page of results.
-
‘meta` [Hash] Metadata about the response, including:
-
‘total` [Integer] The total number of available results.
-
Retrieves all paginated results from the specified API endpoint.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/lattice/client.rb', line 29 def self.paginated_get(path, params = {}) params[:offset] = params[:offset] || 0 params[:limit] = params[:limit] || 100 has_more = true all_results = [] while has_more response = get(path, params) all_results.concat(response["data"]) has_more = response["meta"]["total"] > params[:offset] + params[:limit] params[:offset] += params[:limit] end all_results end |