class Itch::Game

Represents a single game and sub-resources

Constants

THEME_DATA

Attributes

id[R]
name[R]
page_url[R]

Public Class Methods

new(agent, map, id = nil, name: nil) click to toggle source
# File lib/itch/game.rb, line 18
def initialize(agent, map, id = nil, name: nil)
  raise Error, "Game ID or name is required" if id.nil? && name.nil?

  @agent = agent
  @map = map

  load_game_info(id, name)
end

Public Instance Methods

css() click to toggle source
# File lib/itch/game.rb, line 33
def css
  theme["css"]
end
css=(css_data) click to toggle source
# File lib/itch/game.rb, line 41
def css=(css_data)
  new_theme = theme
  new_theme["css"] = css_data
  self.theme = new_theme
end
reward(id) click to toggle source
# File lib/itch/game.rb, line 51
def reward(id)
  rewards.find { |reward| reward.id == id }
end
rewards() click to toggle source
# File lib/itch/game.rb, line 47
def rewards
  Rewards.new(@agent, @id)
end
theme() click to toggle source
# File lib/itch/game.rb, line 27
def theme
  JSON.parse(theme_data)["theme"]
rescue StandardError
  {}
end
theme=(theme_data) click to toggle source
# File lib/itch/game.rb, line 37
def theme=(theme_data)
  @agent.post edit_theme_url, theme_post_data(theme_data)
end

Protected Instance Methods

edit_page() click to toggle source
# File lib/itch/game.rb, line 130
def edit_page
  with_login do
    @agent.get edit_url
  end
end
edit_theme_url() click to toggle source
# File lib/itch/game.rb, line 118
def edit_theme_url
  "#{@page_url}/edit"
end
edit_url() click to toggle source
# File lib/itch/game.rb, line 114
def edit_url
  format(Itch::URL::EDIT_GAME, id: @id)
end
form() click to toggle source
# File lib/itch/game.rb, line 122
def form
  edit_page.form_with(action: edit_url)
end
game_page() click to toggle source
# File lib/itch/game.rb, line 126
def game_page
  @agent.get @page_url
end
load_game_info(id, name) click to toggle source
# File lib/itch/game.rb, line 57
def load_game_info(id, name)
  if id
    data = @map.find!(id)
  elsif name
    data = @map.find_by_name!(name)
  else
    raise Error, "Name or ID is required when initializing Itch::Game"
  end

  @id = data[:id]
  @page_url = data[:url]
  @name = data[:name]
end
theme_csrf_token() click to toggle source

rubocop:enable Metrics/MethodLength, Metrics/AbcSize

# File lib/itch/game.rb, line 98
def theme_csrf_token
  page = with_login { game_page }

  page.at_css("meta[name='csrf_token']")["value"]
end
theme_data() click to toggle source
# File lib/itch/game.rb, line 104
def theme_data
  page = with_login { game_page }

  script = page.css("script").find do |node|
    node.text =~ THEME_DATA
  end.text

  THEME_DATA.match(script)[1]
end
theme_post_data(new_theme) click to toggle source

rubocop:disable Metrics/MethodLength, Metrics/AbcSize

# File lib/itch/game.rb, line 72
def theme_post_data(new_theme)
  filtered_theme = new_theme.reject do |k, _v|
    %w[background_image background_repeat background_position banner_image banner_position].include?(k)
  end

  post_data = filtered_theme.transform_keys do |k|
    "layout[#{k}]"
  end.merge({ "csrf_token" => theme_csrf_token })

  if new_theme["background_image"]
    post_data["layout[background_image][image_id]"] = new_theme["background_image"]["id"]
    post_data["layout[background_image][repeat]"] = new_theme["background_repeat"]
    post_data["layout[background_image][position]"] = new_theme["background_position"]
  end

  if new_theme["banner_image"]
    post_data["layout[banner_image][id]"] = new_theme["banner_image"]["id"]
    post_data["layout[banner_image][position]"] = new_theme["banner_position"]
  end

  post_data["header_font_family"] ||= "_default"

  post_data
end