Skip to content

Commit

Permalink
added support for image
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Adler committed Feb 11, 2015
1 parent b690e3a commit 9f706e2
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 35 deletions.
1 change: 1 addition & 0 deletions .ruby-gemset
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
giant_bomb_api
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby-2.2.0
4 changes: 1 addition & 3 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
source "https://rubygems.org"
gemspec

require 'byebug'
gemspec
11 changes: 10 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ GEM
slop (~> 3.6)
codeclimate-test-reporter (0.4.5)
simplecov (>= 0.7.1, < 1.0.0)
coderay (1.1.0)
columnize (0.9.0)
debugger-linecache (1.2.0)
diff-lcs (1.2.5)
Expand All @@ -31,9 +32,17 @@ GEM
faraday (>= 0.7.4, < 0.10)
i18n (0.7.0)
json (1.8.2)
method_source (0.8.2)
minitest (5.5.1)
multi_json (1.10.1)
multipart-post (2.0.0)
pry (0.10.1)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
pry-byebug (3.0.1)
byebug (~> 3.4)
pry (~> 0.10)
rake (10.1.1)
rspec (3.1.0)
rspec-core (~> 3.1.0)
Expand Down Expand Up @@ -62,9 +71,9 @@ PLATFORMS
ruby

DEPENDENCIES
byebug
codeclimate-test-reporter
giant_bomb_api!
pry-byebug
rake (~> 10.1.0)
rspec (~> 3.0)
timecop
2 changes: 1 addition & 1 deletion giant_bomb_api.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Gem::Specification.new do |s|

s.add_development_dependency 'rake', '~> 10.1.0'
s.add_development_dependency 'rspec', '~> 3.0'
s.add_development_dependency 'byebug'
s.add_development_dependency 'pry-byebug'
s.add_development_dependency 'timecop'
s.add_development_dependency 'codeclimate-test-reporter'

Expand Down
3 changes: 3 additions & 0 deletions lib/giant_bomb_api.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
require 'active_support/all'

require "giant_bomb_api/resource"
require "giant_bomb_api/resource_value_setter"
require "giant_bomb_api/resource/game"
require "giant_bomb_api/resource/platform"
require "giant_bomb_api/resource/game_rating"
require "giant_bomb_api/resource/factory"
require "giant_bomb_api/resource/image"
require "giant_bomb_api/resource/character"
require "giant_bomb_api/resource/company"
require "giant_bomb_api/response"
Expand Down
6 changes: 3 additions & 3 deletions lib/giant_bomb_api/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ module GiantBombApi
module Resource

def self.extended(base)
base.instance_variable_set("@resource_attributes", [])
base.instance_variable_set("@resource_attributes", {})
base.include ResourceValueSetter
end

def resource_attribute(*attributes)
def resource_attribute(*attributes, resource_name: nil)
attributes.each do |attribute_name|
instance_variable_get("@resource_attributes") << attribute_name
instance_variable_get("@resource_attributes")[attribute_name] = resource_name

class_eval do
attr_accessor attribute_name
Expand Down
9 changes: 2 additions & 7 deletions lib/giant_bomb_api/resource/factory.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
require 'active_support/inflector'

module GiantBombApi
class Resource::Factory

def self.init_resource_from(json)
api_detail_url = json["api_detail_url"]
return if api_detail_url.nil?

resource_name = discover_resource_name(api_detail_url)
def self.init_resource_from(json, resource_name = nil)
resource_name = discover_resource_name(json["api_detail_url"]) if resource_name.nil?
return if resource_name.nil?

init_object(resource_name, json)
Expand Down
2 changes: 2 additions & 0 deletions lib/giant_bomb_api/resource/game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class Resource::Game
resource_attribute :deck, :description, :expected_release_day, :expected_release_month
resource_attribute :expected_release_quarter, :expected_release_year, :id, :name
resource_attribute :number_of_user_reviews, :original_release_date

resource_attribute :image, resource_name: :image
resource_attribute :platforms
resource_attribute :original_game_rating

Expand Down
16 changes: 16 additions & 0 deletions lib/giant_bomb_api/resource/image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module GiantBombApi
class Resource::Image
extend GiantBombApi::Resource

resource_attribute :icon_url
resource_attribute :medium_url
resource_attribute :screen_url
resource_attribute :small_url
resource_attribute :super_url
resource_attribute :thumb_url
resource_attribute :tiny_url

private

end
end
7 changes: 3 additions & 4 deletions lib/giant_bomb_api/resource_value_setter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@ module GiantBombApi
module ResourceValueSetter

def initialize(json)
@json = json
init_resource_attributes_from(json)
end

def init_resource_attributes_from(hash)
self.class.instance_variable_get("@resource_attributes").each do |attribute|
self.class.instance_variable_get("@resource_attributes").each do |attribute, resource_name|
attribute = attribute.to_s

hash_value = hash[attribute]
value = nil

unless hash_value.nil?
if hash_value.is_a? Hash
value = GiantBombApi::Resource::Factory.init_resource_from(hash_value)
value = GiantBombApi::Resource::Factory.init_resource_from(hash_value, resource_name)
elsif hash_value.is_a? Array
value = hash_value.map { |json| GiantBombApi::Resource::Factory.init_resource_from(json) }.compact
value = hash_value.map { |json| GiantBombApi::Resource::Factory.init_resource_from(json, resource_name) }.compact
else
value = hash_value
end
Expand Down
36 changes: 20 additions & 16 deletions spec/lib/resource/factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,31 @@
describe GiantBombApi::Resource::Factory do

let(:game_json) {{'api_detail_url' => 'http://www.giantbomb.com/api/game/3030-41693/', 'some' => 'data' }}
let(:platform_json) {{'api_detail_url' => 'http://www.giantbomb.com/api/platform/3045-146/', 'some' => 'data' }}
let(:unknown_json) {{'api_detail_url' => 'http://www.giantbomb.com/api/unknown/1234-567/', 'some' => 'data' }}
let(:image_json) {{"icon_url": "http://url","medium_url": "http://url2","screen_url": "http://url3"}}

describe '#init_resource_from' do
context 'when a json with game-api-detail-url is given' do
let(:game) { double('Game') }
it 'initializes a GiantBombApi::Resource::Game and return it' do
expect(GiantBombApi::Resource::Game).to receive(:new).with(game_json).and_return(game)
expect(GiantBombApi::Resource::Factory.init_resource_from(game_json)).to eq game
context "when a json without resource_name is given" do
context 'and a api_detail_url is present' do
let(:game) { double('Game') }

it 'initializes the object based on the found resource_name in the api_detail_url' do
expect(GiantBombApi::Resource::Game).to receive(:new).with(game_json).and_return(game)
expect(GiantBombApi::Resource::Factory.init_resource_from(game_json)).to eq game
end

context 'and an unknown-resource-name is discovered' do
it 'will not fail and return nil' do
expect(GiantBombApi::Resource::Factory.init_resource_from(unknown_json)).to be_nil
end
end
end
end
context 'when a json with platform-api-detail-url is given' do
let(:platform) { double('Platform') }
it 'initializes a GiantBombApi::Resource::Platform and return it' do
expect(GiantBombApi::Resource::Platform).to receive(:new).with(platform_json).and_return(platform)
expect(GiantBombApi::Resource::Factory.init_resource_from(platform_json)).to eq platform
end
end
context 'when an unknow-json is given' do
it 'will not fail and return nil' do
expect(GiantBombApi::Resource::Factory.init_resource_from(unknown_json)).to be_nil
context "when a json with resource_name is given" do
let(:image) { double('Image') }
it 'initializes the object based on the given resource_name' do
expect(GiantBombApi::Resource::Image).to receive(:new).with(image_json).and_return(image)
expect(GiantBombApi::Resource::Factory.init_resource_from(image_json, :image)).to eq image
end
end
end
Expand Down
13 changes: 13 additions & 0 deletions spec/lib/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@
expect(result.original_release_date).to eq "2014-10-07 00:00:00"
end

it 'sets the correct platforms' do
result = response.results.first

expect(result.image).to be_present
expect(result.image.icon_url).to eq "http://static.giantbomb.com/uploads/square_avatar/8/82063/2560905-driveclub.jpg"
expect(result.image.medium_url).to eq "http://static.giantbomb.com/uploads/scale_medium/8/82063/2560905-driveclub.jpg"
expect(result.image.screen_url).to eq "http://static.giantbomb.com/uploads/screen_medium/8/82063/2560905-driveclub.jpg"
expect(result.image.small_url).to eq "http://static.giantbomb.com/uploads/scale_small/8/82063/2560905-driveclub.jpg"
expect(result.image.super_url).to eq "http://static.giantbomb.com/uploads/scale_large/8/82063/2560905-driveclub.jpg"
expect(result.image.thumb_url).to eq "http://static.giantbomb.com/uploads/scale_avatar/8/82063/2560905-driveclub.jpg"
expect(result.image.tiny_url).to eq "http://static.giantbomb.com/uploads/square_mini/8/82063/2560905-driveclub.jpg"
end

it 'sets the correct platforms' do
result = response.results.first

Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'giant_bomb_api'
require 'rspec'
require 'timecop'
require 'pry-byebug'

RSpec.configure do |config|

Expand Down

0 comments on commit 9f706e2

Please sign in to comment.