Class: Rublox::Client
- Inherits:
-
Object
- Object
- Rublox::Client
- 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.
Instance Attribute Summary collapse
- #http_client ⇒ HTTPClient readonly
Instance Method Summary collapse
-
#group_from_id(id) ⇒ FullGroup
A model of the group specified by the ID.
-
#initialize(roblosecurity = "") ⇒ Client
constructor
Initialize the client with a .ROBLOSECURITY cookie if you require functionality that needs it.
-
#user_from_id(id) ⇒ FullUser
A model of the user specified by the ID.
-
#user_from_username(username) ⇒ FullUser
A model of the user specified by the ID.
-
#user_presence_from_id(id) ⇒ Presence
A model of the presence specified by the user’s ID.
Constructor Details
#initialize(roblosecurity = "") ⇒ Client
Initialize the client with a .ROBLOSECURITY cookie if you require functionality that needs it.
45 46 47 |
# File 'lib/rublox.rb', line 45 def initialize(roblosecurity = "") @http_client = HTTPClient.new(roblosecurity) end |
Instance Attribute Details
#http_client ⇒ HTTPClient (readonly)
The HTTP client should only be used when there are no methods provided by the library to achieve what you want.
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.
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.
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
This method sends 2 requests, use #user_from_id if possible.
Returns a model of the user specified by the ID.
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.
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 |