Skip to content

Instantly share code, notes, and snippets.

@lucashungaro
Created June 27, 2012 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucashungaro/3001503 to your computer and use it in GitHub Desktop.
Save lucashungaro/3001503 to your computer and use it in GitHub Desktop.
DIP snippet 1
class Game < ActiveRecord::Base
belongs_to :category
validates_presence_of :title, :category_id, :description,
:price, :platform, :year
end
 
class GamePriceService
attr_accessor :game
 
# we could use a config file
BASE_URL = "http://thegamedatabase.com/api/game"
API_KEY = "ek2o1je"
 
def initialize(game)
self.game = game
end
 
def get_price
data = open("#{BASE_URL}/#{game.name}/price?api_key=#{API_KEY}")
JsonParserLib.parse(data)
end
end
 
class GamePrinter
attr_accessor :game
 
def initialize(game)
self.game = game
end
 
def print
price_service = GamePriceService.new(game)
<<-EOF
#{game.name}, #{game.platform}, #{game.year}

current value is #{price_service.get_price[:value]}
EOF
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment