Class: Osu::Client

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

Overview

Client interface that caches a token to make subsequent requests with

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Client

Returns a new instance of Client


11
12
13
# File 'lib/osu/client.rb', line 11

def initialize(key)
  @key = key
end

Instance Attribute Details

#keyString (readonly)

Returns auth key

Returns:

  • (String)

    auth key


9
10
11
# File 'lib/osu/client.rb', line 9

def key
  @key
end

Instance Method Details

#beatmap(id, author: nil, mode: nil, limit: nil) ⇒ Beatmap+

Returns beatmaps matching search criteria

Parameters:

  • id (Integer)

    beatmap id

  • author (String)

    author name

  • mode (Symbol)

    game mode (see Osu::API::MODE)

  • limit (Integer)

    number of maps to return

Returns:


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/osu/client.rb', line 30

def beatmap(id, author: nil, mode: nil, limit: nil)
  payload = API::Beatmap.new(
    id: id,
    author: author,
    mode: mode,
    limit: limit
  ).execute(key)

  return if payload.empty?

  return Beatmap.new payload[0] if id && author.nil? && mode.nil? && limit.nil?
  payload.map { |e| Beatmap.new e }
end

#beatmap_score(id, user: nil, mode: nil, mods: nil, limit: nil) ⇒ Array<Score>

Parameters:

  • id (Integer)

    id of the beatmap to get scores for

  • user (String, Integer)

    filter scores by player name or ID

  • mode (Symbol)

    filter scores by game mode (see Osu::API::MODE)

  • mods (Integer, Array<Symbol>)

    mods bits or an array of mods symbols (see Osu::Mods::MODS hashmap)

  • limit (Integer)

    maximum amount of results to return

Returns:


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/osu/client.rb', line 59

def beatmap_score(id, user: nil, mode: nil, mods: nil, limit: nil)
  payload = API::BeatmapScore.new(
    id,
    user: user,
    mode: mode,
    mods: mods,
    limit: limit
  ).execute(key)

  return if payload.empty?

  payload.map do |e|
    e['beatmap_id'] = id
    Score.new(e)
  end
end

#beatmap_set(id) ⇒ Array<Beatmap>

Returns beatmaps belonging to this set

Parameters:

  • id (Integer)

    beatmap set id

Returns:

  • (Array<Beatmap>)

    beatmaps belonging to this set


46
47
48
49
50
51
# File 'lib/osu/client.rb', line 46

def beatmap_set(id)
  payload = API::Beatmap.new(set: id).execute(key)

  maps =  payload.map { |e| Beatmap.new e }
  BeatmapSet.new(maps) unless payload.empty?
end

#user(name, mode = :standard, event_days: nil) ⇒ User

Parameters:

  • name (String, Integer)

    name or ID of the user

  • mode (Symbol) (defaults to: :standard)

    game mode to pull stats for (see Osu::API::MODE)

  • event_days (Integer)

    number of days to pull event history from

Returns:


19
20
21
22
23
# File 'lib/osu/client.rb', line 19

def user(name, mode = :standard, event_days: nil)
  raise "Requested user for unknown mode '#{mode}'" unless API::MODE.include? mode
  payload = API::User.new(name, mode, event_days: event_days).execute(key)
  User.new(payload[0], mode) unless payload.empty?
end

#user_score(user, sort = :best, mode: nil, limit: nil) ⇒ Array<Score>

Parameters:

  • user (String, Integer)

    filter scores by player name or ID

  • sort (Symbol) (defaults to: :best)

    either :best or :recent

  • mode (Symbol)

    filter scores by game mode (see Osu::API::MODE)

  • limit (Integer)

    maximum amount of results to return

Returns:


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/osu/client.rb', line 81

def user_score(user, sort = :best, mode: nil, limit: nil)
  payload = if sort == :best
    API::UserBestScore.new(
      user,
      mode,
      limit: limit
    )
  elsif sort == :recent
    API::UserRecentScore.new(
      user,
      mode,
      limit: limit
    )
  else
    raise 'Sort must be either :best or :recent!'
  end

  payload = payload.execute(key)

  return if payload.empty?

  payload.map do |e|
    e['username'] = user if user.is_a? String
    Score.new(e)
  end
end