Class: TrophyApiClient::AsyncUsersClient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_client:) ⇒ TrophyApiClient::AsyncUsersClient

Parameters:



458
459
460
# File 'lib/trophy_api_client/users/client.rb', line 458

def initialize(request_client:)
  @request_client = request_client
end

Instance Attribute Details

#request_clientTrophyApiClient::AsyncRequestClient (readonly)



454
455
456
# File 'lib/trophy_api_client/users/client.rb', line 454

def request_client
  @request_client
end

Instance Method Details

#achievements(id:, include_incomplete: nil, request_options: nil) ⇒ Array<TrophyApiClient::CompletedAchievementResponse>

Get a user’s achievements.

Examples:

api = TrophyApiClient::Client.new(
  base_url: "https://api.example.com",
  environment: TrophyApiClient::Environment::DEFAULT,
  api_key: "YOUR_API_KEY"
)
api.users.achievements(id: "userId")

Parameters:

  • id (String)

    ID of the user.

  • include_incomplete (String) (defaults to: nil)

    When set to ‘true’, returns both completed and incomplete achievements for the user. When omitted or set to any other value, returns only completed achievements.

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

Returns:



754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
# File 'lib/trophy_api_client/users/client.rb', line 754

def achievements(id:, include_incomplete: 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 || {}),
        "includeIncomplete": include_incomplete
      }.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(request_options: request_options)}/users/#{id}/achievements"
    end
    parsed_json = JSON.parse(response.body)
    parsed_json&.map do |item|
      item = item.to_json
      TrophyApiClient::CompletedAchievementResponse.from_json(json_object: item)
    end
  end
end

#all_metrics(id:, request_options: nil) ⇒ Array<TrophyApiClient::MetricResponse>

Get a single user’s progress against all active metrics.

Examples:

api = TrophyApiClient::Client.new(
  base_url: "https://api.example.com",
  environment: TrophyApiClient::Environment::DEFAULT,
  api_key: "YOUR_API_KEY"
)
api.users.all_metrics(id: "userId")

Parameters:

Returns:



625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
# File 'lib/trophy_api_client/users/client.rb', line 625

def all_metrics(id:, 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
      unless request_options.nil? || request_options&.additional_query_parameters.nil?
        req.params = { **(request_options&.additional_query_parameters || {}) }.compact
      end
      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(request_options: request_options)}/users/#{id}/metrics"
    end
    parsed_json = JSON.parse(response.body)
    parsed_json&.map do |item|
      item = item.to_json
      TrophyApiClient::MetricResponse.from_json(json_object: item)
    end
  end
end

#create(request:, request_options: nil) ⇒ TrophyApiClient::User

Create a new user.

Examples:

api = TrophyApiClient::Client.new(
  base_url: "https://api.example.com",
  environment: TrophyApiClient::Environment::DEFAULT,
  api_key: "YOUR_API_KEY"
)
api.users.create(request: { id: "user-id" })

Parameters:

  • request (Hash)

    The user object.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:



481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/trophy_api_client/users/client.rb', line 481

def create(request:, 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 || {}), **(request_options&.additional_body_parameters || {}) }.compact
      req.url "#{@request_client.get_url(request_options: request_options)}/users"
    end
    TrophyApiClient::User.from_json(json_object: response.body)
  end
end

#get(id:, request_options: nil) ⇒ TrophyApiClient::User

Get a single user.

Examples:

api = TrophyApiClient::Client.new(
  base_url: "https://api.example.com",
  environment: TrophyApiClient::Environment::DEFAULT,
  api_key: "YOUR_API_KEY"
)
api.users.get(id: "userId")

Parameters:

Returns:



513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# File 'lib/trophy_api_client/users/client.rb', line 513

def get(id:, 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
      unless request_options.nil? || request_options&.additional_query_parameters.nil?
        req.params = { **(request_options&.additional_query_parameters || {}) }.compact
      end
      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(request_options: request_options)}/users/#{id}"
    end
    TrophyApiClient::User.from_json(json_object: response.body)
  end
end

#identify(id:, request:, request_options: nil) ⇒ TrophyApiClient::User

Identify a user.

Examples:

api = TrophyApiClient::Client.new(
  base_url: "https://api.example.com",
  environment: TrophyApiClient::Environment::DEFAULT,
  api_key: "YOUR_API_KEY"
)
api.users.identify(id: "id", request: { email: "[email protected]", tz: "Europe/London", attributes: { "department": "engineering", "role": "developer" } })

Parameters:

  • id (String)

    ID of the user to identify.

  • request (Hash)

    The user object.Request of type TrophyApiClient::UpdatedUser, as a Hash

    • :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:



554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# File 'lib/trophy_api_client/users/client.rb', line 554

def identify(id:, request:, request_options: nil)
  Async do
    response = @request_client.conn.put 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 || {}), **(request_options&.additional_body_parameters || {}) }.compact
      req.url "#{@request_client.get_url(request_options: request_options)}/users/#{id}"
    end
    TrophyApiClient::User.from_json(json_object: response.body)
  end
end

#metric_event_summary(id:, key:, aggregation:, start_date:, end_date:, request_options: nil) ⇒ Array<TrophyApiClient::Users::UsersMetricEventSummaryResponseItem>

Get a summary of metric events over time for a user.

Examples:

api = TrophyApiClient::Client.new(
  base_url: "https://api.example.com",
  environment: TrophyApiClient::Environment::DEFAULT,
  api_key: "YOUR_API_KEY"
)
api.users.metric_event_summary(
  id: "userId",
  key: "words-written",
  aggregation: DAILY,
  start_date: "2024-01-01",
  end_date: "2024-01-31"
)

Parameters:

  • id (String)

    ID of the user.

  • key (String)

    Unique key of the metric.

  • aggregation (TrophyApiClient::Users::UsersMetricEventSummaryRequestAggregation)

    The time period over which to aggregate the event data.

  • start_date (String)

    The start date for the data range in YYYY-MM-DD format. The startDate must be before the endDate, and the date range must not exceed 400 days.

  • end_date (String)

    The end date for the data range in YYYY-MM-DD format. The endDate must be after the startDate, and the date range must not exceed 400 days.

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

Returns:



710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
# File 'lib/trophy_api_client/users/client.rb', line 710

def metric_event_summary(id:, key:, aggregation:, start_date:, end_date:, 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 || {}),
        "aggregation": aggregation,
        "startDate": start_date,
        "endDate": end_date
      }.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(request_options: request_options)}/users/#{id}/metrics/#{key}/event-summary"
    end
    parsed_json = JSON.parse(response.body)
    parsed_json&.map do |item|
      item = item.to_json
      TrophyApiClient::Users::UsersMetricEventSummaryResponseItem.from_json(json_object: item)
    end
  end
end

#points(id:, key:, awards: nil, request_options: nil) ⇒ TrophyApiClient::GetUserPointsResponse

Get a user’s points for a specific points system.

Examples:

api = TrophyApiClient::Client.new(
  base_url: "https://api.example.com",
  environment: TrophyApiClient::Environment::DEFAULT,
  api_key: "YOUR_API_KEY"
)
api.users.points(id: "userId", key: "points-system-key")

Parameters:

  • id (String)

    ID of the user.

  • key (String)

    Key of the points system.

  • awards (Integer) (defaults to: nil)

    The number of recent point awards to return.

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

Returns:



832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
# File 'lib/trophy_api_client/users/client.rb', line 832

def points(id:, key:, awards: 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 || {}), "awards": awards }.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(request_options: request_options)}/users/#{id}/points/#{key}"
    end
    TrophyApiClient::GetUserPointsResponse.from_json(json_object: response.body)
  end
end

#points_event_summary(id:, key:, aggregation:, start_date:, end_date:, request_options: nil) ⇒ Array<TrophyApiClient::Users::UsersPointsEventSummaryResponseItem>

Get a summary of points awards over time for a user for a specific points

system.

Examples:

api = TrophyApiClient::Client.new(
  base_url: "https://api.example.com",
  environment: TrophyApiClient::Environment::DEFAULT,
  api_key: "YOUR_API_KEY"
)
api.users.points_event_summary(
  id: "userId",
  key: "points-system-key",
  aggregation: DAILY,
  start_date: "2024-01-01",
  end_date: "2024-01-31"
)

Parameters:

  • id (String)

    ID of the user.

  • key (String)

    Key of the points system.

  • aggregation (TrophyApiClient::Users::UsersPointsEventSummaryRequestAggregation)

    The time period over which to aggregate the event data.

  • start_date (String)

    The start date for the data range in YYYY-MM-DD format. The startDate must be before the endDate, and the date range must not exceed 400 days.

  • end_date (String)

    The end date for the data range in YYYY-MM-DD format. The endDate must be after the startDate, and the date range must not exceed 400 days.

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

Returns:



877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
# File 'lib/trophy_api_client/users/client.rb', line 877

def points_event_summary(id:, key:, aggregation:, start_date:, end_date:, 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 || {}),
        "aggregation": aggregation,
        "startDate": start_date,
        "endDate": end_date
      }.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(request_options: request_options)}/users/#{id}/points/#{key}/event-summary"
    end
    parsed_json = JSON.parse(response.body)
    parsed_json&.map do |item|
      item = item.to_json
      TrophyApiClient::Users::UsersPointsEventSummaryResponseItem.from_json(json_object: item)
    end
  end
end

#single_metric(id:, key:, request_options: nil) ⇒ TrophyApiClient::MetricResponse

Get a user’s progress against a single active metric.

Examples:

api = TrophyApiClient::Client.new(
  base_url: "https://api.example.com",
  environment: TrophyApiClient::Environment::DEFAULT,
  api_key: "YOUR_API_KEY"
)
api.users.single_metric(id: "userId", key: "key")

Parameters:

Returns:



664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
# File 'lib/trophy_api_client/users/client.rb', line 664

def single_metric(id:, key:, 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
      unless request_options.nil? || request_options&.additional_query_parameters.nil?
        req.params = { **(request_options&.additional_query_parameters || {}) }.compact
      end
      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(request_options: request_options)}/users/#{id}/metrics/#{key}"
    end
    TrophyApiClient::MetricResponse.from_json(json_object: response.body)
  end
end

#streak(id:, history_periods: nil, request_options: nil) ⇒ TrophyApiClient::StreakResponse

Get a user’s streak data.

Examples:

api = TrophyApiClient::Client.new(
  base_url: "https://api.example.com",
  environment: TrophyApiClient::Environment::DEFAULT,
  api_key: "YOUR_API_KEY"
)
api.users.streak(id: "userId")

Parameters:

  • id (String)

    ID of the user.

  • history_periods (Integer) (defaults to: nil)

    The number of past streak periods to include in the streakHistory field of the response.

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

Returns:



795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
# File 'lib/trophy_api_client/users/client.rb', line 795

def streak(id:, history_periods: 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 || {}),
        "historyPeriods": history_periods
      }.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(request_options: request_options)}/users/#{id}/streak"
    end
    TrophyApiClient::StreakResponse.from_json(json_object: response.body)
  end
end

#update(id:, request:, request_options: nil) ⇒ TrophyApiClient::User

Update a user.

Examples:

api = TrophyApiClient::Client.new(
  base_url: "https://api.example.com",
  environment: TrophyApiClient::Environment::DEFAULT,
  api_key: "YOUR_API_KEY"
)
api.users.update(id: "id", request: { email: "[email protected]", tz: "Europe/London", attributes: { "department": "engineering", "role": "developer" } })

Parameters:

  • id (String)

    ID of the user to update.

  • request (Hash)

    The user object.Request of type TrophyApiClient::UpdatedUser, as a Hash

    • :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:



593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# File 'lib/trophy_api_client/users/client.rb', line 593

def update(id:, request:, request_options: nil)
  Async do
    response = @request_client.conn.patch 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 || {}), **(request_options&.additional_body_parameters || {}) }.compact
      req.url "#{@request_client.get_url(request_options: request_options)}/users/#{id}"
    end
    TrophyApiClient::User.from_json(json_object: response.body)
  end
end