Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Little resistant to adding personal editor settings to the repo, even mine.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You know, there are different levels of "editor settings" (convention): "team" (it can have several projects), "projects" (where we're), "personal".

And I respect any level of preferences, but was trying to introduce project-wide convention. To keep project's source code in one style (not to mix space-indentation with tabs, etc.)

If you're against it — I can remove. But in my practice it helps, not to specific maintainers or contributors, but to a project, and all its members (how to write code for the specific project despite personal preferences).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like an additional example:

I have personal taste in tab-indentation. Even in Ruby. But I don't enforce it. And I have a code editor plugin, which toggles my settings (indentation, line length / wrapping, encoding, etc.) to the specific open project.

I wish similar to everyone. You can write as you want, but in a single project we're all should stick together, with chosen rules.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlexWayfer fair points; how about this, fine with leaving it if we bump the .editorconfig / rubocop line length to the current rubocop default of 120, everything else seems fairly reasonable for what I'd expect from a ruby ide.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlexWayfer fair points; how about this, fine with leaving it if we bump the .editorconfig / rubocop line length to the current rubocop default of 120, everything else seems fairly reasonable for what I'd expect from a ruby ide.

What is "ruby ide"? I've been using Atom Editor, now I use Pulsar Editor. I can use VS Code — whatever.

You can open EditorConfig's site: https://editorconfig.org/#pre-installed

Some editors work with it "out-of-box", some requires (simple) plugins. Even RubyMine, Sublime, etc.

Again: if I have editor's setting for tab-indentation — I'd have problems with this project with 2-spaces indentation. Which I'm not against, this config is just a kind of automatization for project-to-project work.

The same for end-of-line chars between UNIX and Windows users on the same projects, etc. Nobody against Windows users or their end-line chars, we just should keep the same chars along the project.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlexWayfer a Ruby IDE, a Integrated development environment is any feature rich software development application. Basically, I'd expect something like VS code, etc to default to most of those settings (other than the line length).

Rubocop's LineLength Cop defaults to 120 so I'd want to keep to that value.


[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
max_line_length = 100
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ spec/secret.rb
test/tmp
test/version_tmp
tmp
spec/examples.txt
5 changes: 4 additions & 1 deletion .rspec
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
-f d -c --warning
--require spec_helper
--format doc
--color
--warning
21 changes: 21 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require:
- rubocop-rake
- rubocop-rspec

AllCops:
NewCops: enable
TargetRubyVersion: 2.4

Layout/LineLength:
Max: 100

Metrics/BlockLength:
Exclude:
- spec/**/*.rb

Naming/FileName:
Exclude:
- lib/steam-api.rb
Naming/AccessorMethodName:
Exclude:
- lib/steam-api/steam/**/*
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

# Specify your gem's dependencies in simple-steam.gemspec
Expand Down
4 changes: 4 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new

task default: :spec

desc 'Alias for spec task'
task test: :spec
5 changes: 2 additions & 3 deletions lib/steam-api.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'faraday'
require 'json'

Expand All @@ -6,7 +8,6 @@
require_relative 'steam-api/client'
require_relative 'steam-api/exceptions'
require_relative 'steam-api/response'
require_relative 'steam-api/helpers'
require_relative 'steam-api/steam/apps'
require_relative 'steam-api/steam/economy'
require_relative 'steam-api/steam/news'
Expand All @@ -15,5 +16,3 @@
require_relative 'steam-api/steam/store'
require_relative 'steam-api/steam/user'
require_relative 'steam-api/steam/user_stats'

include Steam::Helpers
14 changes: 10 additions & 4 deletions lib/steam-api/client.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# frozen_string_literal: true

module Steam
# Client object used to communicate with the steam webapi
class Client
def initialize(url)
# @param [String] api The endpoint of the API
# @param [String] base_url the root uri for steam's API
# @return [Steam::Client] The Client
def initialize(api, base_url: 'https://api.steampowered.com')
url = File.join(base_url, api)
@conn = Faraday.new(url: url)
end

Expand All @@ -12,16 +18,16 @@ def initialize(url)
def get(resource, params: {}, key: Steam.apikey)
params[:key] = key
response = @conn.get resource, params
JSON.parse(response.body)
# response
response = JSON.parse(response.body)
response = Response.new response
rescue JSON::ParserError
puts response.body
# If the steam web api returns an error it's virtually never in json, so
# lets pretend that we're getting some sort of consistant response
# for errors.
raise Steam::UnavailableError if response.status == '503'

{ error: '500 Internal Server Error' }
Response.new error: '500 Internal Server Error'
end
end
end
2 changes: 2 additions & 0 deletions lib/steam-api/exceptions.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Steam
# Error returning the requested object from the Steam API
class SteamError < StandardError
Expand Down
15 changes: 0 additions & 15 deletions lib/steam-api/helpers.rb

This file was deleted.

52 changes: 29 additions & 23 deletions lib/steam-api/response.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
# frozen_string_literal: true

module Steam
# Since the steam responses are so randomly inconsistant we're making a new
# class to manage the responses.
# FIXME: move all hash extensions here once the gem is finished and make
class Response < Hash
# def parse_key(key)
# fail Steam::JSONError unless self.key?(key)
# self[key]
# end
end
end
def initialize(raw_response)
super()

class Hash
# Simple method to access a nested field, since Valve seems to like
# nesting their json a few levels on every request.
# @param [String] key The key to extract from the hash
def parse_key(key)
raise Steam::JSONError unless key?(key)
raw_response.each do |key, value|
self[key] =
case value
when Hash then self.class.new(value)
else value
end
end
end

self[key]
end
# Simple method to access a nested field, since Valve seems to like
# nesting their json a few levels on every request.
# @param [String] key The key to extract from the hash
def parse_key(key)
raise Steam::JSONError unless key?(key)

self[key]
end

# Many responses from the apis (but not all) include a success
# field, so this allows us to check it wiht minimal fuss.
# @param [String] success_condition what the success condition should be
# @return [Boolean] Returns true or raises an exception.
def check_success(success_condition: true)
success = parse_key('success')
raise Steam::SteamError unless success == success_condition
# Many responses from the apis (but not all) include a success
# field, so this allows us to check it wiht minimal fuss.
# @param [String] success_condition what the success condition should be
# @return [Boolean] Returns true or raises an exception.
def check_success(success_condition: true)
success = parse_key('success')
raise Steam::SteamError unless success == success_condition

true
true
end
end
end
2 changes: 2 additions & 0 deletions lib/steam-api/ruby/hash.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# Takes a hash and converts it into a URL encoded parameter string.
# NOTE: this does not do any uri escaping at this point, since all args
# should be numeric.
Expand Down
23 changes: 10 additions & 13 deletions lib/steam-api/steam.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
# Base class def
# frozen_string_literal: true

# Base class definition
module Steam
@apikey = ENV['STEAM_API_KEY']
class << self
attr_writer :apikey

def self.apikey
if @apikey.nil?
unless ENV.key?('STEAM_API_KEY')
raise ArgumentError, 'Please set your Steam API key.'
end
def apikey
return @apikey if @apikey

@apikey = ENV['STEAM_API_KEY']
end
@apikey
end
raise ArgumentError, 'Please set your Steam API key.' unless ENV.key?('STEAM_API_KEY')

def self.apikey=(key)
@apikey = key
@apikey = ENV.fetch('STEAM_API_KEY')
end
end
end
75 changes: 39 additions & 36 deletions lib/steam-api/steam/apps.rb
Original file line number Diff line number Diff line change
@@ -1,47 +1,50 @@
# frozen_string_literal: true

module Steam
# A Ruby DSL for communicating with the Apps portion of the Steam Web API.
# @see https://developer.valvesoftware.com/wiki/Steam_Web_API
# @since 1.0.0
module Apps
# Get Steam Applications
# @return [Hash] A list of objects containing the title and app ID of
# each program available in the store.
# @see http://wiki.teamfortress.com/wiki/WebAPI/GetAppList
def self.get_all
response = client.get('GetApplist/v2')
.parse_key('applist')
.parse_key('apps')
response
end
class << self
# Get Steam Applications
# @return [Hash] A list of objects containing the title and app ID of
# each program available in the store.
# @see http://wiki.teamfortress.com/wiki/WebAPI/GetAppList
def get_all
client.get('GetApplist/v2').parse_key('applist').parse_key('apps')
end

# Get Servers at Address
# @param [String] addr IP or IP:queryport to list
# @return [Hash] A hash containing the API response
# @see http://wiki.teamfortress.com/wiki/WebAPI/GetServersAtAddress
def self.get_servers(addr: nil, api_version: 'v1')
response = client.get "GetServersAtAddress/#{api_version}",
params: { addr: ERB::Util.url_encode(addr) }
response = response.parse_key('response')
response.check_success
response.parse_key('servers')
end
# Get Servers at Address
# @param [String] addr IP or IP:queryport to list
# @return [Hash] A hash containing the API response
# @see http://wiki.teamfortress.com/wiki/WebAPI/GetServersAtAddress
def get_servers(addr: nil, api_version: 'v1')
response = client.get(
"GetServersAtAddress/#{api_version}", params: { addr: ERB::Util.url_encode(addr) }
)
response = response.parse_key('response')
response.check_success
response.parse_key('servers')
end

# Check if a given version of an App is current
# @param [Fixnum] appid AppID of game
# @param [Fixnum] version The installed version of the game
# @return [Hash] A hash containing the API response
# @see http://wiki.teamfortress.com/wiki/WebAPI/UpToDateCheck
def self.up_to_date(appid: nil, version: 'v1', api_version: 'v1')
response = client.get "UpToDateCheck/#{api_version}",
params: { appid: appid, version: version }
response = response.parse_key('response')
response.check_success
response.delete('success')
response
end
# Check if a given version of an App is current
# @param [Fixnum] appid AppID of game
# @param [Fixnum] version The installed version of the game
# @return [Hash] A hash containing the API response
# @see http://wiki.teamfortress.com/wiki/WebAPI/UpToDateCheck
def up_to_date(appid: nil, version: 'v1', api_version: 'v1')
response = client.get(
"UpToDateCheck/#{api_version}", params: { appid: appid, version: version }
)
response = response.parse_key('response')
response.check_success
response.delete('success')
response
end

def self.client
build_client 'ISteamApps'
def client
Steam::Client.new 'ISteamApps'
end
end
end
end
Loading