Class: TrophyApiClient::AsyncStreaksClient

Inherits:
Object
  • Object
show all
Defined in:
lib/trophy_api_client/streaks/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_client:) ⇒ TrophyApiClient::AsyncStreaksClient

Parameters:



98
99
100
# File 'lib/trophy_api_client/streaks/client.rb', line 98

def initialize(request_client:)
  @request_client = request_client
end

Instance Attribute Details

#request_clientTrophyApiClient::AsyncRequestClient (readonly)



94
95
96
# File 'lib/trophy_api_client/streaks/client.rb', line 94

def request_client
  @request_client
end

Instance Method Details

#list(user_ids: nil, request_options: nil) ⇒ TrophyApiClient::BULK_STREAK_RESPONSE

Get the streak lengths of a list of users, ranked by streak length from longest

to shortest.

Examples:

api = TrophyApiClient::Client.new(
  base_url: "https://api.example.com",
  environment: TrophyApiClient::Environment::PRODUCTION,
  api_key: "YOUR_API_KEY"
)
api.streaks.list

Parameters:

Returns:



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/trophy_api_client/streaks/client.rb', line 115

def list(user_ids: nil, request_options: nil)
  Async do
    response = @request_client.conn.get do |req|
      req.options.timeout = request_options.timeout_in_seconds unless request_options&.timeout_in_seconds.nil?
      req.headers["X-API-KEY"] = request_options.api_key unless request_options&.api_key.nil?
      req.headers = {
    **(req.headers || {}),
    **@request_client.get_headers,
    **(request_options&.additional_headers || {})
      }.compact
      req.params = { **(request_options&.additional_query_parameters || {}), "userIds": user_ids }.compact
      unless request_options.nil? || request_options&.additional_body_parameters.nil?
        req.body = { **(request_options&.additional_body_parameters || {}) }.compact
      end
      req.url "#{@request_client.get_url(environment: api, request_options: request_options)}/streaks"
    end
    parsed_json = JSON.parse(response.body)
    parsed_json&.map do |item|
      item = item.to_json
      TrophyApiClient::BulkStreakResponseItem.from_json(json_object: item)
    end
  end
end

#rankings(limit: nil, type: nil, request_options: nil) ⇒ Array<TrophyApiClient::StreakRankingUser>

Get the top users by streak length (active or longest).

Examples:

api = TrophyApiClient::Client.new(
  base_url: "https://api.example.com",
  environment: TrophyApiClient::Environment::PRODUCTION,
  api_key: "YOUR_API_KEY"
)
api.streaks.rankings(limit: 1, type: ACTIVE)

Parameters:

Returns:



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/trophy_api_client/streaks/client.rb', line 152

def rankings(limit: nil, type: nil, request_options: nil)
  Async do
    response = @request_client.conn.get do |req|
      req.options.timeout = request_options.timeout_in_seconds unless request_options&.timeout_in_seconds.nil?
      req.headers["X-API-KEY"] = request_options.api_key unless request_options&.api_key.nil?
      req.headers = {
    **(req.headers || {}),
    **@request_client.get_headers,
    **(request_options&.additional_headers || {})
      }.compact
      req.params = { **(request_options&.additional_query_parameters || {}), "limit": limit, "type": type }.compact
      unless request_options.nil? || request_options&.additional_body_parameters.nil?
        req.body = { **(request_options&.additional_body_parameters || {}) }.compact
      end
      req.url "#{@request_client.get_url(environment: api, request_options: request_options)}/streaks/rankings"
    end
    parsed_json = JSON.parse(response.body)
    parsed_json&.map do |item|
      item = item.to_json
      TrophyApiClient::StreakRankingUser.from_json(json_object: item)
    end
  end
end