rawg_rb 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.rspec +1 -0
- data/.rubocop.yml +14 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +75 -0
- data/LICENSE.txt +21 -0
- data/README.md +73 -0
- data/Rakefile +4 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/examples/example.rb +136 -0
- data/lib/rawg.rb +7 -0
- data/lib/rawg/client.rb +106 -0
- data/lib/rawg/collection.rb +58 -0
- data/lib/rawg/game.rb +31 -0
- data/lib/rawg/utils.rb +35 -0
- data/lib/rawg/version.rb +5 -0
- data/rawg_rb.gemspec +49 -0
- metadata +181 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 13396908ead644d462b48d9d0c61cf5fa091b4684710b9a2bfe26af6b3ebd7df
|
4
|
+
data.tar.gz: adc1b93decf1429fddfdc7e7dd9202364d091d1e6ed7a61b8cea5bf09654c076
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1fab65be1610075248e38d14d571bbdc4beb1b7237aed00d4b9f56e91b69a7886071470864358379db842257dc3c9a281d62609d72757b2b2cdcb98b836079ea
|
7
|
+
data.tar.gz: 13f37a249b8c4079f6334bff381958ed94fd6ec3f3eebbd2ab2d58b0be3cf9797fff4cf2eeb0daa5cda34bce3ff4c272d87cf4739e5a7287db2eb5813c33dba7
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/.rubocop.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rawg_rb (0.0.0)
|
5
|
+
faraday (~> 0.15.4)
|
6
|
+
faraday_middleware (~> 0.13.1)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activesupport (5.2.3)
|
12
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
13
|
+
i18n (>= 0.7, < 2)
|
14
|
+
minitest (~> 5.1)
|
15
|
+
tzinfo (~> 1.1)
|
16
|
+
addressable (2.6.0)
|
17
|
+
public_suffix (>= 2.0.2, < 4.0)
|
18
|
+
coderay (1.1.2)
|
19
|
+
concurrent-ruby (1.1.5)
|
20
|
+
crack (0.4.3)
|
21
|
+
safe_yaml (~> 1.0.0)
|
22
|
+
diff-lcs (1.3)
|
23
|
+
factory_bot (5.0.2)
|
24
|
+
activesupport (>= 4.2.0)
|
25
|
+
faraday (0.15.4)
|
26
|
+
multipart-post (>= 1.2, < 3)
|
27
|
+
faraday_middleware (0.13.1)
|
28
|
+
faraday (>= 0.7.4, < 1.0)
|
29
|
+
hashdiff (0.4.0)
|
30
|
+
i18n (1.6.0)
|
31
|
+
concurrent-ruby (~> 1.0)
|
32
|
+
method_source (0.9.2)
|
33
|
+
minitest (5.11.3)
|
34
|
+
multipart-post (2.1.1)
|
35
|
+
pry (0.12.2)
|
36
|
+
coderay (~> 1.1.0)
|
37
|
+
method_source (~> 0.9.0)
|
38
|
+
public_suffix (3.1.0)
|
39
|
+
rake (10.5.0)
|
40
|
+
rspec (3.8.0)
|
41
|
+
rspec-core (~> 3.8.0)
|
42
|
+
rspec-expectations (~> 3.8.0)
|
43
|
+
rspec-mocks (~> 3.8.0)
|
44
|
+
rspec-core (3.8.1)
|
45
|
+
rspec-support (~> 3.8.0)
|
46
|
+
rspec-expectations (3.8.4)
|
47
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
48
|
+
rspec-support (~> 3.8.0)
|
49
|
+
rspec-mocks (3.8.1)
|
50
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
51
|
+
rspec-support (~> 3.8.0)
|
52
|
+
rspec-support (3.8.2)
|
53
|
+
safe_yaml (1.0.5)
|
54
|
+
thread_safe (0.3.6)
|
55
|
+
tzinfo (1.2.5)
|
56
|
+
thread_safe (~> 0.1)
|
57
|
+
webmock (3.6.0)
|
58
|
+
addressable (>= 2.3.6)
|
59
|
+
crack (>= 0.3.2)
|
60
|
+
hashdiff (>= 0.4.0, < 2.0.0)
|
61
|
+
|
62
|
+
PLATFORMS
|
63
|
+
ruby
|
64
|
+
|
65
|
+
DEPENDENCIES
|
66
|
+
bundler (~> 2.0)
|
67
|
+
factory_bot (~> 5.0, >= 5.0.2)
|
68
|
+
pry (~> 0.12.2)
|
69
|
+
rake (~> 10.0)
|
70
|
+
rawg_rb!
|
71
|
+
rspec (~> 3.8)
|
72
|
+
webmock (~> 3.6)
|
73
|
+
|
74
|
+
BUNDLED WITH
|
75
|
+
2.0.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Ivan Lobko
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# 💎 RAWG.rb
|
2
|
+
|
3
|
+
This is a Ruby client for RAWG API.
|
4
|
+
|
5
|
+
[RAWG.io](https://rawg.io) is the largest video game database and game discovery service. See [rawg.io/apidocs](https://rawg.io/apidocs) for rules and additional information about the API.
|
6
|
+
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'rawg_rb'
|
14
|
+
```
|
15
|
+
|
16
|
+
Or install manually:
|
17
|
+
|
18
|
+
$ gem install rawg_rb
|
19
|
+
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
# You should provide a user agent to identify your app.
|
25
|
+
rawg = RAWG::Client.new(user_agent: 'MyAwesomeApp/1.0')
|
26
|
+
|
27
|
+
# Fetch a game by slug or id
|
28
|
+
rawg.game('fallout')
|
29
|
+
rawg.game(132026)
|
30
|
+
|
31
|
+
minecraft = rawg.game('minecraft') # => <RAWG::Game>
|
32
|
+
minecraft.id # => 22509
|
33
|
+
minecraft.slug # => "minecraft"
|
34
|
+
minecraft.name # => "Minecraft"
|
35
|
+
minecraft.description # => "<p>One of the most popular games of the 2010s...
|
36
|
+
minecraft.website # => "https://minecraft.net"
|
37
|
+
minecraft.rating # => 4.32
|
38
|
+
|
39
|
+
# Find similar games
|
40
|
+
games_like_minecraft = minecraft.suggested # => <RAWG::Collection>
|
41
|
+
|
42
|
+
# Collection is Enumerable.
|
43
|
+
# If the result contains multiple pages, additional pages will be requested if needed.
|
44
|
+
games_like_minecraft.first(2).map(&:name) # => ['Project Explore', 'Planet Nomads']
|
45
|
+
|
46
|
+
# Search games
|
47
|
+
rawg.games(search: 'gta') # => <RAWG::Collection>
|
48
|
+
rawg.games(search: 'zombie', genres: 'racing') # => <RAWG::Collection>
|
49
|
+
|
50
|
+
```
|
51
|
+
|
52
|
+
|
53
|
+
### Simple Requests
|
54
|
+
|
55
|
+
These methods return a hash containing API response as it is.
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
rawg.all_games(genres: 'indie')
|
59
|
+
rawg.search_games('zombie', genres: 'racing')
|
60
|
+
rawg.search_games(genres: 'indie')
|
61
|
+
rawg.game_info(132026)
|
62
|
+
rawg.game_suggest('minecraft')
|
63
|
+
```
|
64
|
+
|
65
|
+
|
66
|
+
## Contributing
|
67
|
+
|
68
|
+
Feel free to submit new issues at [github.com/ivanlobko/rawg.rb/issues](https://github.com/ivanlobko/rawg.rb/issues).
|
69
|
+
|
70
|
+
|
71
|
+
## License
|
72
|
+
|
73
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'rawg'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/examples/example.rb
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rawg'
|
4
|
+
|
5
|
+
rawg = RAWG::Client.new(user_agent: 'Game Finder', page_size: 40)
|
6
|
+
|
7
|
+
rawg.all_games(genres: 'indie') # Done
|
8
|
+
|
9
|
+
# Searches
|
10
|
+
# /api/games?search=gta
|
11
|
+
rawg.search_games('gta') # Done
|
12
|
+
# /api/games?search=civ&genres=strategy
|
13
|
+
rawg.search_games('civ', genres: 'strategy') # Done
|
14
|
+
# /api/games?search=zombie&genres=racing,sports
|
15
|
+
rawg.search_games('zombie', genres: ['racing','sports']) # Done
|
16
|
+
rawg.search_users('anton') # Done
|
17
|
+
# rawg.search_developers('nintendo') # Skip
|
18
|
+
|
19
|
+
# Games
|
20
|
+
rawg.game(12343) # Done
|
21
|
+
rawg.game('minecraft') # Done
|
22
|
+
rawg.game_suggest('minecraft') # Done
|
23
|
+
rawg.game_reviews('factorio') # Done
|
24
|
+
|
25
|
+
rawg.games.count #
|
26
|
+
rawg.games(genres: 'racing').count #
|
27
|
+
rawg
|
28
|
+
.games(genres: 'racing')
|
29
|
+
.count { |g| g.name.contains('zombie') } #
|
30
|
+
rawg.games(search: 'zombie', genres: 'racing').count #
|
31
|
+
rawg.users(1)
|
32
|
+
.games(search: 'rally', statuses: 'playing')
|
33
|
+
.map { |g| g.stores.contains?(:steam) } #
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
# Users
|
38
|
+
rawg.user(1) # Done
|
39
|
+
rawg.user('orels1') # Done
|
40
|
+
rawg.user_games('orels1') # Done
|
41
|
+
# /api/users/orels1/games?statuses=owned
|
42
|
+
rawg.user_games('orels1', statuses: 'owned') # Done
|
43
|
+
rawg.user_reviews('orels1') # Done
|
44
|
+
# rawg.user_followers('orels1') # Skip
|
45
|
+
# rawg.user_collections('orels1') # Skip
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
# Users
|
53
|
+
|
54
|
+
# Get all users
|
55
|
+
rawg.users
|
56
|
+
|
57
|
+
# Get total number of users
|
58
|
+
rawg.users.count
|
59
|
+
|
60
|
+
# Search user by username, full_name, etc.
|
61
|
+
# /api/users?search={arg}
|
62
|
+
rawg.users.search('anton')
|
63
|
+
rawg.users.search('anton').count
|
64
|
+
rawg.users.search('anton').map(&:name)
|
65
|
+
rawg.users.search('anton').first
|
66
|
+
|
67
|
+
# Find user by id or username
|
68
|
+
# /api/users/<id or username>
|
69
|
+
rawg.users.find('orels1')
|
70
|
+
rawg.users.find(9)
|
71
|
+
rawg.users.find('orels1').games(statuses: 'playing')
|
72
|
+
rawg.users.find('orels1').games.playing.first
|
73
|
+
rawg.users.find('orels1').games.with(statuses: 'playing').map(&:name)
|
74
|
+
|
75
|
+
# User's games
|
76
|
+
# /api/users/<id or username>/games
|
77
|
+
rawg.users.find(9).games # => Enumerator
|
78
|
+
rawg.users.find(9).games.to_a
|
79
|
+
rawg.users.find(9).games.count
|
80
|
+
rawg.users.find(9).games.first(100)
|
81
|
+
|
82
|
+
# User's reviews
|
83
|
+
# /api/users/<id or username>/reviews
|
84
|
+
rawg.users.find(9).reviews # => Enumerator
|
85
|
+
rawg.users.find(9).reviews.to_a
|
86
|
+
rawg.users.find(9).reviews.count
|
87
|
+
rawg.users.find(9).reviews.filter { |r| r.is_text == true }
|
88
|
+
|
89
|
+
rawg.users.find(9).followers.count
|
90
|
+
|
91
|
+
# Games
|
92
|
+
|
93
|
+
# /api/games?genres=strategy
|
94
|
+
rawg.games.with(genres: 'strategy').count
|
95
|
+
|
96
|
+
# Search games
|
97
|
+
# /api/games?search={param}
|
98
|
+
rawg.games.search('red')
|
99
|
+
|
100
|
+
# Find game by id or slug
|
101
|
+
rawg.games.find('factorio')
|
102
|
+
|
103
|
+
|
104
|
+
rawg.games # => Enumerator
|
105
|
+
rawg.games # => Games
|
106
|
+
rawg.games.select { |g| g.genres.include?(Genre.new(:racing)) }
|
107
|
+
rawg.games.search('bla bla') # => Games
|
108
|
+
rawg.games.search('bla bla'). # => Games
|
109
|
+
|
110
|
+
|
111
|
+
|
112
|
+
# Проблема
|
113
|
+
# Мы хотим посчитать все игры с жанром 'racing'
|
114
|
+
# Нижеследующая строка скачает все игры и посчитает жанры в Руби:
|
115
|
+
rawg.games.count { |g| g.genres.include?(:racing) }
|
116
|
+
# А нужно, чтобы клиент сделал запрос /api/games?genres=racing и вернул response[:count]:
|
117
|
+
rawg.all_games(genres: 'racing')[:count]
|
118
|
+
# Может сделать так? Вроде так более очевидно, где мы управляем запросом, а где кодим на Руби
|
119
|
+
rawg.games(genres: 'racing').count
|
120
|
+
# Проверим на других примерах:
|
121
|
+
rawg.games.count
|
122
|
+
rawg.games(genres: 'racing').count { |g| g.name.contains('zombie') } # пагинация, работает Руби
|
123
|
+
rawg.games(search: 'zombie', genres: 'racing').count # работает запрос
|
124
|
+
rawg.users(1).games(search: 'rally', statuses: 'playing').map { |g| g.stores.contains?(:steam) }
|
125
|
+
rawg.games('factorio')
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
# Developers
|
130
|
+
|
131
|
+
# Search developers
|
132
|
+
rawg.developers.search('valve')
|
133
|
+
|
134
|
+
# Find developer
|
135
|
+
rawg.developers.find('valve-software')
|
136
|
+
rawg.developers.find('valve-software').top_games.first
|
data/lib/rawg.rb
ADDED
data/lib/rawg/client.rb
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'faraday'
|
4
|
+
require 'faraday_middleware'
|
5
|
+
|
6
|
+
module RAWG
|
7
|
+
class Client
|
8
|
+
DEFAULT_USER_AGENT = "rawg.rb/#{RAWG::VERSION}"
|
9
|
+
BASE_URL = 'https://api.rawg.io'
|
10
|
+
|
11
|
+
attr_reader :user_agent
|
12
|
+
|
13
|
+
def initialize(user_agent: nil)
|
14
|
+
@user_agent = combine_user_agents(user_agent, DEFAULT_USER_AGENT)
|
15
|
+
@http_client = default_http_client
|
16
|
+
end
|
17
|
+
|
18
|
+
def get(path, query = {})
|
19
|
+
response = @http_client.get(path, format_query(query)).body
|
20
|
+
return nil unless response.is_a?(Hash)
|
21
|
+
return nil if response[:detail] == 'Not found.'
|
22
|
+
|
23
|
+
response
|
24
|
+
end
|
25
|
+
|
26
|
+
def all_games(options = {})
|
27
|
+
get('/api/games', options)
|
28
|
+
end
|
29
|
+
|
30
|
+
def all_users(options = {})
|
31
|
+
get('/api/users', options)
|
32
|
+
end
|
33
|
+
|
34
|
+
def search_games(query, options = {})
|
35
|
+
all_games(search: query, **options)
|
36
|
+
end
|
37
|
+
|
38
|
+
def search_users(query, options = {})
|
39
|
+
all_users(search: query, **options)
|
40
|
+
end
|
41
|
+
|
42
|
+
def game_info(game)
|
43
|
+
get("/api/games/#{game}")
|
44
|
+
end
|
45
|
+
|
46
|
+
def game_suggest(game, options = {})
|
47
|
+
get("/api/games/#{game}/suggested", options)
|
48
|
+
end
|
49
|
+
|
50
|
+
def game_reviews(game, options = {})
|
51
|
+
get("/api/games/#{game}/reviews", options)
|
52
|
+
end
|
53
|
+
|
54
|
+
def user_info(user)
|
55
|
+
get("/api/users/#{user}")
|
56
|
+
end
|
57
|
+
|
58
|
+
def user_games(user, options = {})
|
59
|
+
get("/api/users/#{user}/games", options)
|
60
|
+
end
|
61
|
+
|
62
|
+
def user_reviews(user, options = {})
|
63
|
+
get("/api/users/#{user}/reviews", options)
|
64
|
+
end
|
65
|
+
|
66
|
+
def game(game)
|
67
|
+
response = game_info(game)
|
68
|
+
RAWG::Game.new(client: self).from_api_response(response)
|
69
|
+
end
|
70
|
+
|
71
|
+
def games(options = {})
|
72
|
+
response = all_games(options)
|
73
|
+
RAWG::Collection.new(RAWG::Game, client: self).from_api_response(response)
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
def combine_user_agents(*user_agents)
|
79
|
+
user_agents.join(' ').squeeze(' ').strip
|
80
|
+
end
|
81
|
+
|
82
|
+
def default_http_client
|
83
|
+
Faraday.new(
|
84
|
+
url: BASE_URL,
|
85
|
+
headers: { user_agent: @user_agent, content_type: 'application/json' }
|
86
|
+
) do |conn|
|
87
|
+
conn.response :json, content_type: /\bjson$/,
|
88
|
+
parser_options: { symbolize_names: true }
|
89
|
+
conn.adapter Faraday.default_adapter
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def format_query(query)
|
94
|
+
query
|
95
|
+
.map { |field, value| [field, serialize_query_value(value)] }
|
96
|
+
.to_h
|
97
|
+
.compact
|
98
|
+
end
|
99
|
+
|
100
|
+
def serialize_query_value(value)
|
101
|
+
return value unless value.is_a? Array
|
102
|
+
|
103
|
+
value.join(',')
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RAWG
|
4
|
+
class Collection
|
5
|
+
include Enumerable
|
6
|
+
|
7
|
+
attr_reader :items_class, :client
|
8
|
+
|
9
|
+
def initialize(klass, client: RAWG::Client.new)
|
10
|
+
@items_class = klass
|
11
|
+
@client = client
|
12
|
+
@count = 0
|
13
|
+
@items = []
|
14
|
+
@next_page_url = nil
|
15
|
+
end
|
16
|
+
|
17
|
+
def from_api_response(response)
|
18
|
+
@next_page_url = response[:next]
|
19
|
+
@count = response[:count]
|
20
|
+
response[:results].each do |attrs|
|
21
|
+
new_item = @items_class.new(client: @client).from_api_response(attrs)
|
22
|
+
@items.push(new_item)
|
23
|
+
end
|
24
|
+
self
|
25
|
+
end
|
26
|
+
|
27
|
+
def each
|
28
|
+
i = 0
|
29
|
+
loop do
|
30
|
+
raise StopIteration if i == @count - 1
|
31
|
+
|
32
|
+
if !@items[i] && @next_page_url
|
33
|
+
response = @client.get(@next_page_url)
|
34
|
+
from_api_response(response)
|
35
|
+
end
|
36
|
+
|
37
|
+
yield @items[i]
|
38
|
+
i += 1
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def count(*args)
|
43
|
+
return super if !args.empty? || block_given?
|
44
|
+
|
45
|
+
@count
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def fetch_next_page
|
51
|
+
# response = @client.get(@next_page_url)
|
52
|
+
# fetched_items = response[:results].map do |item|
|
53
|
+
# @items_class.new(client: @client).from_api_response(item)
|
54
|
+
# end
|
55
|
+
# @items.push(*fetched_items)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/lib/rawg/game.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative './utils'
|
4
|
+
|
5
|
+
module RAWG
|
6
|
+
class Game
|
7
|
+
include RAWG::Utils
|
8
|
+
|
9
|
+
lazy_attr_accessor :id, :slug, :name, :description,
|
10
|
+
:released, :website, :rating,
|
11
|
+
init: ->(_) { @client.game_info(@id) }
|
12
|
+
|
13
|
+
def initialize(options = {})
|
14
|
+
@client = options[:client] || RAWG::Client.new
|
15
|
+
assign_attributes(options)
|
16
|
+
yield self if block_given?
|
17
|
+
end
|
18
|
+
|
19
|
+
def from_api_response(response)
|
20
|
+
assign_attributes(response)
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
def suggested(options)
|
25
|
+
response = client.game_suggest(@id, options)
|
26
|
+
RAWG::Collection
|
27
|
+
.new(RAWG::Game, client: client)
|
28
|
+
.from_api_response(response)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/rawg/utils.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RAWG
|
4
|
+
module Utils
|
5
|
+
def self.included(base)
|
6
|
+
base.extend(ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def lazy_attr_accessor(*attrs, init:)
|
11
|
+
attr_writer(*attrs)
|
12
|
+
lazy_attr_reader(*attrs, init: init)
|
13
|
+
end
|
14
|
+
|
15
|
+
def lazy_attr_reader(*attrs, init:)
|
16
|
+
attrs.each do |attr|
|
17
|
+
define_method(attr) do
|
18
|
+
instance_variable_get("@#{attr}") || begin
|
19
|
+
init.call(attr)
|
20
|
+
instance_variable_get("@#{attr}")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def assign_attributes(attrs)
|
28
|
+
return unless attrs.is_a?(Hash)
|
29
|
+
|
30
|
+
attrs.each do |attr, value|
|
31
|
+
send("#{attr}=", value) if respond_to?("#{attr}=")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/rawg/version.rb
ADDED
data/rawg_rb.gemspec
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'rawg/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'rawg_rb'
|
9
|
+
spec.version = RAWG::VERSION
|
10
|
+
spec.authors = ['Ivan Lobko']
|
11
|
+
spec.email = ['ivan@lobko.com']
|
12
|
+
spec.summary = 'A Ruby client for RAWG.io API'
|
13
|
+
spec.homepage = 'https://github.com/ivanlobko/rawg.rb'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
if spec.respond_to?(:metadata)
|
19
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
20
|
+
|
21
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
22
|
+
spec.metadata['source_code_uri'] = 'https://github.com/ivanlobko/rawg.rb'
|
23
|
+
# spec.metadata['changelog_uri'] = "TODO: Put your gem's CHANGELOG.md URL here."
|
24
|
+
else
|
25
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
26
|
+
'public gem pushes.'
|
27
|
+
end
|
28
|
+
|
29
|
+
# Specify which files should be added to the gem when it is released.
|
30
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
31
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
32
|
+
`git ls-files -z`
|
33
|
+
.split("\x0")
|
34
|
+
.reject { |f| f.match(%r{^(test|spec|features)/}) }
|
35
|
+
end
|
36
|
+
spec.bindir = 'exe'
|
37
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
38
|
+
spec.require_paths = ['lib']
|
39
|
+
|
40
|
+
spec.add_dependency 'faraday', '~> 0.15.4'
|
41
|
+
spec.add_dependency 'faraday_middleware', '~> 0.13.1'
|
42
|
+
|
43
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
44
|
+
spec.add_development_dependency 'factory_bot', '~> 5.0', '>= 5.0.2'
|
45
|
+
spec.add_development_dependency 'pry', '~> 0.12.2'
|
46
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
47
|
+
spec.add_development_dependency 'rspec', '~> 3.8'
|
48
|
+
spec.add_development_dependency 'webmock', '~> 3.6'
|
49
|
+
end
|
metadata
ADDED
@@ -0,0 +1,181 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rawg_rb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ivan Lobko
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-06-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.15.4
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.15.4
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: faraday_middleware
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.13.1
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.13.1
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: factory_bot
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.0'
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: 5.0.2
|
65
|
+
type: :development
|
66
|
+
prerelease: false
|
67
|
+
version_requirements: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - "~>"
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '5.0'
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 5.0.2
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: pry
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - "~>"
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: 0.12.2
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - "~>"
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: 0.12.2
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: rake
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - "~>"
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '10.0'
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - "~>"
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '10.0'
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
name: rspec
|
105
|
+
requirement: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - "~>"
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '3.8'
|
110
|
+
type: :development
|
111
|
+
prerelease: false
|
112
|
+
version_requirements: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - "~>"
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '3.8'
|
117
|
+
- !ruby/object:Gem::Dependency
|
118
|
+
name: webmock
|
119
|
+
requirement: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - "~>"
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '3.6'
|
124
|
+
type: :development
|
125
|
+
prerelease: false
|
126
|
+
version_requirements: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - "~>"
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '3.6'
|
131
|
+
description:
|
132
|
+
email:
|
133
|
+
- ivan@lobko.com
|
134
|
+
executables: []
|
135
|
+
extensions: []
|
136
|
+
extra_rdoc_files: []
|
137
|
+
files:
|
138
|
+
- ".gitignore"
|
139
|
+
- ".rspec"
|
140
|
+
- ".rubocop.yml"
|
141
|
+
- Gemfile
|
142
|
+
- Gemfile.lock
|
143
|
+
- LICENSE.txt
|
144
|
+
- README.md
|
145
|
+
- Rakefile
|
146
|
+
- bin/console
|
147
|
+
- bin/setup
|
148
|
+
- examples/example.rb
|
149
|
+
- lib/rawg.rb
|
150
|
+
- lib/rawg/client.rb
|
151
|
+
- lib/rawg/collection.rb
|
152
|
+
- lib/rawg/game.rb
|
153
|
+
- lib/rawg/utils.rb
|
154
|
+
- lib/rawg/version.rb
|
155
|
+
- rawg_rb.gemspec
|
156
|
+
homepage: https://github.com/ivanlobko/rawg.rb
|
157
|
+
licenses:
|
158
|
+
- MIT
|
159
|
+
metadata:
|
160
|
+
homepage_uri: https://github.com/ivanlobko/rawg.rb
|
161
|
+
source_code_uri: https://github.com/ivanlobko/rawg.rb
|
162
|
+
post_install_message:
|
163
|
+
rdoc_options: []
|
164
|
+
require_paths:
|
165
|
+
- lib
|
166
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
167
|
+
requirements:
|
168
|
+
- - ">="
|
169
|
+
- !ruby/object:Gem::Version
|
170
|
+
version: '0'
|
171
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
172
|
+
requirements:
|
173
|
+
- - ">="
|
174
|
+
- !ruby/object:Gem::Version
|
175
|
+
version: '0'
|
176
|
+
requirements: []
|
177
|
+
rubygems_version: 3.0.3
|
178
|
+
signing_key:
|
179
|
+
specification_version: 4
|
180
|
+
summary: A Ruby client for RAWG.io API
|
181
|
+
test_files: []
|