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:



62
63
64
# File 'lib/trophy_api_client/achievements/client.rb', line 62

def initialize(request_client:)
  @request_client = request_client
end

Instance Attribute Details

#request_clientTrophyApiClient::AsyncRequestClient (readonly)



58
59
60
# File 'lib/trophy_api_client/achievements/client.rb', line 58

def request_client
  @request_client
end

Instance Method Details

#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::DEFAULT,
  api_key: "YOUR_API_KEY"
)
api.achievements.complete(key: "finish-onboarding", user: { 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)

    • :subscribe_to_emails (Boolean)

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

Returns:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/trophy_api_client/achievements/client.rb', line 84

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(request_options: request_options)}/achievements/#{key}/complete"
    end
    TrophyApiClient::AchievementCompletionResponse.from_json(json_object: response.body)
  end
end