class Gogcom::Game

Public Class Methods

new(options) click to toggle source
# File lib/gogcom/game.rb, line 4
def initialize(options)
  @name = options[:name] || nil
end

Public Instance Methods

get() click to toggle source

Main method to get game data

# File lib/gogcom/game.rb, line 9
def get()
  parse(fetch())
end

Private Instance Methods

fetch() click to toggle source

Fetches raw data and parses as JSON object

@return [Object]

# File lib/gogcom/game.rb, line 18
def fetch()
  name = urlfy(@name)
  page = Net::HTTP.get(URI("http://www.gog.com/game/" + name))
  data = JSON.parse(page[/(?<=var gogData = )(.*)(?=;)/,1])
  data
end
get_avg_rating(data) click to toggle source
# File lib/gogcom/game.rb, line 70
def get_avg_rating(data)
  data["gameProductData"]["rating"].to_s.insert(1, ".")
end
get_avg_ratings_count(data) click to toggle source
# File lib/gogcom/game.rb, line 74
def get_avg_ratings_count(data)
  data["gameProductData"]["votesCount"]
end
get_bonus_content(data) click to toggle source
# File lib/gogcom/game.rb, line 112
def get_bonus_content(data)
  bonusContent = []
  bonusContent_raw = data["gameProductData"]["bonusContent"]
  
  bonusContent_raw["hidden"].each do |bc|
    bonusContent.push(bc["name"])
  end

  bonusContent_raw["visible"].each do |bc|
    bonusContent.push(bc["name"])
  end

  bonusContent
end
get_description(data) click to toggle source
# File lib/gogcom/game.rb, line 62
def get_description(data)
  data["gameProductData"]["description"]["full"]
end
get_developer(data) click to toggle source
# File lib/gogcom/game.rb, line 94
def get_developer(data)
  data["gameProductData"]["developer"]["name"]
end
get_download_size(data) click to toggle source
# File lib/gogcom/game.rb, line 54
def get_download_size(data)
  data["gameProductData"]["downloadSize"]
end
get_genres(data) click to toggle source
# File lib/gogcom/game.rb, line 44
def get_genres(data)
  genres = []
  genres_raw = data["gameProductData"]["genres"]    
  genres_raw.each do |genre|
    genres.push(genre["name"])
  end

  genres
end
get_languages(data) click to toggle source
# File lib/gogcom/game.rb, line 90
def get_languages(data)
  data["gameProductData"]["languages"]
end
get_modes(data) click to toggle source
# File lib/gogcom/game.rb, line 102
def get_modes(data)
  game_modes = []
  modes_raw = data["gameProductData"]["modes"]
  modes_raw.each do |mode|
    game_modes.push(mode["title"])
  end

  game_modes
end
get_pegi_age(data) click to toggle source
# File lib/gogcom/game.rb, line 144
def get_pegi_age(data)
  data["gameProductData"]["pegiAge"] 
end
get_platforms(data) click to toggle source
# File lib/gogcom/game.rb, line 78
def get_platforms(data)
  platforms = []
  platforms_raw = data["gameProductData"]["worksOn"]
  platforms_raw.each do |platform|
    if(platform[1])
      platforms.push(platform[0])
    end
  end

  platforms
end
get_price(data) click to toggle source
# File lib/gogcom/game.rb, line 66
def get_price(data)
  data["gameProductData"]["price"]["symbol"] + data["gameProductData"]["price"]["amount"]
end
get_publisher(data) click to toggle source
# File lib/gogcom/game.rb, line 98
def get_publisher(data)
  data["gameProductData"]["publisher"]["name"]
end
get_release_date(data) click to toggle source
# File lib/gogcom/game.rb, line 58
def get_release_date(data)
  data["gameProductData"]["releaseDate"]
end
get_reviews(data) click to toggle source
# File lib/gogcom/game.rb, line 127
def get_reviews(data)
  reviews = []
  reviews_raw = data["reviews"]["pages"][0]
  reviews_raw.each do |review|
    r = Review.new

    r.title = review["title"]
    r.rating = review["rating"].to_s.insert(1, ".")
    r.author = review["author"]["name"]
    r.body = review["description"]
    
    reviews.push(r)
  end

  reviews
end
get_title(data) click to toggle source
# File lib/gogcom/game.rb, line 40
def get_title(data)
  data["gameProductData"]["title"]
end
parse(data) click to toggle source

Parses raw data and returns game item.

@param [Object] @return [Struct]

# File lib/gogcom/game.rb, line 29
def parse(data)
  game = GameItem.new(get_title(data), get_genres(data),
    get_download_size(data), get_release_date(data), get_description(data),
    get_price(data), get_avg_rating(data), get_avg_ratings_count(data),
    get_platforms(data), get_languages(data), get_developer(data),
    get_publisher(data), get_modes(data), get_bonus_content(data),
    get_reviews(data), get_pegi_age(data))      

  game
end
urlfy(name) click to toggle source

Converts “Game Name” to “game_name”

@param [String] @return [String]

# File lib/gogcom/game.rb, line 152
def urlfy(name)
  name = name.gsub(/[^0-9A-Za-z\s]/, '').gsub(/\s/, '_').downcase
  name
end