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

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


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

def initialize(attrs = {})
  per_page = attrs.delete(:per_page)

  attrs.each { |key, value| instance_variable_set("@#{key}", value) }

  process_html = ->(html) do
    cards_from(html).map { |card| screenshot_from(card) }
  end
  @paginator = ScreenshotPaginator.new(process_html, max_per_page: MAX_PER_PAGE,
                                       per_page: per_page, steam_per_page: per_page)
end

Instance Attribute Details

#idObject (readonly)

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



20
21
22
# File 'lib/rsteamshot/app.rb', line 20

def id
  @id
end

#nameObject (readonly)

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



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

def name
  @name
end

Class Method Details

.download_apps_list(path) ⇒ Object

Public: Writes a JSON file at the given location with the latest list of apps on Steam.

path - a String file path

Returns nothing.



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

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

.search(raw_query, apps_list_path) ⇒ Object

Public: Find Steam apps by name.

raw_query - a String search query for an app or game on Steam apps_list_path - a String file path to the JSON file produced by #download_apps_list

Returns an Array of Rsteamshot::Apps.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rsteamshot/app.rb', line 42

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

  unless apps_list_path
    raise BadAppsFile, 'no path given to JSON apps list from Steam'
  end

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

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

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

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

  query = raw_query.downcase
  results = []
  apps.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

#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.



110
111
112
113
114
115
# File 'lib/rsteamshot/app.rb', line 110

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