Class: SteamCondenser::Community::GameStatsSchema

Inherits:
Object
  • Object
show all
Includes:
Cacheable
Defined in:
lib/steam-condenser/community/game_stats_schema.rb

Constant Summary collapse

@@default_language =
:english

Instance Attribute Summary collapse

Attributes included from Cacheable

#fetch_time

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Cacheable

#cache, #cache_id_value, #cache_ids, #cached_instance, #fetched?, included

Constructor Details

#initialize(app_id, language, fetch = true, bypass_cache = false) ⇒ GameStatsSchema

Returns a new instance of GameStatsSchema

Parameters:

  • fetch (Boolean)

    if true the object's data is fetched after creation

  • bypass_cache (Boolean)

    if true the object's data is fetched again even if it has been cached already


40
41
42
# File 'lib/steam-condenser/community/game_stats_schema.rb', line 40

def initialize(app_id, language = @@default_language)
  @app_id = app_id
end

Instance Attribute Details

#achievementsHash<String, GameAchievement> (readonly)

Returns:


21
22
23
# File 'lib/steam-condenser/community/game_stats_schema.rb', line 21

def achievements
  @achievements
end

#app_idObject (readonly)

Returns the value of attribute app_id


23
24
25
# File 'lib/steam-condenser/community/game_stats_schema.rb', line 23

def app_id
  @app_id
end

#app_nameObject (readonly)

Returns the value of attribute app_name


25
26
27
# File 'lib/steam-condenser/community/game_stats_schema.rb', line 25

def app_name
  @app_name
end

#app_versionObject (readonly)

Returns the value of attribute app_version


27
28
29
# File 'lib/steam-condenser/community/game_stats_schema.rb', line 27

def app_version
  @app_version
end

#dataObject (readonly)

Returns the value of attribute data


29
30
31
# File 'lib/steam-condenser/community/game_stats_schema.rb', line 29

def data
  @data
end

Class Method Details

.default_languageObject


31
32
33
# File 'lib/steam-condenser/community/game_stats_schema.rb', line 31

def self.default_language
  @@default_language
end

.default_language=(language) ⇒ Object


35
36
37
# File 'lib/steam-condenser/community/game_stats_schema.rb', line 35

def self.default_language=(language)
  @@default_language = language.to_sym
end

Instance Method Details

#achievement(api_name) ⇒ Object


44
45
46
# File 'lib/steam-condenser/community/game_stats_schema.rb', line 44

def achievement(api_name)
  @achievements[api_name]
end

#achievement_translations(language) ⇒ Object


71
72
73
74
# File 'lib/steam-condenser/community/game_stats_schema.rb', line 71

def achievement_translations(language)
  add_language(language) unless @achievement_translations.key? language
  @achievement_translations[language]
end

#add_language(language) ⇒ Object (protected)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/steam-condenser/community/game_stats_schema.rb', line 91

def add_language(language)
  schema = fetch_language language

  @datum_names[language] = {}
  schema[:availableGameStats][:stats].each do |data|
    @datum_names[language][data[:name]] = data[:displayName]
  end

  @achievement_translations[language] = {}
  schema[:availableGameStats][:achievements].each do |data|
    @achievement_translations[language][data[:name]] = {
      :description => data[:description],
      :name => data[:displayName]
    }
  end
end

#datum(api_name) ⇒ Object


76
77
78
# File 'lib/steam-condenser/community/game_stats_schema.rb', line 76

def datum(api_name)
  @data[api_name]
end

#datum_names(language) ⇒ Object


80
81
82
83
# File 'lib/steam-condenser/community/game_stats_schema.rb', line 80

def datum_names(language)
  add_language(language) unless @datum_names.key? language
  @datum_names[language]
end

#fetchObject


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/steam-condenser/community/game_stats_schema.rb', line 48

def fetch
  language = @@default_language
  schema = fetch_language language

  @app_name    = schema[:gameName]
  @app_version = schema[:gameVersion].to_i

  @datum_names = { language => {} }
  @data = Hash[schema[:availableGameStats][:stats].map do |data|
    @datum_names[language][data[:name]] = data[:displayName]
    [data[:name], GameStatsDatum.new(self, data)]
  end]

  @achievement_translations = { language => {} }
  @achievements = Hash[schema[:availableGameStats][:achievements].map do |data|
    @achievement_translations[language][data[:name]] = {
      :description => data[:description],
      :name => data[:displayName]
    }
    [data[:name], GameAchievement.new(self, data)]
  end]
end

#fetch_language(language) ⇒ Object (protected)


108
109
110
111
# File 'lib/steam-condenser/community/game_stats_schema.rb', line 108

def fetch_language(language)
  params = { appid: @app_id, l: language }
  WebApi.json('ISteamUserStats', 'GetSchemaForGame', 2, params)[:game]
end

#inspectObject


85
86
87
# File 'lib/steam-condenser/community/game_stats_schema.rb', line 85

def inspect
  "#<#{self.class}:#@app_id \"#@app_name\" (#@app_version)>"
end