ruby-lol 0.0.6 → 0.0.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4e4514fa6155984e91035d39c11f299138b3b104
4
- data.tar.gz: 38fd725d0b900483523b5ffb7fb93c5131ce9eda
3
+ metadata.gz: 64b2c711ab04175c2affa3e50c2e2edcd19174cc
4
+ data.tar.gz: 332cbea429a999952017e23e26e156dcf777e64c
5
5
  SHA512:
6
- metadata.gz: 2a9c947b9c8f803bf1cf13850bfd40f0d7aad75b1bd81878a508cef07f51353306154b859e26a7fab50f496913e3153275119f5d062786477a6d9105f7b08374
7
- data.tar.gz: 26103c71d7782de216d3eeb7a81f0062e4bd33f101fa0bb4cc29d6e39de0a798221ce0516042060cbfaa275ff0cf4c51abce4752ff1222e0281a07f8c204383b
6
+ metadata.gz: 745627a7bcdcb12d570f246dd5da1dd8636e7061036de7f9b24b4ea9a7b323f9db119c923d53c49efd0b20acd9d80cd7fbba8944cac0d6e1a509f6746c4817bf
7
+ data.tar.gz: 7b7c2824be9137a8f3c7b63bf6851ca912f4750076f159cb7aac1b91f00877a10f43f3b3a7086119af4b5881390fb06c7c8534a273193731f4939148ce5b0d37
data/README.md CHANGED
@@ -3,6 +3,10 @@
3
3
 
4
4
  ruby-lol is a wrapper to the [Riot Games API](https://developer.riotgames.com).
5
5
 
6
+ ## IMPORTANT NOTICE
7
+
8
+ An important piece of refactoring is happening in the *refactoring* branch. Check it out for updated docs. I plan to merge it in this week.
9
+
6
10
  ## Installation
7
11
 
8
12
  Add this line to your application's Gemfile:
@@ -32,32 +36,48 @@ Or install it yourself as:
32
36
  na_client = Lol::Client.new "my_api_key", :region => "na"
33
37
  # => <Lol::Client:0x007fd09d1abb00 @api_key="my_api_key", @region="na">
34
38
 
35
- # gets all champions
36
- champions = client.champion
37
- # => Array of Lol::Champion
38
-
39
- # let's play a bit, who is free to play?
40
- client.champion.select {|c| c.free_to_play }.map {|c| c.name}
41
- # => %w(Aatrox Cassiopeia Lux Malphite MissFortune MonkeyKing Nautilus Sivir Talon Taric)
42
-
43
- # it's time to fetch some of my games, isn't it?
44
- games = client.game my_summoner_id
45
- # => Array of Lol::Game
46
-
47
- # let's get one game and look into it
48
- game = games.first
49
-
50
- # who was I playing with?
51
- game.fellow_players
52
- # => Array of Lol::Player
53
-
54
- # gimme some stats!
55
- game.statistics
56
- # => Array of Lol::RawStatistic
57
-
58
- # let's get some info about my Leagues now
59
- leagues = client.league my_summoner_id
60
- # => Array of Lol::League
39
+ # Available Requests
40
+ client.champion
41
+ # => Lol::ChampionRequest
42
+ client.game
43
+ # => Lol::GameRequest
44
+ client.league
45
+ # => Lol::LeagueRequest
46
+ client.stats
47
+ # => Lol::StatsRequest
48
+ client.summoner
49
+ # => Lol::SummonerRequest
50
+ client.team
51
+ # => Lol::TeamRequest
52
+
53
+ # Available methods for each request type
54
+ client.champion.get
55
+ # => Lol::Champion
56
+
57
+ client.game.recent(summoner_id)
58
+ # => Lol::Game
59
+
60
+ client.league.get(summoner_id)
61
+ # => Lol::League
62
+
63
+ client.stats.summary(summoner_id)
64
+ # => Lol::SummaryStats
65
+ client.stats.ranked(summoner_id)
66
+ # => Lol::RankedStats
67
+
68
+ client.summoner.masteries(summoner_id)
69
+ # => Lol::Masteries
70
+ client.summoner.runes(summoner_id)
71
+ # => Lol::Runes
72
+ client.summoner.by_name(name)
73
+ # => Lol::Summoner
74
+ client.summoner.get(summoner_id)
75
+ # => Lol::Summoner
76
+ client.summoner.name(summoner_ids)
77
+ # => Array
78
+
79
+ client.team.get(summoner_id)
80
+ # => Array
61
81
  ```
62
82
 
63
83
  ## Contributing
data/lib/lol/champion.rb CHANGED
@@ -1,5 +1,3 @@
1
- require 'lol/model'
2
-
3
1
  module Lol
4
2
  class Champion < Lol::Model
5
3
  # @!attribute [r] id
@@ -0,0 +1,11 @@
1
+ module Lol
2
+ class ChampionRequest < Request
3
+
4
+ # Retrieve all champions, v1.1
5
+ # @return [Array] an array of champions
6
+ def get
7
+ perform_request(api_url("v1.1", "champion"))["champions"].map {|c| Champion.new(c)}
8
+ end
9
+
10
+ end
11
+ end
data/lib/lol/client.rb CHANGED
@@ -5,7 +5,6 @@ module Lol
5
5
  class InvalidAPIResponse < StandardError; end
6
6
 
7
7
  class Client
8
- include HTTParty
9
8
 
10
9
  # @!attribute [rw] region
11
10
  # @return [String] name of region
@@ -15,110 +14,29 @@ module Lol
15
14
  # @return [String] the API key that has been used
16
15
  attr_reader :api_key
17
16
 
18
- # Returns a full url for an API call
19
- # @param version [String] API version to call
20
- # @param path [String] API path to call
21
- # @return [String] full fledged url
22
- def api_url version, path, params = {}
23
- lol = version == "v1.1" ? "lol" : ""
24
- query_string = URI.encode_www_form params.merge api_key: api_key
25
- File.join "http://prod.api.pvp.net/api/", lol, "/#{region}/#{version}/", "#{path}?#{query_string}"
26
- end
27
-
28
- # Calls the API via HTTParty and handles errors
29
- #
30
- def get url
31
- response = self.class.get(url)
32
- if response.is_a?(Hash) && response["status"]
33
- raise InvalidAPIResponse.new(response["status"]["message"])
34
- else
35
- response
36
- end
37
- end
38
-
39
- # Calls the latest API version of champion
17
+ # @return [ChampionRequest]
40
18
  def champion
41
- champion11
42
- end
43
-
44
- # Retrieve all champions, v1.1
45
- # @return [Array] an array of champions
46
- def champion11
47
- get(api_url("v1.1", "champion"))["champions"].map {|c| Champion.new(c)}
48
- end
49
-
50
- # Calls the latest API version of game returning the list of
51
- # recent games played by a summoner
52
- def game *args
53
- game11 *args
54
- end
55
-
56
- # Returns a list of the recent games played by a summoner
57
- # @param summoner_id [Fixnum] Summoner id
58
- # @return [Array] an array of games
59
- def game11 summoner_id
60
- summoner_api_path = "game/by-summoner/#{summoner_id}/recent"
61
- get(api_url("v1.1", summoner_api_path))["games"].map do |game_data|
62
- Game.new game_data
63
- end
64
- end
65
-
66
- # Calls the latest API version of league
67
- def league summoner_id
68
- league21 summoner_id
69
- end
70
-
71
- # Retrieves leagues data for summoner, including leagues for all of summoner's teams, v2.1
72
- # @return [Array] an array of champions
73
- def league21 summoner_id
74
- response = get(api_url("v2.1", "league/by-summoner/#{summoner_id}"))[summoner_id]
75
- response.is_a?(Hash) ? [League.new(response)] : response.map {|l| League.new l}
76
- end
77
-
78
- # Calls the latest API version of stats
79
- def stats *args
80
- stats11 *args
81
- end
82
-
83
- # Retrieves player statistics summaries for the given summoner
84
- # @return [Array] an array of player statistics, one per queue type
85
- def stats11 summoner_id, extra = {}
86
- if extra.keys.select { |k| k.to_sym != :season }.any?
87
- raise ArgumentError, 'Only :season is allowed as extra parameter'
88
- end
89
- stats_api_path = "stats/by-summoner/#{summoner_id}/summary"
90
- get(api_url('v1.1', stats_api_path, extra))['playerStatSummaries'].map do |player_stat_data|
91
- PlayerStatistic.new player_stat_data
92
- end
19
+ @champion_request ||= ChampionRequest.new(api_key, region)
93
20
  end
94
21
 
95
- # Calls the latest API version of ranked_stats
96
- def ranked_stats *args
97
- ranked_stats11 *args
22
+ # @return [GameRequest]
23
+ def game
24
+ @game_request ||= GameRequest.new(api_key, region)
98
25
  end
99
26
 
100
- # Retrieves ranked statistics summary for the given summoner
101
- # @return [RankedStatisticsSummary] Ranked Stats.
102
- # Includes stats for Twisted Treeline and Summoner's Rift
103
- def ranked_stats11 summoner_id, extra = {}
104
- if extra.keys.select { |k| k.to_sym != :season }.any?
105
- raise ArgumentError, 'Only :season is allowed as extra parameter'
106
- end
107
- stats_api_path = "stats/by-summoner/#{summoner_id}/ranked"
108
- RankedStatisticsSummary.new get api_url 'v1.1', stats_api_path, extra
27
+ # @return [StatsRequest]
28
+ def stats
29
+ @stats_request ||= StatsRequest.new(api_key, region)
109
30
  end
110
31
 
111
- # Calls the latest API version of team returning the list of teams for the given summoner
112
- def team *args
113
- team21 *args
32
+ # @return [LeagueRequest]
33
+ def league
34
+ @league_request ||= LeagueRequest.new(api_key, region)
114
35
  end
115
36
 
116
- # Retrieves the list of Teams for the given summoner
117
- # @return [Array] List of Team
118
- def team21 summoner_id
119
- get(api_url 'v2.1', "team/by-summoner/#{summoner_id}").map do |team_data|
120
- Team.new team_data
121
- end
37
+ # @return [TeamRequest]
38
+ def team
39
+ @team_request ||= TeamRequest.new(api_key, region)
122
40
  end
123
41
 
124
42
  # Initializes a Lol::Client
@@ -0,0 +1,13 @@
1
+ module Lol
2
+ class GameRequest < Request
3
+ # Returns a list of the recent games played by a summoner
4
+ # @param summoner_id [Fixnum] Summoner id
5
+ # @return [Array] an array of games
6
+ def recent summoner_id
7
+ summoner_api_path = "game/by-summoner/#{summoner_id}/recent"
8
+ perform_request(api_url("v1.1", summoner_api_path))["games"].map do |game_data|
9
+ Game.new game_data
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ module Lol
2
+ class LeagueRequest < Request
3
+ # Retrieves leagues data for summoner, including leagues for all of summoner's teams, v2.1
4
+ # @return [Array] an array of champions
5
+ def get summoner_id
6
+ response = perform_request(api_url("v2.1", "league/by-summoner/#{summoner_id}"))[summoner_id]
7
+ response.is_a?(Hash) ? [League.new(response)] : response.map {|l| League.new l}
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Lol
2
+ class TeamRequest < Request
3
+ # Retrieves the list of Teams for the given summoner
4
+ # @return [Array] List of Team
5
+ def get summoner_id
6
+ perform_request(api_url 'v2.1', "team/by-summoner/#{summoner_id}").map do |team_data|
7
+ Team.new team_data
8
+ end
9
+ end
10
+ end
11
+ end
data/lib/lol/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Lol
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
data/lib/lol.rb CHANGED
@@ -1,18 +1,27 @@
1
- require __dir__ + "/lol/client"
2
- require __dir__ + "/lol/champion"
3
- require __dir__ + "/lol/game"
4
- require __dir__ + "/lol/league"
5
- require __dir__ + "/lol/player"
6
- require __dir__ + "/lol/raw_statistic"
7
- require __dir__ + "/lol/player_statistic"
8
- require __dir__ + "/lol/aggregated_statistic"
9
- require __dir__ + "/lol/ranked_statistics_summary"
10
- require __dir__ + "/lol/champion_statistics_summary"
11
- require __dir__ + "/lol/champion_statistic"
12
- require __dir__ + "/lol/team"
13
- require __dir__ + "/lol/roster"
14
- require __dir__ + "/lol/team_member"
15
- require __dir__ + "/lol/team_statistic"
16
- require __dir__ + "/lol/match_summary"
17
- require __dir__ + "/lol/league_entry"
18
- require __dir__ + "/lol/mini_series"
1
+ require "lol/client"
2
+
3
+ require "lol/request"
4
+ require "lol/champion_request"
5
+ require "lol/game_request"
6
+ require "lol/stats_request"
7
+ require "lol/team_request"
8
+ require "lol/league_request"
9
+
10
+ require "lol/model"
11
+ require "lol/champion"
12
+ require "lol/game"
13
+ require "lol/league"
14
+ require "lol/player"
15
+ require "lol/raw_statistic"
16
+ require "lol/player_statistic"
17
+ require "lol/aggregated_statistic"
18
+ require "lol/ranked_statistics_summary"
19
+ require "lol/champion_statistics_summary"
20
+ require "lol/champion_statistic"
21
+ require "lol/team"
22
+ require "lol/roster"
23
+ require "lol/team_member"
24
+ require "lol/team_statistic"
25
+ require "lol/match_summary"
26
+ require "lol/league_entry"
27
+ require "lol/mini_series"
@@ -0,0 +1,32 @@
1
+ require "spec_helper"
2
+ require "lol"
3
+
4
+ include Lol
5
+
6
+ describe ChampionRequest do
7
+ it "inherits from Request" do
8
+ expect(ChampionRequest.ancestors[1]).to eq(Request)
9
+ end
10
+
11
+ describe "get" do
12
+ let(:request) { ChampionRequest.new "api_key", "euw" }
13
+
14
+ subject do
15
+ expect(request).to receive(:perform_request).with(request.api_url("v1.1", "champion")).and_return(load_fixture("champion", "v1.1", "get"))
16
+
17
+ request.get
18
+ end
19
+
20
+ it "returns an array" do
21
+ expect(subject).to be_a(Array)
22
+ end
23
+
24
+ it "returns an array of champions" do
25
+ expect(subject.map {|e| e.class}.uniq).to eq([Champion])
26
+ end
27
+
28
+ it "fetches champions from the API" do
29
+ expect(subject.size).to eq(load_fixture("champion", "v1.1", "get")["champions"].size)
30
+ end
31
+ end
32
+ end
@@ -20,200 +20,64 @@ describe Client do
20
20
  end
21
21
  end
22
22
 
23
- describe "#get" do
24
- it "calls HTTParty get" do
25
- expect(Client).to receive(:get).and_return(error_401)
26
- expect { subject.get "foo"}.to raise_error(InvalidAPIResponse)
27
- end
28
-
29
- it "handles 401" do
30
- expect(Client).to receive(:get).and_return(error_401)
31
- expect { subject.champion }.to raise_error(InvalidAPIResponse)
32
- end
33
- end
34
23
 
35
24
  describe "#champion" do
36
- it "defaults to v1.1" do
37
- expect(subject).to receive(:champion11)
38
- subject.champion
39
- end
40
- end
41
-
42
- describe "#champion11" do
43
- let(:client) { Client.new "foo" }
44
-
45
- subject do
46
- expect(client).to receive(:get).with(client.api_url("v1.1", "champion")).and_return(load_fixture("champion", "v1.1", "get"))
47
-
48
- client.champion11
25
+ it "returns an instance of ChampionRequest" do
26
+ expect(subject.champion).to be_a(ChampionRequest)
49
27
  end
50
28
 
51
- it "returns an array" do
52
- expect(subject).to be_a(Array)
53
- end
29
+ it "initializes the ChampionRequest with the current API key and region" do
30
+ expect(ChampionRequest).to receive(:new).with(subject.api_key, subject.region)
54
31
 
55
- it "returns an array of champions" do
56
- expect(subject.map {|e| e.class}.uniq).to eq([Champion])
57
- end
58
-
59
- it "fetches champions from the API" do
60
- expect(subject.size).to eq(load_fixture("champion", "v1.1", "get")["champions"].size)
32
+ subject.champion
61
33
  end
62
34
  end
63
35
 
64
36
  describe '#game' do
65
- it 'requires a summoner id' do
66
- expect { subject.game }.to raise_error ArgumentError
67
- end
68
-
69
- it 'defaults to v1.1' do
70
- expect(subject).to receive(:game11).with 'foo'
71
- subject.game 'foo'
37
+ it "returns an instance of GameRequest" do
38
+ expect(subject.game).to be_a(GameRequest)
72
39
  end
73
- end
74
-
75
- describe '#game11' do
76
- let(:client) { Client.new 'foo' }
77
40
 
78
- subject do
79
- expect(Client).to receive(:get).with(client.api_url('v1.1', "game/by-summoner/1/recent")).and_return load_fixture('game', 'v1.1', 'get')
80
- client.game11 1
81
- end
82
-
83
- it 'returns an array' do
84
- expect(subject).to be_a Array
85
- end
86
-
87
- it 'returns an array of Games' do
88
- expect(subject.map(&:class).uniq).to eq [Game]
89
- end
41
+ it "initializes the GameRequest with the current API key and region" do
42
+ expect(GameRequest).to receive(:new).with(subject.api_key, subject.region)
90
43
 
91
- it 'fetches games from the API' do
92
- expect(subject.size).to eq load_fixture('game', 'v1.1', 'get')['games'].size
44
+ subject.game
93
45
  end
94
46
  end
95
47
 
96
48
  describe '#stats' do
97
- it 'defaults to v1.1' do
98
- expect(subject).to receive(:stats11).with 'foo'
99
- subject.stats 'foo'
100
- end
101
- end
102
-
103
- describe '#stats11' do
104
- let(:client) { Client.new 'foo' }
105
- let(:fixture) { load_fixture 'stats', 'v1.1', 'get' }
106
-
107
- subject do
108
- expect(Client).to receive(:get).with(client.api_url('v1.1', "stats/by-summoner/1/summary")).and_return fixture
109
- client.stats11 1
110
- end
111
-
112
- it 'requires a summoner' do
113
- expect { client.stats }.to raise_error ArgumentError
114
- end
115
-
116
- it 'returns an array' do
117
- expect(subject).to be_a Array
118
- end
119
-
120
- it 'returns an array of PlayerStatistic' do
121
- expect(subject.map(&:class).uniq).to eq [PlayerStatistic]
122
- end
123
-
124
- it 'fetches PlayerStatistics from the API' do
125
- expect(subject.size).to eq load_fixture('stats', 'v1.1', 'get')['playerStatSummaries'].size
126
- end
127
-
128
- it 'optionally accepts a season' do
129
- expect(Client).to receive(:get).with(client.api_url('v1.1', 'stats/by-summoner/1/summary', season: '1')).and_return fixture
130
- client.stats11 '1', season: '1'
131
- end
132
-
133
- it 'raises an error when unexpected parameter is received' do
134
- expect { client.stats11 '1', asd: 'foo' }.to raise_error ArgumentError
135
- end
136
- end
137
-
138
- describe '#ranked_stats' do
139
- it 'defaults to v1.1' do
140
- expect(subject).to receive(:ranked_stats11).with 'foo'
141
- subject.ranked_stats 'foo'
142
- end
143
- end
144
-
145
- describe '#ranked_stats11' do
146
- let(:client) { Client.new 'foo' }
147
- let(:fixture) { load_fixture 'ranked_stats', 'v1.1', 'get' }
148
-
149
- subject do
150
- expect(Client).to receive(:get).with(client.api_url('v1.1', "stats/by-summoner/1/ranked")).and_return fixture
151
- client.ranked_stats11 1
49
+ it "returns an instance of StatsRequest" do
50
+ expect(subject.stats).to be_a(StatsRequest)
152
51
  end
153
52
 
154
- it 'requires a summoner' do
155
- expect { client.ranked_stats }.to raise_error ArgumentError
156
- end
157
-
158
- it 'returns a RankedStatisticsSummary' do
159
- expect(subject).to be_a RankedStatisticsSummary
160
- end
161
-
162
- it 'fetches RankedStatisticsSummary from the API' do
163
- expect(subject.champions.size).to eq load_fixture('ranked_stats', 'v1.1', 'get')['champions'].size
164
- end
165
-
166
- it 'optionally accepts a season' do
167
- expect(Client).to receive(:get).with(client.api_url('v1.1', 'stats/by-summoner/1/ranked', season: '1')).and_return fixture
168
- client.ranked_stats11 '1', season: '1'
169
- end
53
+ it "initializes the StatsRequest with the current API key and region" do
54
+ expect(StatsRequest).to receive(:new).with(subject.api_key, subject.region)
170
55
 
171
- it 'raises an error when unexpected parameter is received' do
172
- expect { client.ranked_stats11 '1', asd: 'foo' }.to raise_error ArgumentError
56
+ subject.stats
173
57
  end
174
58
  end
175
59
 
176
60
  describe '#team' do
177
- it 'defaults to v2.1' do
178
- expect(subject).to receive(:team21).with 'foo'
179
- subject.team 'foo'
180
- end
181
- end
182
-
183
- describe '#team21' do
184
- let(:client) { Client.new 'foo' }
185
- let(:fixture) { load_fixture 'team', 'v2.1', 'get' }
186
-
187
- subject do
188
- expect(Client).to receive(:get).with(client.api_url('v2.1', "team/by-summoner/1")).and_return fixture
189
- client.team21 1
61
+ it "returns an instance of TeamRequest" do
62
+ expect(subject.team).to be_a(TeamRequest)
190
63
  end
191
64
 
192
- it 'requires a summoner' do
193
- expect { client.team21 }.to raise_error ArgumentError
194
- end
65
+ it "initializes the TeamRequest with the current API key and region" do
66
+ expect(TeamRequest).to receive(:new).with(subject.api_key, subject.region)
195
67
 
196
- it 'returns an array' do
197
- expect(subject).to be_a Array
198
- end
199
-
200
- it 'returns an array of Team' do
201
- expect(subject.map(&:class).uniq).to eq [Team]
202
- end
203
-
204
- it 'fetches Team from the API' do
205
- expect(subject.size).to eq fixture.size
68
+ subject.team
206
69
  end
207
70
  end
208
71
 
209
- describe "#region" do
210
- it "returns current region" do
211
- expect(subject.region).to eq("euw")
72
+ describe "#league" do
73
+ it "returns an instance of LeagueRequest" do
74
+ expect(subject.league).to be_a(LeagueRequest)
212
75
  end
213
76
 
214
- it "can be set to a new region" do
215
- subject.region = "NA"
216
- expect(subject.region).to eq("NA")
77
+ it "initializes the LeagueRequest with the current API key and region" do
78
+ expect(LeagueRequest).to receive(:new).with(subject.api_key, subject.region)
79
+
80
+ subject.league
217
81
  end
218
82
  end
219
83
 
@@ -227,50 +91,15 @@ describe Client do
227
91
  end
228
92
  end
229
93
 
230
- describe "api_url" do
231
- it "defaults on Client#region" do
232
- expect(subject.api_url("foo", "bar")).to match(/\/euw\//)
233
- end
234
-
235
- it "requires a version and a path" do
236
- expect { subject.api_url("foo") }.to raise_error(ArgumentError)
237
- end
238
-
239
- it "returns a full fledged api url" do
240
- expect(subject.api_url("foo", "bar")).to eq("http://prod.api.pvp.net/api/euw/foo/bar?api_key=foo")
241
- end
242
-
243
- it "has lol if url is v1.1" do
244
- expect(subject.api_url("v1.1", "foo")).to eq("http://prod.api.pvp.net/api/lol/euw/v1.1/foo?api_key=foo")
245
- end
246
-
247
- it "does not have lol if url is v2.1 or greater" do
248
- expect(subject.api_url("v2.1", "foo")).to eq("http://prod.api.pvp.net/api/euw/v2.1/foo?api_key=foo")
249
- end
250
-
251
- it "optionally accept query string parameters" do
252
- expect(subject.api_url("v2.1", "foo", a: 'b')).to eq("http://prod.api.pvp.net/api/euw/v2.1/foo?a=b&api_key=foo")
94
+ describe "#region" do
95
+ it "returns current region" do
96
+ expect(subject.region).to eq("euw")
253
97
  end
254
- end
255
98
 
256
- describe "league" do
257
- it "calls latest version of league" do
258
- expect(subject).to receive(:league21)
259
- subject.league("foo")
99
+ it "can be set to a new region" do
100
+ subject.region = "NA"
101
+ expect(subject.region).to eq("NA")
260
102
  end
261
103
  end
262
104
 
263
- describe "league21" do
264
- let(:client) { Client.new "foo" }
265
-
266
- subject do
267
- expect(client).to receive(:get).with(client.api_url("v2.1", "league/by-summoner/foo")).and_return(load_fixture("league", "v2.1", "get"))
268
-
269
- client.league21("foo")
270
- end
271
-
272
- it "returns an array of Leagues" do
273
- expect(subject.map(&:class).uniq).to eq([League])
274
- end
275
- end
276
105
  end
@@ -0,0 +1,33 @@
1
+ require "spec_helper"
2
+ require "lol"
3
+
4
+ include Lol
5
+
6
+ describe GameRequest do
7
+ it "inherits from Request" do
8
+ expect(GameRequest.ancestors[1]).to eq(Request)
9
+ end
10
+
11
+ describe "#recent" do
12
+ let(:request) { GameRequest.new "api_key", "euw" }
13
+
14
+ subject do
15
+ expect(request.class).to receive(:get).with(request.api_url('v1.1', "game/by-summoner/1/recent")).and_return load_fixture('game', 'v1.1', 'get')
16
+
17
+ request.recent 1
18
+ end
19
+
20
+ it 'returns an array' do
21
+ expect(subject).to be_a Array
22
+ end
23
+
24
+ it 'returns an array of Games' do
25
+ expect(subject.map(&:class).uniq).to eq [Game]
26
+ end
27
+
28
+ it 'fetches games from the API' do
29
+ expect(subject.size).to eq load_fixture('game', 'v1.1', 'get')['games'].size
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,24 @@
1
+ require "spec_helper"
2
+ require "lol"
3
+
4
+ include Lol
5
+
6
+ describe LeagueRequest do
7
+ it "inherits from Request" do
8
+ expect(LeagueRequest.ancestors[1]).to eq(Request)
9
+ end
10
+
11
+ describe "#get" do
12
+ let(:request) { LeagueRequest.new "api_key", "euw" }
13
+
14
+ subject do
15
+ expect(request.class).to receive(:get).with(request.api_url("v2.1", "league/by-summoner/foo")).and_return(load_fixture("league", "v2.1", "get"))
16
+
17
+ request.get("foo")
18
+ end
19
+
20
+ it "returns an array of Leagues" do
21
+ expect(subject.map(&:class).uniq).to eq([League])
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,51 @@
1
+ module Lol
2
+ # Encapsulates common methods for all requests
3
+ # Request classes inherit from this
4
+ class Request
5
+ include HTTParty
6
+
7
+ # @!attribute [r] api_key
8
+ # @return [String] api_key
9
+ attr_reader :api_key
10
+
11
+
12
+ # @!attribute [rw] region
13
+ # @return [String] region
14
+ attr_accessor :region
15
+
16
+ # Returns a full url for an API call
17
+ # @param version [String] API version to call
18
+ # @param path [String] API path to call
19
+ # @return [String] full fledged url
20
+ def api_url version, path, params = {}
21
+ lol = version == "v1.1" ? "lol" : ""
22
+ query_string = URI.encode_www_form params.merge api_key: api_key
23
+ File.join "http://prod.api.pvp.net/api/", lol, "/#{region}/#{version}/", "#{path}?#{query_string}"
24
+ end
25
+
26
+ # Calls the API via HTTParty and handles errors
27
+ # @param url [String] the url to call
28
+ # @return [String] raw response of the call
29
+ def perform_request url
30
+ response = self.class.get(url)
31
+ if response.is_a?(Hash) && response["status"]
32
+ raise InvalidAPIResponse.new(response["status"]["message"])
33
+ else
34
+ response
35
+ end
36
+ end
37
+
38
+ def initialize api_key, region
39
+ @api_key = api_key
40
+ @region = region
41
+ end
42
+ private
43
+
44
+ # Sets api_key to new_key
45
+ # @param new_key [String] a Riot Games API key
46
+ # @return [String] new_key
47
+ def api_key= new_key
48
+ @api_key = new_key
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,56 @@
1
+ require "spec_helper"
2
+ require "lol"
3
+
4
+ include Lol
5
+
6
+ describe Request do
7
+ context "initialization" do
8
+ it "requires an api_key parameter" do
9
+ expect { ChampionRequest.new }.to raise_error(ArgumentError)
10
+ end
11
+
12
+ it "accepts a region parameter" do
13
+ expect { ChampionRequest.new "foo" }.to raise_error(ArgumentError)
14
+ end
15
+ end
16
+
17
+ subject { Request.new "api_key", "euw"}
18
+
19
+ describe "#perform_request" do
20
+ it "calls HTTParty get" do
21
+ expect(subject.class).to receive(:get).and_return(error_401)
22
+ expect { subject.perform_request "foo"}.to raise_error(InvalidAPIResponse)
23
+ end
24
+
25
+ it "handles 401" do
26
+ expect(subject.class).to receive(:get).and_return(error_401)
27
+ expect { subject.perform_request "foo" }.to raise_error(InvalidAPIResponse)
28
+ end
29
+ end
30
+
31
+ describe "api_url" do
32
+ it "defaults on Request#region" do
33
+ expect(subject.api_url("foo", "bar")).to match(/\/euw\//)
34
+ end
35
+
36
+ it "requires an api_key a version and a path" do
37
+ expect { subject.api_url "foo" }.to raise_error(ArgumentError)
38
+ end
39
+
40
+ it "returns a full fledged api url" do
41
+ expect(subject.api_url("foo", "bar")).to eq("http://prod.api.pvp.net/api/euw/foo/bar?api_key=api_key")
42
+ end
43
+
44
+ it "has lol if url is v1.1" do
45
+ expect(subject.api_url("v1.1", "foo")).to eq("http://prod.api.pvp.net/api/lol/euw/v1.1/foo?api_key=api_key")
46
+ end
47
+
48
+ it "does not have lol if url is v2.1 or greater" do
49
+ expect(subject.api_url("v2.1", "foo")).to eq("http://prod.api.pvp.net/api/euw/v2.1/foo?api_key=api_key")
50
+ end
51
+
52
+ it "optionally accept query string parameters" do
53
+ expect(subject.api_url("v2.1", "foo", a: 'b')).to eq("http://prod.api.pvp.net/api/euw/v2.1/foo?a=b&api_key=api_key")
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,29 @@
1
+ module Lol
2
+ class StatsRequest < Request
3
+ # Retrieves player statistics summaries for the given summoner
4
+ # @param [String] summoner_id
5
+ # @return [Array] an array of player statistics, one per queue type
6
+ def summary summoner_id, extra = {}
7
+ if extra.keys.select { |k| k.to_sym != :season }.any?
8
+ raise ArgumentError, 'Only :season is allowed as extra parameter'
9
+ end
10
+ stats_api_path = "stats/by-summoner/#{summoner_id}/summary"
11
+ perform_request(api_url('v1.1', stats_api_path, extra))['playerStatSummaries'].map do |player_stat_data|
12
+ PlayerStatistic.new player_stat_data
13
+ end
14
+ end
15
+
16
+ # Retrieves ranked statistics summary for the given summoner
17
+ # @param [String] summoner_id
18
+ # @return [RankedStatisticsSummary] Ranked Stats.
19
+ # Includes stats for Twisted Treeline and Summoner's Rift
20
+ def ranked summoner_id, extra = {}
21
+ if extra.keys.select { |k| k.to_sym != :season }.any?
22
+ raise ArgumentError, 'Only :season is allowed as extra parameter'
23
+ end
24
+ stats_api_path = "stats/by-summoner/#{summoner_id}/ranked"
25
+ RankedStatisticsSummary.new perform_request api_url 'v1.1', stats_api_path, extra
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,79 @@
1
+ require "spec_helper"
2
+ require "lol"
3
+
4
+ include Lol
5
+
6
+ describe StatsRequest do
7
+ let(:request) { StatsRequest.new "api_key", "euw" }
8
+
9
+ it "inherits from Request" do
10
+ expect(GameRequest.ancestors[1]).to eq(Request)
11
+ end
12
+
13
+ describe "#summary" do
14
+ let(:fixture) { load_fixture 'stats', 'v1.1', 'get' }
15
+
16
+ subject do
17
+ expect(request.class).to receive(:get).with(request.api_url('v1.1', "stats/by-summoner/1/summary")).and_return fixture
18
+
19
+ request.summary 1
20
+ end
21
+
22
+ it 'requires a summoner' do
23
+ expect { request.summary }.to raise_error ArgumentError
24
+ end
25
+
26
+ it 'returns an array' do
27
+ expect(subject).to be_a Array
28
+ end
29
+
30
+ it 'returns an array of PlayerStatistic' do
31
+ expect(subject.map(&:class).uniq).to eq [PlayerStatistic]
32
+ end
33
+
34
+ it 'fetches PlayerStatistics from the API' do
35
+ expect(subject.size).to eq load_fixture('stats', 'v1.1', 'get')['playerStatSummaries'].size
36
+ end
37
+
38
+ it 'optionally accepts a season' do
39
+ expect(request.class).to receive(:get).with(request.api_url('v1.1', 'stats/by-summoner/1/summary', season: '1')).and_return fixture
40
+ request.summary '1', season: '1'
41
+ end
42
+
43
+ it 'raises an error when unexpected parameter is received' do
44
+ expect { request.summary '1', asd: 'foo' }.to raise_error ArgumentError
45
+ end
46
+ end
47
+
48
+ describe "#ranked" do
49
+ let(:fixture) { load_fixture 'ranked_stats', 'v1.1', 'get' }
50
+
51
+ subject do
52
+ expect(request.class).to receive(:get).with(request.api_url('v1.1', "stats/by-summoner/1/ranked")).and_return fixture
53
+ request.ranked 1
54
+ end
55
+
56
+ it 'requires a summoner' do
57
+ expect { request.ranked }.to raise_error ArgumentError
58
+ end
59
+
60
+ it 'returns a RankedStatisticsSummary' do
61
+ expect(subject).to be_a RankedStatisticsSummary
62
+ end
63
+
64
+ it 'fetches RankedStatisticsSummary from the API' do
65
+ expect(subject.champions.size).to eq load_fixture('ranked_stats', 'v1.1', 'get')['champions'].size
66
+ end
67
+
68
+ it 'optionally accepts a season' do
69
+ expect(request.class).to receive(:get).with(request.api_url('v1.1', 'stats/by-summoner/1/ranked', season: '1')).and_return fixture
70
+
71
+ request.ranked '1', season: '1'
72
+ end
73
+
74
+ it 'raises an error when unexpected parameter is received' do
75
+ expect { request.ranked '1', asd: 'foo' }.to raise_error ArgumentError
76
+ end
77
+ end
78
+
79
+ end
@@ -0,0 +1,39 @@
1
+ require "spec_helper"
2
+ require "lol"
3
+
4
+ include Lol
5
+
6
+ describe TeamRequest do
7
+ let(:request) { TeamRequest.new "api_key", "euw" }
8
+
9
+ it "inherits from Request" do
10
+ expect(TeamRequest.ancestors[1]).to eq(Request)
11
+ end
12
+
13
+ describe "get" do
14
+ let(:request) { TeamRequest.new "api_key", "euw" }
15
+ let(:fixture) { load_fixture 'team', 'v2.1', 'get' }
16
+
17
+ subject do
18
+ expect(request.class).to receive(:get).with(request.api_url('v2.1', "team/by-summoner/1")).and_return fixture
19
+ request.get 1
20
+ end
21
+
22
+ it 'requires a summoner' do
23
+ expect { request.get }.to raise_error ArgumentError
24
+ end
25
+
26
+ it 'returns an array' do
27
+ expect(subject).to be_a Array
28
+ end
29
+
30
+ it 'returns an array of Team' do
31
+ expect(subject.map(&:class).uniq).to eq [Team]
32
+ end
33
+
34
+ it 'fetches Team from the API' do
35
+ expect(subject.size).to eq fixture.size
36
+ end
37
+ end
38
+
39
+ end
data/spec/spec_helper.rb CHANGED
@@ -43,3 +43,7 @@ end
43
43
  def error_401
44
44
  {"status" => {"message" => "Foo", "status_code" => 401}}
45
45
  end
46
+
47
+ RSpec.configure do |c|
48
+ c.fail_fast = true
49
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-lol
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Giovanni Intini
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-14 00:00:00.000000000 Z
11
+ date: 2013-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -244,12 +244,15 @@ files:
244
244
  - lib/lol.rb
245
245
  - lib/lol/aggregated_statistic.rb
246
246
  - lib/lol/champion.rb
247
+ - lib/lol/champion_request.rb
247
248
  - lib/lol/champion_statistic.rb
248
249
  - lib/lol/champion_statistics_summary.rb
249
250
  - lib/lol/client.rb
250
251
  - lib/lol/game.rb
252
+ - lib/lol/game_request.rb
251
253
  - lib/lol/league.rb
252
254
  - lib/lol/league_entry.rb
255
+ - lib/lol/league_request.rb
253
256
  - lib/lol/match_summary.rb
254
257
  - lib/lol/mini_series.rb
255
258
  - lib/lol/model.rb
@@ -260,6 +263,7 @@ files:
260
263
  - lib/lol/roster.rb
261
264
  - lib/lol/team.rb
262
265
  - lib/lol/team_member.rb
266
+ - lib/lol/team_request.rb
263
267
  - lib/lol/team_statistic.rb
264
268
  - lib/lol/version.rb
265
269
  - ruby-lol.gemspec
@@ -270,12 +274,15 @@ files:
270
274
  - spec/fixtures/v2.1/get-league.json
271
275
  - spec/fixtures/v2.1/get-team.json
272
276
  - spec/lol/aggregated_statistic_spec.rb
277
+ - spec/lol/champion_request_spec.rb
273
278
  - spec/lol/champion_spec.rb
274
279
  - spec/lol/champion_statistic_spec.rb
275
280
  - spec/lol/champion_statistics_summary_spec.rb
276
281
  - spec/lol/client_spec.rb
282
+ - spec/lol/game_request_spec.rb
277
283
  - spec/lol/game_spec.rb
278
284
  - spec/lol/league_entry_spec.rb
285
+ - spec/lol/league_request_spec.rb
279
286
  - spec/lol/league_spec.rb
280
287
  - spec/lol/match_summary_spec.rb
281
288
  - spec/lol/mini_series_spec.rb
@@ -283,8 +290,13 @@ files:
283
290
  - spec/lol/player_statistic_spec.rb
284
291
  - spec/lol/ranked_statistics_summary_spec.rb
285
292
  - spec/lol/raw_statistic_spec.rb
293
+ - spec/lol/request.rb
294
+ - spec/lol/request_spec.rb
286
295
  - spec/lol/roster_spec.rb
296
+ - spec/lol/stats_request.rb
297
+ - spec/lol/stats_request_spec.rb
287
298
  - spec/lol/team_member_spec.rb
299
+ - spec/lol/team_request_spec.rb
288
300
  - spec/lol/team_spec.rb
289
301
  - spec/lol/team_statistic_spec.rb
290
302
  - spec/spec_helper.rb
@@ -321,12 +333,15 @@ test_files:
321
333
  - spec/fixtures/v2.1/get-league.json
322
334
  - spec/fixtures/v2.1/get-team.json
323
335
  - spec/lol/aggregated_statistic_spec.rb
336
+ - spec/lol/champion_request_spec.rb
324
337
  - spec/lol/champion_spec.rb
325
338
  - spec/lol/champion_statistic_spec.rb
326
339
  - spec/lol/champion_statistics_summary_spec.rb
327
340
  - spec/lol/client_spec.rb
341
+ - spec/lol/game_request_spec.rb
328
342
  - spec/lol/game_spec.rb
329
343
  - spec/lol/league_entry_spec.rb
344
+ - spec/lol/league_request_spec.rb
330
345
  - spec/lol/league_spec.rb
331
346
  - spec/lol/match_summary_spec.rb
332
347
  - spec/lol/mini_series_spec.rb
@@ -334,8 +349,13 @@ test_files:
334
349
  - spec/lol/player_statistic_spec.rb
335
350
  - spec/lol/ranked_statistics_summary_spec.rb
336
351
  - spec/lol/raw_statistic_spec.rb
352
+ - spec/lol/request.rb
353
+ - spec/lol/request_spec.rb
337
354
  - spec/lol/roster_spec.rb
355
+ - spec/lol/stats_request.rb
356
+ - spec/lol/stats_request_spec.rb
338
357
  - spec/lol/team_member_spec.rb
358
+ - spec/lol/team_request_spec.rb
339
359
  - spec/lol/team_spec.rb
340
360
  - spec/lol/team_statistic_spec.rb
341
361
  - spec/spec_helper.rb