rublox 0.1.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,34 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "time"
4
-
5
- require "rublox/derive/user"
6
-
7
- module Rublox
8
- # @note This class is handled internally by the public interface such as
9
- # {FullGroup#owner}. You should not be creating it yourself.
10
- # The {LimitedUser} is an user containing less information. It exists as some
11
- # API's do not return full user information, and I decided to not send extra
12
- # requests. You can call {#refresh} to get a {FullUser} if needed.
13
- class LimitedUser
14
- include User
15
-
16
- # (see FullUser#id)
17
- attr_reader :id
18
-
19
- # (see FullUser#username)
20
- attr_reader :username
21
-
22
- # (see FullUser#display_name)
23
- attr_reader :display_name
24
-
25
- # (see FullUser#initialize)
26
- def initialize(data, client)
27
- @id = data["id"] || data["userId"] || data["builderId"]
28
- @username = data["name"] || data["username"] || data["builder"]
29
- @display_name = data["displayName"] || data["DisplayName"]
30
-
31
- @client = client
32
- end
33
- end
34
- end
@@ -1,39 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Rublox
4
- # @note Only use if you have an use case that the library doesn't cover (and
5
- # create an issue and perhaps we'll implement it!).
6
- module Cache
7
- # The key for the user cache.
8
- USER = :users
9
- # The key for the group cache.
10
- GROUP = :groups
11
- # The key for the page cache.
12
- PAGE = :pages
13
-
14
- # @!visiblity private
15
- @cache = {
16
- users: {},
17
- groups: {},
18
- pages: {}
19
- }
20
-
21
- # Try to get an object from cache.
22
- # @param type [Symbol] {USER}, {GROUP} or {PAGE}
23
- # @param id [Integer] the ID of the object
24
- # @return [FullUser, FullGroup, Pages, nil]
25
- def self.get(type, id)
26
- @cache[type][id]
27
- end
28
-
29
- # Set an object in the cache, under the type's key.
30
- # @param type [Symbol] {USER}, {GROUP} or {PAGE}
31
- # @param id [Integer] the ID of the object
32
- # @param object [FullUser, FullGroup, Pages] the object to be added to the
33
- # cache
34
- # @return [nil]
35
- def self.set(type, id, object)
36
- @cache[type][id] = object
37
- end
38
- end
39
- end
@@ -1,95 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "http"
4
- require "json"
5
-
6
- require "rublox/util/errors"
7
-
8
- module Rublox
9
- # @note Only use if you have an use case that the library doesn't cover (and
10
- # create an issue and perhaps we'll implement it!).
11
- # The {HTTPClient} is a wrapper around the http library, designed specifically
12
- # for the Roblox API. It automatically handles the X-CSRF-TOKEN and the
13
- # .ROBLOSECURITY cookie.
14
- class HTTPClient
15
- # @param roblosecurity [String]
16
- def initialize(roblosecurity = "")
17
- @client = HTTP.cookies(
18
- {
19
- ".ROBLOSECURITY": roblosecurity
20
- }
21
- )
22
- end
23
-
24
- # Send a GET request to the specified URL.
25
- # @param url [String]
26
- # @return [Hash]
27
- def get(url, *args)
28
- request(:get, url, *args)
29
- end
30
-
31
- # Send a POST request to the specified URL.
32
- # @param url [String]
33
- # @return [Hash]
34
- def post(url, *args)
35
- request(:post, url, *args)
36
- end
37
-
38
- # Send a PATCH request to the specified URL.
39
- # @param url [String]
40
- # @return [Hash]
41
- def patch(url, *args)
42
- request(:patch, url, *args)
43
- end
44
-
45
- # Send a DELETE request to the specified URL.
46
- # @param url [String]
47
- # @return [Hash]
48
- def delete(url, *args)
49
- request(:delete, url, *args)
50
- end
51
-
52
- private
53
-
54
- # @param verb [Symbol]
55
- # @param url [String]
56
- # @return [Hash]
57
- def request(verb, url, *args)
58
- response = @client.request(verb, url, *args)
59
- return JSON.parse(response.body) if response.status == 200
60
-
61
- handle_status_code(response, verb, url, *args)
62
- end
63
-
64
- # @param response [HTTP::Response]
65
- def handle_status_code(response, verb, url, *args)
66
- case response.status
67
- # token validation failed
68
- when 403
69
- @client = @client.headers(
70
- {
71
- "x-csrf-token": response.headers["x-csrf-token"]
72
- }
73
- )
74
- # retry the request
75
- request(verb, url, *args)
76
- # invalid .ROBLOSECURITY cookie
77
- when 401
78
- raise Errors::InvalidROBLOSECURITYError
79
- else
80
- raise Errors::UnhandledStatusCodeError.new(response.status, get_errors_from_response(response))
81
- end
82
- end
83
-
84
- # @param response [HTTP::Response]
85
- def get_errors_from_response(response)
86
- body = JSON.parse(response.body)
87
- rescue JSON::ParserError
88
- "\ncould not parse errors, raw body:\n#{response.body}\n"
89
- else
90
- body["errors"].reduce("") do |error_message, error|
91
- error_message + "\tCode: #{error['code']}\n\tMessage: #{error['message']}\n\n"
92
- end
93
- end
94
- end
95
- end
@@ -1,85 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "rublox/util/url"
4
- require "rublox/util/cache"
5
-
6
- module Rublox
7
- # The order to sort pages in.
8
- module SortOrder
9
- # Sort the pages ina ascending order.
10
- ASCENDING = "Asc"
11
- # Sort the pages in descending order.
12
- DESCENDING = "Desc"
13
- end
14
-
15
- # The {Pages} class acts as an iterator over pages returned by the API.
16
- # @!visibility private
17
- # @todo Make more customizable, polish and battle-test
18
- # @note Only use if you have an use case that the library doesn't cover (and
19
- # create an issue and perhaps we'll implement it!).
20
- class Pages
21
- # How many items are returned per page
22
- PAGE_DATA_LIMIT = 100
23
-
24
- # @!visibility private
25
- DEFAULT_REQUEST_PARAMETERS = {
26
- params: {
27
- sortOrder: SortOrder::DESCENDING,
28
- limit: PAGE_DATA_LIMIT
29
- }
30
- }.freeze
31
-
32
- # @param client [Client]
33
- # @param initial_data [Hash]
34
- # @param url [String]
35
- # @param request_params [Hash]
36
- def initialize(client, initial_data, url, request_params = {}, &data_handler)
37
- request_params[:params] = DEFAULT_REQUEST_PARAMETERS[:params].merge(
38
- request_params
39
- )
40
-
41
- @client = client
42
- @raw_data = initial_data
43
- @url = url
44
- @request_parameters = request_parameters
45
- @data_handler = data_handler
46
-
47
- @data = []
48
- end
49
-
50
- # Iterate over the pages
51
- def each
52
- raise unless block_given?
53
- # i = 0
54
-
55
- # until @raw_data
56
- # if i == @data.length
57
- # i = 0
58
- # data = Cache.get(Cache::PAGE, @raw_data["nextPageCursor"])
59
- # if data
60
- # @data = data
61
- # else
62
- # @raw_data, @data = next_page
63
- # break if @raw_data.empty?
64
-
65
- # Cache.set(Cache::PAGE, @raw_data["nextPageCursor"], @data)
66
- # end
67
- # end
68
-
69
- # yield @data[i]
70
- # i += 1
71
- # end
72
- end
73
-
74
- private
75
-
76
- # @return [Array] tuple {raw data, processed data}
77
- def next_page
78
- @request_parameters[:params][:cursor] = @raw_data["nextPageCursor"]
79
-
80
- data = @client.http_client.get(@url, @request_parameters)
81
-
82
- [data["data"], @data_handler.call(data["data"])]
83
- end
84
- end
85
- end
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Rublox
4
- # Module for making URLs.
5
- module URL
6
- # The base URL to be used. If using a proxy, you can change the URL as long
7
- # as it follows Roblox's API structure:
8
- # https://users.roblox.com/v1/users/1
9
- # Bad:
10
- # https://myproxy.test/get-user/1
11
- # https://myproxy.test/users/1
12
- # https://users.myproxy.test/users/1
13
- # Good:
14
- # https://users.myproxy.test/v1/users/1
15
- BASE_URL = "roblox.com"
16
-
17
- # Creates an endpoint URL from the the given ApiSite and path.
18
- # @param api_site [String] ApiSite
19
- # @param path [String]
20
- # @return [String] the endpoint
21
- def self.endpoint(api_site, path)
22
- "https://#{api_site}.#{BASE_URL}/#{path}"
23
- end
24
- end
25
- end
@@ -1,6 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Rublox
4
- # Current version of rublox
5
- VERSION = "0.1.0"
6
- end