From ff454a22cb5ef81ac9c18b3ccbe3ac20d1c20a4a Mon Sep 17 00:00:00 2001 From: Olga Grabek Date: Sat, 24 Jan 2015 10:58:48 +0100 Subject: [PATCH] correct Game#schema when game does not have schema --- lib/steam_web_api/game.rb | 2 +- spec/fixtures/vcr/game_schema_no_schema.yml | 34 ++++++++++ spec/steam_web_api/game_spec.rb | 70 +++++++++++++-------- 3 files changed, 80 insertions(+), 26 deletions(-) create mode 100644 spec/fixtures/vcr/game_schema_no_schema.yml diff --git a/lib/steam_web_api/game.rb b/lib/steam_web_api/game.rb index 552e1b5..0797cbc 100644 --- a/lib/steam_web_api/game.rb +++ b/lib/steam_web_api/game.rb @@ -19,7 +19,7 @@ def all def schema @response = get('/ISteamUserStats/GetSchemaForGame/v2', appid: game_id, key: SteamWebApi.config.api_key) - build_response('game') { |data| { name: data['gameName'], version: data['gameVersion'], achievements: data['availableGameStats']['achievements'], stats: data['availableGameStats']['stats'] } } + build_response('game') { |data| { name: data['gameName'], version: data['gameVersion'], achievements: data.fetch('availableGameStats') { {} }.fetch('achievements') { [] }, stats: data.fetch('availableGameStats') { {} }.fetch('stats') { [] } } } end def achievement_percentages diff --git a/spec/fixtures/vcr/game_schema_no_schema.yml b/spec/fixtures/vcr/game_schema_no_schema.yml new file mode 100644 index 0000000..b3f4848 --- /dev/null +++ b/spec/fixtures/vcr/game_schema_no_schema.yml @@ -0,0 +1,34 @@ +--- +http_interactions: +- request: + method: get + uri: http://api.steampowered.com/ISteamUserStats/GetSchemaForGame/v2?appid=4760&format=json&key= + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - Faraday v0.9.0 + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Date: + - Sat, 24 Jan 2015 09:51:38 GMT + Expires: + - Sat, 24 Jan 2015 09:51:38 GMT + Content-Type: + - application/json; charset=UTF-8 + Content-Length: + - '18' + body: + encoding: UTF-8 + string: "{\n\t\"game\": {\n\n\t}\n}" + http_version: + recorded_at: Sat, 24 Jan 2015 09:51:38 GMT +recorded_with: VCR 2.9.3 diff --git a/spec/steam_web_api/game_spec.rb b/spec/steam_web_api/game_spec.rb index 6442a6d..676f21f 100644 --- a/spec/steam_web_api/game_spec.rb +++ b/spec/steam_web_api/game_spec.rb @@ -47,33 +47,53 @@ end describe '#schema' do - - it 'returns game schema' do - VCR.use_cassette('game_schema') do - schema = game.schema - expect(schema.name).to eq 'ValveTestApp8930' - expect(schema.version).to eq '108' - expect(schema.achievements.first).to eq( - { - "name" => "ACHIEVEMENT_WIN_WASHINGTON", + + context 'when game has schema' do + + it 'returns game schema' do + VCR.use_cassette('game_schema') do + schema = game.schema + expect(schema.name).to eq 'ValveTestApp8930' + expect(schema.version).to eq '108' + expect(schema.achievements.first).to eq( + { + "name" => "ACHIEVEMENT_WIN_WASHINGTON", + "defaultvalue" => 0, + "displayName" => "First in the Hearts of Your Countrymen", + "hidden" => 0, + "description" => "Beat the game on any difficulty setting as Washington.", + "icon" => "http://cdn.akamai.steamstatic.com/steamcommunity/public/images/apps/8930/4cf17a59d70b2decfd4369de8a7429e7b00f5ab8.jpg", + "icongray" => "http://cdn.akamai.steamstatic.com/steamcommunity/public/images/apps/8930/2ce109f9be6cb3193a385444b9b0b0ffcc7b2219.jpg" + } + ) + expect(schema.stats.first).to eq( + { + "name" => "ESTEAMSTAT_TOTAL_WINS", "defaultvalue" => 0, - "displayName" => "First in the Hearts of Your Countrymen", - "hidden" => 0, - "description" => "Beat the game on any difficulty setting as Washington.", - "icon" => "http://cdn.akamai.steamstatic.com/steamcommunity/public/images/apps/8930/4cf17a59d70b2decfd4369de8a7429e7b00f5ab8.jpg", - "icongray" => "http://cdn.akamai.steamstatic.com/steamcommunity/public/images/apps/8930/2ce109f9be6cb3193a385444b9b0b0ffcc7b2219.jpg" - } - ) - expect(schema.stats.first).to eq( - { - "name" => "ESTEAMSTAT_TOTAL_WINS", - "defaultvalue" => 0, - "displayName" => "Total Games Won." - } - ) - expect(schema.success).to be true - end + "displayName" => "Total Games Won." + } + ) + expect(schema.success).to be true + end + end + end + + context 'when game does not have schema' do + + it 'returns object with empty values' do + VCR.use_cassette('game_schema_no_schema') do + schema = SteamWebApi::Game.new(4760).schema + expect(schema.name).to be_nil + expect(schema.version).to be_nil + expect(schema.achievements).to eq [] + expect(schema.stats).to eq [] + expect(schema.success).to be true + end + end + + end + end