Class: TrophyApiClient::AsyncAchievementsClient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_client:) ⇒ TrophyApiClient::AsyncAchievementsClient

Parameters:



106
107
108
# File 'lib/trophy_api_client/achievements/client.rb', line 106

def initialize(request_client:)
  @request_client = request_client
end

Instance Attribute Details

#request_clientTrophyApiClient::AsyncRequestClient (readonly)



102
103
104
# File 'lib/trophy_api_client/achievements/client.rb', line 102

def request_client
  @request_client
end

Instance Method Details

#all(user_attributes: nil, request_options: nil) ⇒ Array<TrophyApiClient::AchievementWithStatsResponse>

Get all achievements and their completion stats.

Examples:

api = TrophyApiClient::Client.new(
  base_url: "https://api.example.com",
  environment: TrophyApiClient::Environment::PRODUCTION,
  api_key: "YOUR_API_KEY"
)
api.achievements.all(user_attributes: "plan-type:premium,region:us-east")

Parameters:

  • user_attributes (String) (defaults to: nil)

    Optional colon-delimited user attributes in the format attribute:value,attribute:value. Only achievements accessible to a user with the provided attributes will be returned.

  • request_options (TrophyApiClient::RequestOptions) (defaults to: nil)

Returns:



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/trophy_api_client/achievements/client.rb', line 124

def all(user_attributes: 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 || {}),
        "userAttributes": user_attributes
      }.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)}/achievements"
    end
    parsed_json = JSON.parse(response.body)
    parsed_json&.map do |item|
      item = item.to_json
      TrophyApiClient::AchievementWithStatsResponse.from_json(json_object: item)
    end
  end
end

#complete(key:, user:, request_options: nil) ⇒ TrophyApiClient::AchievementCompletionResponse

Mark an achievement as completed for a user.

Examples:

api = TrophyApiClient::Client.new(
  base_url: "https://api.example.com",
  environment: TrophyApiClient::Environment::PRODUCTION,
  api_key: "YOUR_API_KEY"
)
api.achievements.complete(key: "finish-onboarding", user: { email: "[email protected]", tz: "Europe/London", id: "user-id" })

Parameters:

  • key (String)

    Unique reference of the achievement as set when created.

  • user (Hash)

    The user that completed the achievement.Request of type TrophyApiClient::UpsertedUser, as a Hash

    • :id (String)

    • :email (String)

    • :name (String)

    • :tz (String)

    • :device_tokens (Array<String>)

    • :subscribe_to_emails (Boolean)

    • :attributes (Hash=> String)

  • request_options (TrophyApiClient::RequestOptions) (defaults to: nil)

Returns:



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/trophy_api_client/achievements/client.rb', line 171

def complete(key:, user:, request_options: nil)
  Async do
    response = @request_client.conn.post 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
      unless request_options.nil? || request_options&.additional_query_parameters.nil?
        req.params = { **(request_options&.additional_query_parameters || {}) }.compact
      end
      req.body = { **(request_options&.additional_body_parameters || {}), user: user }.compact
      req.url "#{@request_client.get_url(environment: api,
                                         request_options: request_options)}/achievements/#{key}/complete"
    end
    TrophyApiClient::AchievementCompletionResponse.from_json(json_object: response.body)
  end
end