Skip to content

Commit

Permalink
Completed refactoring.
Browse files Browse the repository at this point in the history
All Requests have been moved in separate classes now.
  • Loading branch information
intinig committed Dec 17, 2013
1 parent 4cf82a4 commit a30eebf
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 27 deletions.
1 change: 1 addition & 0 deletions lib/lol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require "lol/game_request"
require "lol/stats_request"
require "lol/team_request"
require "lol/league_request"

require "lol/model"
require "lol/champion"
Expand Down
7 changes: 0 additions & 7 deletions lib/lol/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ def team
@team_request ||= TeamRequest.new(api_key, region)
end

# Retrieves leagues data for summoner, including leagues for all of summoner's teams, v2.1
# @return [Array] an array of champions
def league21 summoner_id
response = get(api_url("v2.1", "league/by-summoner/#{summoner_id}"))[summoner_id]
response.is_a?(Hash) ? [League.new(response)] : response.map {|l| League.new l}
end

# Initializes a Lol::Client
# @param api_key [String]
# @param options [Hash]
Expand Down
11 changes: 11 additions & 0 deletions lib/lol/league_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Lol
class LeagueRequest < Request
# Retrieves leagues data for summoner, including leagues for all of summoner's teams, v2.1
# @return [Array] an array of champions
def get summoner_id
response = perform_request(api_url("v2.1", "league/by-summoner/#{summoner_id}"))[summoner_id]
response.is_a?(Hash) ? [League.new(response)] : response.map {|l| League.new l}
end

end
end
32 changes: 12 additions & 20 deletions spec/lol/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@
end
end

describe "#league" do
it "returns an instance of LeagueRequest" do
expect(subject.league).to be_a(LeagueRequest)
end

it "initializes the LeagueRequest with the current API key and region" do
expect(LeagueRequest).to receive(:new).with(subject.api_key, subject.region)

subject.league
end
end

describe "#api_key" do
it "returns an api key" do
expect(subject.api_key).to eq("foo")
Expand All @@ -90,24 +102,4 @@
end
end

describe "league" do
it "calls latest version of league" do
expect(subject).to receive(:league21)
subject.league("foo")
end
end

describe "league21" do
let(:client) { Client.new "foo" }

subject do
expect(client).to receive(:get).with(client.api_url("v2.1", "league/by-summoner/foo")).and_return(load_fixture("league", "v2.1", "get"))

client.league21("foo")
end

it "returns an array of Leagues" do
expect(subject.map(&:class).uniq).to eq([League])
end
end
end
24 changes: 24 additions & 0 deletions spec/lol/league_request_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require "spec_helper"
require "lol"

include Lol

describe LeagueRequest do
it "inherits from Request" do
expect(LeagueRequest.ancestors[1]).to eq(Request)
end

describe "#get" do
let(:request) { LeagueRequest.new "api_key", "euw" }

subject do
expect(request.class).to receive(:get).with(request.api_url("v2.1", "league/by-summoner/foo")).and_return(load_fixture("league", "v2.1", "get"))

request.get("foo")
end

it "returns an array of Leagues" do
expect(subject.map(&:class).uniq).to eq([League])
end
end
end

0 comments on commit a30eebf

Please sign in to comment.