Class: Rublox::Client

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

Overview

The Client object is the gateway to the API. Tt supplies methods that return classes modeled after the interactions you can do with the API.

Initialize the client with a .ROBLOSECURITY cookie if you need functionality that requires it.

Examples:

require "rublox"
# without a cookie
client = Rublox::Client.new
# with a cookie
client = Rublox::Client.new("_|WARNING:-DO-NOT-SHARE-THIS.--Sharing-this ...")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(roblosecurity = "") ⇒ Client

Initialize the client with a .ROBLOSECURITY cookie if you require functionality that needs it.

Examples:

require "rublox"
# without a cookie
client = Rublox::Client.new
# with a cookie
client = Rublox::Client.new("_|WARNING:-DO-NOT-SHARE-THIS.--Sharing-this ...")

Parameters:

  • roblosecurity (String, nil) (defaults to: "")

    a valid .ROBLOSECURITY cookie


45
46
47
# File 'lib/rublox.rb', line 45

def initialize(roblosecurity = "")
  @http_client = HTTPClient.new(roblosecurity)
end

Instance Attribute Details

#http_clientHTTPClient (readonly)

Note:

The HTTP client should only be used when there are no methods provided by the library to achieve what you want.

Returns:


34
35
36
# File 'lib/rublox.rb', line 34

def http_client
  @http_client
end

Instance Method Details

#group_from_id(id) ⇒ FullGroup

Returns a model of the group specified by the ID.

Examples:

client = Rublox::Client.new
group = client.group_from_id(1)
puts group.name # -> RobloHunks

Parameters:

  • id (Integer)

    the groups's ID

Returns:

  • (FullGroup)

    a model of the group specified by the ID


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rublox.rb', line 102

def group_from_id(id)
  group = Cache.get(Cache::GROUP, id)
  return group if group

  data = @http_client.get(
    URL.endpoint("groups", "v1/groups/#{id}")
  )
rescue Errors::UnhandledStatusCodeError
  raise Errors::GroupNotFoundError, id
else
  group = FullGroup.new(data, self)
  Cache.set(Cache::GROUP, id, group)

  group
end

#user_from_id(id) ⇒ FullUser

Returns a model of the user specified by the ID.

Examples:

client = Rublox::Client.new
user = client.user_from_id(1)
puts user.username # -> Roblox

Parameters:

  • id (Integer)

    the user's ID

Returns:

  • (FullUser)

    a model of the user specified by the ID


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rublox.rb', line 55

def user_from_id(id)
  user = Cache.get(Cache::USER, id)
  return user if user

  data = @http_client.get(
    URL.endpoint("users", "v1/users/#{id}")
  )
rescue Errors::UnhandledStatusCodeError
  raise Errors::UserNotFoundError, id
else
  user = FullUser.new(
    data,
    self
  )
  Cache.set(Cache::USER, id, user)

  user
end

#user_from_username(username) ⇒ FullUser

Note:

This method sends 2 requests, use #user_from_id if possible.

Returns a model of the user specified by the ID.

Examples:

client = Rublox::Client.new
user = client.user_from_username("Roblox")
puts user.id # -> 1

Parameters:

  • username (String)

    the user's username

Returns:

  • (FullUser)

    a model of the user specified by the ID

Raises:


81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rublox.rb', line 81

def user_from_username(username)
  data = @http_client.post(
    URL.endpoint("users", "/v1/usernames/users"),
    json: {
      usernames: [username],
      excludeBannedUsers: false
    }
  )["data"]
  raise Errors::UserNotFoundError.new(nil, username) if data.empty?

  user_from_id(
    data[0]["id"]
  )
end

#user_presence_from_id(id) ⇒ Presence

Returns a model of the presence specified by the user's ID.

Parameters:

  • id (Integer)

    the user's ID

Returns:

  • (Presence)

    a model of the presence specified by the user's ID


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rublox.rb', line 120

def user_presence_from_id(id)
  data = http_client.post(
    URL.endpoint("presence", "v1/presence/users"),
    json: {
      userIds: [id]
    }
  )
rescue Errors::UnhandledStatusCodeError
  raise Errors::PresenceNotFoundError, id
else
  Presence.new(
    data["userPresences"][0],
    self
  )
end