-
-
Save dawidlenkiewicz/6116862 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Game < ActiveRecord::Base | |
| belongs_to :category | |
| validates_presence_of :title, :category_id, :description, | |
| :price, :platform, :year | |
| def get_official_price | |
| open("http://thegamedatabase.com/api/game/#{name}/price?api_key=ek2o1je") | |
| end | |
| def print | |
| <<-EOF | |
| #{name}, #{platform}, #{year} | |
| current value is #{get_official_price} | |
| EOF | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Game < ActiveRecord::Base | |
| belongs_to :category | |
| validates_presence_of :title, :category_id, :description, | |
| :price, :platform, :year | |
| end | |
| class GamePriceService | |
| attr_accessor :game | |
| 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 GamePresenter | |
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Game < ActiveRecord::Base | |
| belongs_to :category | |
| validates_presence_of :title, :category_id, :description, | |
| :price, :platform, :year | |
| def price | |
| price = read_attribute(:price) | |
| unless price.present? | |
| update_attribute(:price, GamePriceService.new(self).get_price) | |
| read_attribute(:price) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment