Skip to content

Commit

Permalink
Merge pull request #2 from nicolaracco/game_request
Browse files Browse the repository at this point in the history
Completed Game request and refactored specs
  • Loading branch information
intinig committed Dec 12, 2013
2 parents 6b4145f + d4e405e commit 013a0c1
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 125 deletions.
2 changes: 2 additions & 0 deletions lib/lol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
require __dir__ + "/lol/champion"
require __dir__ + "/lol/game"
require __dir__ + "/lol/league"
require __dir__ + "/lol/player"
require __dir__ + "/lol/statistic"
16 changes: 3 additions & 13 deletions lib/lol/champion.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Lol
class Champion

require 'lol/model'

module Lol
class Champion < Lol::Model
# @!attribute [r] raw
# @return [String] raw version of options Hash used to initialize Champion
attr_reader :raw
Expand Down Expand Up @@ -55,16 +55,6 @@ class Champion
# @return [false] if the Champion is disabled in ranked play
attr_reader :ranked_play_enabled

# Initializes a Lol::Champion
# @param options [Hash]
# @return [Lol::Champion]
def initialize options = {}
@raw = options
options.each do |attribute_name, value|
send "#{attribute_name.to_s.underscore}=", value
end
end

private

attr_writer :id, :name, :active, :attack_rank, :defense_rank, :magic_rank,
Expand Down
2 changes: 1 addition & 1 deletion lib/lol/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def game *args
# @param summoner_id [Fixnum] Summoner id
# @return [Array] an array of games
def game11 summoner_id
summoner_api_path = "game/by-summoner/#{summoner_id}"
summoner_api_path = "game/by-summoner/#{summoner_id}/recent"
get(api_url("v1.1", summoner_api_path))["games"].map do |game_data|
Game.new game_data
end
Expand Down
24 changes: 9 additions & 15 deletions lib/lol/game.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'active_support/core_ext/string/inflections'
require 'lol/model'

module Lol
class Game
class Game < Lol::Model
# @!attribute [r] raw
# @return [Hash] raw version of options Hash used to initialize Game
attr_reader :raw
Expand Down Expand Up @@ -67,32 +67,26 @@ class Game
# @return [Fixnum] Team Id associated with game
attr_reader :team_id

# Initializes a Lol::Game
# @param options [Hash]
# @return [Lol::Game]
def initialize options = {}
@raw = options
options.each do |attribute_name, value|
send "#{attribute_name.to_s.underscore}=", value
end
end

private

attr_writer :champion_id, :game_id, :game_mode, :game_type, :invalid,
:level, :map_id, :spell1, :spell2, :sub_type, :team_id,
:create_date_str

def create_date= value
@create_date = DateTime.strptime value.to_s, '%s'
@create_date = value.is_a?(DateTime) && value || DateTime.strptime(value.to_s, '%s')
end

def fellow_players= collection
@fellow_players = collection.map { |c| c }
@fellow_players = collection.map do |c|
c.respond_to?(:[]) && Player.new(c) || c
end
end

def statistics= collection
@statistics = collection.map { |c| c }
@statistics = collection.map do |c|
c.respond_to?(:[]) && Statistic.new(c) || c
end
end
end
end
15 changes: 15 additions & 0 deletions lib/lol/model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'active_support/core_ext/string/inflections'

module Lol
class Model
# Initializes a Lol::Model
# @param options [Hash]
# @return [Lol::Model]
def initialize options = {}
@raw = options
options.each do |attribute_name, value|
send "#{attribute_name.to_s.underscore}=", value
end
end
end
end
25 changes: 25 additions & 0 deletions lib/lol/player.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'lol/model'

module Lol
class Player < Lol::Model
# @!attribute [r] raw
# @return [Hash] raw version of options Hash used to initialize Player
attr_reader :raw

# @!attribute [r] champion_id
# @return [Fixnum] Champion Id associated with player
attr_reader :champion_id

# @!attribute [r] summoner_id
# @return [Fixnum] Summoner Id associated with player
attr_reader :summoner_id

# @!attribute [r] team_id
# @return [Fixnum] Team Id associated with player
attr_reader :team_id

private

attr_writer :champion_id, :summoner_id, :team_id
end
end
25 changes: 25 additions & 0 deletions lib/lol/statistic.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'lol/model'

module Lol
class Statistic < Lol::Model
# @!attribute [r] raw
# @return [Hash] raw version of options Hash used to initialize Statistic
attr_reader :raw

# @!attribute [r] id
# @return [Fixnum] Raw Statistic Id
attr_reader :id

# @!attribute [r] name
# @return [String] Raw Statistic name
attr_reader :name

# @!attribute [r] value
# @return [Fixnum] Raw Statistic value
attr_reader :value

private

attr_writer :id, :name, :value
end
end
42 changes: 7 additions & 35 deletions spec/lol/champion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,15 @@
include Lol

describe Champion do
describe "#new" do
it "takes an option hash as argument" do
expect { Champion.new({ :id => 1 }) }.not_to raise_error
end

it 'raises an error if an attribute is not allowed' do
expect { Champion.new({ :foo => :bar }) }.to raise_error NoMethodError
end

it 'sets the given option hash as #raw' do
options = { :id => 1 }
expect(Champion.new(options).raw).to eq options
end

%w(id name active attack_rank defense_rank magic_rank difficulty_rank bot_enabled free_to_play bot_mm_enabled ranked_play_enabled).each do |attribute|
it "sets #{attribute} if the hash contains #{attribute}" do
game = Champion.new({ attribute => 'foo' })
expect(game.send attribute).to eq 'foo'
end

it "sets #{attribute} if the hash contains #{camelize attribute}" do
game = Champion.new({ camelize(attribute) => 'foo' })
expect(game.send attribute).to eq 'foo'
end
end
end

describe "#raw" do
it "is readonly" do
expect { Champion.new.raw = "bar" }.to raise_error(NoMethodError)
end
it_behaves_like 'Lol model' do
let(:valid_attributes) { { id: 1 } }
end

%w(id name active attack_rank defense_rank magic_rank difficulty_rank bot_enabled free_to_play bot_mm_enabled ranked_play_enabled).each do |attr|
describe "##{attr}" do
it "is readonly" do
expect(subject).to_not respond_to "#{attr}="
%w(id name active attack_rank defense_rank magic_rank difficulty_rank bot_enabled free_to_play bot_mm_enabled ranked_play_enabled).each do |attribute|
describe "#{attribute} attribute" do
it_behaves_like 'plain attribute' do
let(:attribute) { attribute }
let(:attribute_value) { 'asd' }
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/lol/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
let(:client) { Client.new 'foo' }

subject do
expect(Client).to receive(:get).with(client.api_url('v1.1', "game/by-summoner/1")).and_return load_fixture('game', 'v1.1', 'get')
expect(Client).to receive(:get).with(client.api_url('v1.1', "game/by-summoner/1/recent")).and_return load_fixture('game', 'v1.1', 'get')
client.game11 1
end

Expand Down
85 changes: 25 additions & 60 deletions spec/lol/game_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,76 +5,41 @@
include Lol

describe Game do
describe '#new' do
it "takes an option hash as argument" do
expect { Game.new({ :game_id => 1 }) }.not_to raise_error
end

it 'raises an error if an attribute is not allowed' do
expect { Game.new({ :foo => :bar }) }.to raise_error NoMethodError
end

it 'sets the given option hash as #raw' do
options = { :game_id => 1 }
expect(Game.new(options).raw).to eq options
end

%w(champion_id create_date_str game_id game_mode game_type invalid level map_id spell1 spell2 sub_type team_id).each do |attribute|
it "sets #{attribute} if the hash contains #{attribute}" do
game = Game.new({ attribute => 'foo' })
expect(game.send attribute).to eq 'foo'
end

it "sets #{attribute} if the hash contains #{camelize attribute}" do
game = Game.new({ camelize(attribute) => 'foo' })
expect(game.send attribute).to eq 'foo'
end
end

%w(fellow_players statistics).each do |attribute|
it "sets #{attribute} if the hash contains #{attribute}" do
game = Game.new({ attribute => ['foo', 'bar'] })
expect(game.send(attribute).size).to eq 2
end

it "sets #{attribute} if the hash contains #{camelize attribute}" do
game = Game.new({ camelize(attribute) => ['foo', 'bar'] })
expect(game.send(attribute).size).to eq 2
end
it_behaves_like 'Lol model' do
let(:valid_attributes) { { game_id: 1 } }
end

it 'raises an error if the value is not enumerable' do
expect { Game.new({ attribute => 'asd' }) }.to raise_error NoMethodError
%w(champion_id create_date_str game_id game_mode game_type invalid level map_id spell1 spell2 sub_type team_id).each do |attribute|
describe "#{attribute} attribute" do
it_behaves_like 'plain attribute' do
let(:attribute) { attribute }
let(:attribute_value) { 'asd' }
end

pending 'parses the collection converting each value in an object'
end
end

context 'when createDate attribute is present' do
it 'sets the create_date attribute parsing the epoch' do
game = Game.new 'createDate' => 123
expect(game.create_date).to be_a DateTime
end

pending 'does not raise error if the attribute value is a DateTime object'
describe 'fellow_players attribute' do
it_behaves_like 'collection attribute' do
let(:attribute) { 'fellow_players' }
let(:attribute_class) { Player }
end
end

context 'when createDate attribute is absent' do
it 'does not set the create_date attribute' do
game = Game.new
expect(game.create_date).to be_nil
end
describe 'statistics attribute' do
it_behaves_like 'collection attribute' do
let(:attribute) { 'statistics' }
let(:attribute_class) { Statistic }
end
end

describe 'attributes' do
%w(champion_id create_date create_date_str fellow_players game_id game_mode game_type invalid level map_id spell1 spell2 statistics sub_type team_id).each do |attribute|
it "##{attribute} has its own reader" do
expect(subject).to respond_to attribute
end
describe 'create_date attribute' do
it_behaves_like 'plain attribute' do
let(:attribute) { 'create_date' }
let(:attribute_value) { DateTime.now }
end

it "##{attribute} cannot be set" do
expect(subject).to_not respond_to "#{attribute}="
end
it "parses the value is it's not a DateTime object" do
expect(Game.new(:create_date => Time.now.to_i).create_date).to be_a DateTime
end
end
end
19 changes: 19 additions & 0 deletions spec/lol/player_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "lol"
require "spec_helper"

include Lol

describe Player do
it_behaves_like 'Lol model' do
let(:valid_attributes) { { team_id: 1 } }
end

%w(champion_id summoner_id team_id).each do |attribute|
describe "#{attribute} attribute" do
it_behaves_like 'plain attribute' do
let(:attribute) { attribute }
let(:attribute_value) { 'asd' }
end
end
end
end
19 changes: 19 additions & 0 deletions spec/lol/statistic_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "lol"
require "spec_helper"

include Lol

describe Statistic do
it_behaves_like 'Lol model' do
let(:valid_attributes) { { id: 1 } }
end

%w(id name value).each do |attribute|
describe "#{attribute} attribute" do
it_behaves_like 'plain attribute' do
let(:attribute) { attribute }
let(:attribute_value) { 'asd' }
end
end
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require "codeclimate-test-reporter"
require "vcr"

Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f}

SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
Coveralls::SimpleCov::Formatter,
SimpleCov::Formatter::HTMLFormatter,
Expand Down
Loading

0 comments on commit 013a0c1

Please sign in to comment.