Class: Rsteamshot::App

Inherits:
Object
  • Object
show all
Defined in:
lib/rsteamshot/app.rb

Overview

Public: Represents a Steam app, like a video game. Used to fetch the screenshots that were taken in that app that Steam users have uploaded.

Defined Under Namespace

Classes: BadAppsFile, BadConfiguration

Constant Summary collapse

MAX_PER_PAGE =

Public: You can fetch this many screenshots at once.

50
APPS_LIST_URL =

Public: The API URL to get a list of apps on Steam.

'http://api.steampowered.com/ISteamApps/GetAppList/v2'
VALID_ORDERS =

Public: How to sort screenshots when they are being retrieved.

%w[mostrecent toprated trendday trendweek trendthreemonths
trendsixmonths trendyear].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ App

Public: Initialize a Steam app with the given attributes.

attrs - the Hash of attributes for this app

:id - the String or Integer app ID
:name - the String name of the app
:per_page - how many results to get in each page; defaults to 10; valid range: 1-50;
            Integer


139
140
141
142
143
# File 'lib/rsteamshot/app.rb', line 139

def initialize(attrs = {})
  attrs.each { |key, value| instance_variable_set("@#{key}", value) }
  @per_page ||= 10
  initialize_paginator
end

Instance Attribute Details

#idObject (readonly)

Public: Returns the ID of the Steam app as an Integer or String.



24
25
26
# File 'lib/rsteamshot/app.rb', line 24

def id
  @id
end

#nameObject (readonly)

Public: Returns the String name of the Steam app, or nil.



27
28
29
# File 'lib/rsteamshot/app.rb', line 27

def name
  @name
end

#per_pageObject

Public: Returns the number of screenshots that will be fetched per page for this app.



30
31
32
# File 'lib/rsteamshot/app.rb', line 30

def per_page
  @per_page
end

Class Method Details

.download_apps_listObject

Public: Writes a JSON file at the location specified in ‘Rsteamshot.configuration` with the latest list of apps on Steam. Will be automatically called by #list.

Returns nothing.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rsteamshot/app.rb', line 36

def self.download_apps_list
  path = Rsteamshot.configuration.apps_list_path

  unless path && path.length > 0
    raise BadConfiguration, 'no path configured for JSON apps list from Steam'
  end

  File.open(path, 'w') do |file|
    IO.copy_stream(open(APPS_LIST_URL), file)
  end
end

.find_by_id(id) ⇒ Object

Public: Find a Steam app by its ID.

id - the String or Integer ID of a game or other app on Steam

Returns an Rsteamshot::App or nil.



126
127
128
129
130
# File 'lib/rsteamshot/app.rb', line 126

def self.find_by_id(id)
  id = id.to_i
  app_data = list.detect { |data| data['appid'] == id }
  new(id: app_data['appid'], name: app_data['name']) if app_data
end

.find_by_name(name) ⇒ Object

Public: Find a Steam app by its name, case insensitive.

name - the String name of a game or other app on Steam

Returns an Rsteamshot::App or nil.



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rsteamshot/app.rb', line 109

def self.find_by_name(name)
  apps = search(name)
  return if apps.length < 1

  exact_match = apps.detect { |app| app.name.downcase == name }
  return exact_match if exact_match

  app = apps.shift
  app = apps.shift while app.name.downcase =~ /\btrailer\b/ && apps.length > 0
  app
end

.listObject

Public: Read the JSON file configured in ‘apps_list_path` and get a list of Steam apps. Will download the latest list of Steam apps to `apps_list_path` if the file does not already exist.

Returns an Array of Hashes for all the Steam apps.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rsteamshot/app.rb', line 57

def self.list
  @@list ||= begin
    path = Rsteamshot.configuration.apps_list_path
    unless path
      raise BadAppsFile, 'no path configured for JSON apps list from Steam'
    end

    download_apps_list unless File.file?(path)
    raise BadAppsFile, "#{path} is not a file" unless File.file?(path)

    json = begin
      JSON.parse(File.read(path))
    rescue JSON::ParserError
      raise BadAppsFile, "#{path} is not a valid JSON file"
    end

    applist = json['applist']
    raise BadAppsFile, "#{path} does not have expected JSON format" unless applist

    apps = applist['apps']
    raise BadAppsFile, "#{path} does not have expected JSON format" unless apps

    apps
  end
end

.reset_listObject

Public: Force the list of Steam apps to be re-downloaded the next time #list is called.



49
50
51
# File 'lib/rsteamshot/app.rb', line 49

def self.reset_list
  @@list = nil
end

.search(raw_query) ⇒ Object

Public: Find Steam apps by name.

raw_query - a String search query for an app or game on Steam

Returns an Array of Rsteamshot::Apps.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rsteamshot/app.rb', line 88

def self.search(raw_query)
  return [] unless raw_query

  query = raw_query.downcase
  results = []
  list.each do |data|
    next unless data['name']

    if data['name'].downcase.include?(query)
      results << new(id: data['appid'], name: data['name'])
    end
  end

  results
end

Instance Method Details

#==(other) ⇒ Object

Public: Check if this App is equivalent to another object.

Returns true if the given object represents the same Steam app.



148
149
150
# File 'lib/rsteamshot/app.rb', line 148

def ==(other)
  other.class == self.class && other.id == id && other.name == name
end

#screenshots(order: nil, page: 1, query: nil) ⇒ Object

Public: Fetch a list of the newest uploaded screenshots for this app on Steam.

order - String specifying which screenshots should be retrieved; choose from mostrecent,

toprated, trendday, trendweek, trendthreemonths, trendsixmonths, and trendyear;
defaults to mostrecent

page - which page of results to fetch; defaults to 1; Integer query - a String of text for searching screenshots

Returns an Array of Rsteamshot::Screenshots.



161
162
163
164
165
166
# File 'lib/rsteamshot/app.rb', line 161

def screenshots(order: nil, page: 1, query: nil)
  return [] unless id

  url = steam_url(order, query, @paginator.per_page)
  @paginator.screenshots(page: page, url: url)
end

#to_hObject

Public: Get a hash representation of this app.

Returns a Hash.



171
172
173
174
175
# File 'lib/rsteamshot/app.rb', line 171

def to_h
  result = { id: id }
  result[:name] = name if name
  result
end

#to_jsonObject

Public: Get a JSON representation of this app.

Returns a String.



180
181
182
# File 'lib/rsteamshot/app.rb', line 180

def to_json
  JSON.pretty_generate(to_h)
end