steam-id2 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ec86282b71c3b0a2d93e517510b3d2e61665dd55
4
- data.tar.gz: 2fd43ccf4fba3dd093cbe227f0f2dd708235de26
3
+ metadata.gz: ee09596078843cb2f0a400b773bfb99c1577e46e
4
+ data.tar.gz: 04544f850fde363f3e85ef340c7c24173d08acd2
5
5
  SHA512:
6
- metadata.gz: a58b02610133e911555d3a2f6f868a63764195373ebd8f3c629666628f584f98333686a72f232b6ac559ca99e7f68db33762e21bcac252cd8b9f1d5918a44673
7
- data.tar.gz: 44f6747c01d2da87c87bbd8680d5e99b2ebd3c62951ce81ab8229098dc5dd8a0db220f1c1b74cf1e07897a2a6c5ec7fba5cde8e92fb7d23b12f968334bc74c33
6
+ metadata.gz: 14c5f853b575116936f27abc8318fc96e89d54c378bdf17748ca68fe8830b40dce7e97c2129fe85a64687bd78067588828911ffbaa795792c2dc673891b90a20
7
+ data.tar.gz: cb02da295fd149a4174c3dc67b659db140345eabacf26a08b1d396b2c742cfe87bc914e6bdad0835078bfa4579b118e3323e8da2bf7678f1d1a4d20e12a114b2
@@ -0,0 +1,43 @@
1
+ # Ruby CircleCI 2.0 configuration file
2
+ #
3
+ # Check https://circleci.com/docs/2.0/language-ruby/ for more details
4
+ #
5
+ version: 2
6
+ jobs:
7
+ build:
8
+ docker:
9
+ - image: circleci/ruby:2.4.1
10
+
11
+ steps:
12
+ - checkout
13
+
14
+ - restore_cache:
15
+ keys:
16
+ - v1-dependencies-{{ checksum "steam-id2.gemspec" }}
17
+ # fallback to using the latest cache if no exact match is found
18
+ - v1-dependencies-
19
+
20
+ - run:
21
+ name: Install Dependencies
22
+ command: |
23
+ bundle install --jobs=4 --retry=3 --path vendor/bundle
24
+
25
+ - save_cache:
26
+ paths:
27
+ - ./vendor/bundle
28
+ key: v1-dependencies-{{ checksum "steam-id2.gemspec" }}
29
+
30
+ - run:
31
+ name: Run Tests
32
+ command: |
33
+ mkdir /tmp/test-results
34
+ bundle exec rspec --format RspecJunitFormatter \
35
+ --out /tmp/test-results/rspec.xml \
36
+ --format progress
37
+
38
+ - store_test_results:
39
+ path: /tmp/test-results
40
+
41
+ - store_artifacts:
42
+ path: /tmp/test-results
43
+ destination: test-results
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/CHANGELOG.md CHANGED
@@ -19,6 +19,16 @@ glance - what to expact from upgrading to a new version.
19
19
  ### Removed
20
20
 
21
21
 
22
+ ## [0.4.0]
23
+
24
+ ### Added
25
+
26
+ - `SteamID::SteamID` class representing a single Steam ID.
27
+ - `Parser` class allowing to parse various formats of Steam IDs into their
28
+ respective account ID.
29
+ - `SteamID#from_string` convenience method.
30
+
31
+
22
32
  ## [0.3.0] - 2017-05-30
23
33
 
24
34
  ### Added
@@ -0,0 +1,146 @@
1
+ # coding: utf-8
2
+
3
+ require 'steam-condenser'
4
+
5
+ module SteamID
6
+ # Create SteamID objects based on numeric or string-based Steam ID inputs.
7
+ # @see SteamID
8
+ class Parser
9
+ # Pattern to match classical Steam IDs (STEAM_0:...)
10
+ PATTERN_STEAM_ID = /^STEAM_[0-9]:([0-9]):([0-9]+)$/i
11
+ # Pattern to match Steam ID 3 format ([U:1:...])
12
+ PATTERN_STEAM_ID_3 = /^\[?U:([0-9]{1,2}):([0-9]+)\]?$/i
13
+ # Pattern to match Steam ID 64 aka profile ID format (765...):
14
+ # Must have at least 14 digits, else ID_64 - OFFSET would be < 0.
15
+ PATTERN_STEAM_ID_64 = /^(765[0-9]{11,})$/
16
+ # Pattern to match plain account IDs.
17
+ # Not sure what its valid range is - exists with at least 7 and 8 numbers.
18
+ PATTERN_ACCOUNT_ID = /^[0-9]+$/
19
+
20
+ # Pattern to match ID64-based profile URLs.
21
+ PATTERN_COMMUNITY_URL = /^https?:\/\/steamcommunity\.com\/profiles\/(765[0-9]{11,})\/?$/
22
+ # Pattern to match ID3-based profile URLs.
23
+ PATTERN_COMMUNITY_URL_STEAM_ID_3 = /^https?:\/\/steamcommunity\.com\/profiles\/\[U:([0-9]{1,2}):([0-9]+)\]$/i
24
+ # Pattern to match custom-URL-based profile URLs.
25
+ PATTERN_CUSTOM_URL = /^https?:\/\/steamcommunity\.com\/id\/([^\/]+)\/?$/
26
+
27
+ # Initialize parser
28
+ # @param api_key [String] Key for Steam web API. `nil` to disable API
29
+ # functionality.
30
+ def initialize(api_key: nil)
31
+ @api_key = api_key
32
+ if api_key
33
+ ::WebApi.api_key = @api_key.to_s
34
+ end
35
+ end
36
+
37
+ # Create SteamID object based on any kind of recognized Steam ID.
38
+ # @param s [String]: Any form of known Steam ID, profile URL, or 'vanity'/
39
+ # custom URL.
40
+ # @see Parser#from_steam_id Supported formats of Steam IDs
41
+ # @see Parser#from_community_url Supported formats of community URLs
42
+ # @see Parser#from_vanity_url Supported formats of vanity URLs
43
+ # @return [SteamID]
44
+ # @raise [ArgumentError] If the supplied string was not a valid custom URL.
45
+ # @raise [WebApiError] If the Steam API returned an error.
46
+ def from_string(s)
47
+ account_id = nil
48
+
49
+ # Todo: Refactor
50
+ begin
51
+ # Checking for Steam ID first. Most restrictive check, and also does
52
+ # not require a call to Steam Web API.
53
+ account_id = from_steam_id(s)
54
+ rescue ArgumentError
55
+ begin
56
+ # Community URL afterwards, does not require an API call either.
57
+ account_id = from_community_url(s)
58
+ rescue ArgumentError
59
+ begin
60
+ # Trying to resolve as custom/vanity URL now.
61
+ account_id = from_vanity_url(s, steam_api_key: steam_api_key)
62
+ rescue ArgumentError
63
+ end
64
+ end
65
+ end
66
+
67
+ if account_id.nil?
68
+ raise ArgumentError, "Could not convert #{ s } to account id."
69
+ else
70
+ account_id
71
+ end
72
+ end
73
+
74
+ # Create SteamID object based on Steam ID.
75
+ # @param id [String] Steam ID, either of:
76
+ # - Steam ID: STEAM_0:0:24110655
77
+ # - Steam ID 3: U:1:48221310
78
+ # - Steam ID 64: 76561198008487038
79
+ # - Account ID: 48221310
80
+ # @return [SteamID]
81
+ # @raise [ArgumentError] If the supplied string could not be converted to
82
+ # an account ID.
83
+ def from_steam_id(id)
84
+ # In case we get a fixnum.
85
+ id = id.to_s
86
+
87
+ # https://developer.valvesoftware.com/wiki/SteamID#Format
88
+ PATTERN_STEAM_ID.match(id) do |m|
89
+ return SteamID.new(m[1].to_i + m[2].to_i * 2)
90
+ end
91
+
92
+ PATTERN_STEAM_ID_3.match(id) do |m|
93
+ return SteamID.new(m[2].to_i)
94
+ end
95
+
96
+ PATTERN_STEAM_ID_64.match(id) do |m|
97
+ return SteamID.new(m[1].to_i - SteamID::ID_64_OFFSET)
98
+ end
99
+
100
+ # Matching this one last, as not to catch an ID 64 on accident.
101
+ PATTERN_ACCOUNT_ID.match(id) do |m|
102
+ return SteamID.new(id.to_i)
103
+ end
104
+
105
+ # If we get until here, we did not match any regex.
106
+ raise ArgumentError, "#{ id.inspect } is not a supported SteamID."
107
+ end
108
+
109
+ # Create SteamID object based on community URL.
110
+ # @param url [String] community URL: http://steamcommunity.com/profiles/76561198008487038
111
+ # @return [SteamID]
112
+ # @raise [ArgumentError] If the supplied string was not a valid community URL.
113
+ def from_community_url(url)
114
+ PATTERN_COMMUNITY_URL.match(url) do |m|
115
+ return self.from_steam_id(m[1])
116
+ end
117
+
118
+ PATTERN_COMMUNITY_URL_STEAM_ID_3.match(url) do |m|
119
+ return self.from_steam_id(m[2])
120
+ end
121
+
122
+ raise ArgumentError, "#{ url.inspect } is not a supported community URL."
123
+ end
124
+
125
+ # Create SteamID object based on custom URL. Note that this requires the
126
+ # API key to be set.
127
+ # @param url [String] custom URL:
128
+ # - http://steamcommunity.com/id/some-custom-url
129
+ # - some-custom-url
130
+ # @return [SteamID]
131
+ # @raise [ArgumentError] If the supplied string was not a valid custom URL.
132
+ # @raise [WebApiError] If the Steam API returned an error.
133
+ def from_vanity_url(url)
134
+ PATTERN_CUSTOM_URL.match(url) do |m|
135
+ url = m[1]
136
+ end
137
+
138
+ steam_id = SteamId.resolve_vanity_url(url)
139
+ if steam_id.nil?
140
+ raise ArgumentError, "#{ url } was not a valid custom URL."
141
+ else
142
+ from_steam_id(steam_id)
143
+ end
144
+ end
145
+ end
146
+ end
@@ -1,119 +1,42 @@
1
- require 'steam-condenser'
1
+ # coding: utf-8
2
2
 
3
3
  module SteamID
4
- # Module to convert various formats of Steam IDs into an account ID.
5
- module SteamID
6
- STEAM_ID_64_OFFSET = 61197960265728
7
-
8
- PATTERN_STEAM_ID = /^STEAM_[0-9]:([0-9]):([0-9]+)$/i
9
- PATTERN_STEAM_ID_3 = /^\[?U:([0-9]{1,2}):([0-9]+)\]?$/i
10
- # Must be at least 14 numbers, else ID_64 - OFFSET would be < 0.
11
- PATTERN_STEAM_ID_64 = /^765([0-9]{11,})$/
12
- # Not sure what its valid range is - exists with at least 7 and 8 numbers.
13
- PATTERN_ACCOUNT_ID = /^[0-9]+$/
14
-
15
- PATTERN_COMMUNITY_URL = /^https?:\/\/steamcommunity\.com\/profiles\/(765[0-9]+)\/?$/
16
- PATTERN_COMMUNITY_URL_STEAM_ID_3 = /^https?:\/\/steamcommunity\.com\/profiles\/\[U:([0-9]{1,2}):([0-9]+)\]$/i
17
- PATTERN_CUSTOM_URL = /^https?:\/\/steamcommunity\.com\/id\/([^\/]+)\/?$/
18
-
19
- # Resolve custom URL into account ID suitable for API calls.
20
- # @param s [String] custom URL:
21
- # - http://steamcommunity.com/id/some-custom-url
22
- # - some-custom-url
23
- # @return [Fixnum] Account ID
24
- # @raise [ArgumentError] If the supplied string was not a valid community URL.
25
- def self.from_vanity_url(url, steam_api_key:)
26
- WebApi.api_key = steam_api_key
27
-
28
- PATTERN_CUSTOM_URL.match(url) do |m|
29
- url = m[1]
30
- end
31
-
32
- steam_id = SteamId.resolve_vanity_url(url)
33
- if steam_id.nil?
34
- raise ArgumentError, "#{ url } was not a valid custom URL."
35
- else
36
- from_steam_id(steam_id)
37
- end
4
+ # Class representing a single Steam ID - that is, one logical account
5
+ class SteamID
6
+ # Offset between ID 64 and account ID.
7
+ ID_64_OFFSET = 76561197960265728
8
+ # Base URL for Steam community profiles based on ID64 / ID3.
9
+ PROFILE_BASE_URL = "https://steamcommunity.com/profiles"
10
+
11
+ attr_reader :account_id
12
+
13
+ # Create new SteamID
14
+ # @param account_id [Integer] Numeric account ID.
15
+ def initialize(account_id)
16
+ @account_id = account_id
38
17
  end
39
18
 
40
- # Convert community URL into account ID suitable for API calls.
41
- # @param s [String] community URL: http://steamcommunity.com/profiles/76561198008487038
42
- # @return [Fixnum] Account ID
43
- # @raise [ArgumentError] If the supplied string was not a valid community URL.
44
- def self.from_community_url(url)
45
- PATTERN_COMMUNITY_URL.match(url) do |m|
46
- return self.from_steam_id(m[1])
47
- end
48
-
49
- PATTERN_COMMUNITY_URL_STEAM_ID_3.match(url) do |m|
50
- return self.from_steam_id(m[2])
51
- end
19
+ # @return [String] Classical Steam ID (eg STEAM_0:....)
20
+ def id
21
+ offset = @account_id / 2
22
+ index = @account_id - 2 * offset
52
23
 
53
- raise ArgumentError, "#{ url.inspect } is not a supported community URL."
24
+ "STEAM_0:#{ index }:#{ offset }"
54
25
  end
55
26
 
56
- # Convert Steam ID into account ID suitable for API calls.
57
- # @param s [String] Steam ID, either of:
58
- # - Steam ID: STEAM_0:0:24110655
59
- # - Steam ID 3: U:1:48221310
60
- # - Steam ID 64: 76561198008487038
61
- # - Account ID: 48221310
62
- # @return [Fixnum] Account ID
63
- # @raise [ArgumentError] If the supplied string could not be converted to
64
- # an account ID.
65
- def self.from_steam_id(id)
66
- # In case we get a fixnum.
67
- id = id.to_s
68
-
69
- # https://developer.valvesoftware.com/wiki/SteamID#Format
70
- PATTERN_STEAM_ID.match(id) do |m|
71
- return m[1].to_i + m[2].to_i * 2
72
- end
73
-
74
- PATTERN_STEAM_ID_3.match(id) do |m|
75
- return m[2].to_i
76
- end
77
-
78
- PATTERN_STEAM_ID_64.match(id) do |m|
79
- return m[1].to_i - STEAM_ID_64_OFFSET
80
- end
81
-
82
- # Matching this one last, as not to catch an ID 64 on accident.
83
- PATTERN_ACCOUNT_ID.match(id) do |m|
84
- return id.to_i
85
- end
86
-
87
- # If we get until here, we did not match any regex.
88
- raise ArgumentError, "#{ id.inspect } is not a supported SteamID."
27
+ # @return [Integer] Steam ID 64 / Steam profile ID (eg 765...)
28
+ def id_64
29
+ @account_id + ID_64_OFFSET
89
30
  end
90
31
 
91
- def self.from_string(s, steam_api_key: nil)
92
- account_id = nil
93
-
94
- # Todo: Refactor
95
- begin
96
- # Checking for Steam ID first. Most restrictive check, and also does
97
- # not require a call to Steam Web API.
98
- account_id = from_steam_id(s)
99
- rescue ArgumentError
100
- begin
101
- # Community URL afterwards, does not require an API call either.
102
- account_id = from_community_url(s)
103
- rescue ArgumentError
104
- begin
105
- # Trying to resolve as custom/vanity URL now.
106
- account_id = from_vanity_url(s, steam_api_key: steam_api_key)
107
- rescue ArgumentError
108
- end
109
- end
110
- end
32
+ # @return [String] Steam ID 3 (eg [U:1:...])
33
+ def id_3
34
+ "[U:1:#{ @account_id }]"
35
+ end
111
36
 
112
- if account_id.nil?
113
- raise ArgumentError, "Could not convert #{ s } to account id."
114
- else
115
- account_id
116
- end
37
+ # @return [String] URL to player's profile on Steam community.
38
+ def profile_url
39
+ "#{ PROFILE_BASE_URL }/#{ id_64 }"
117
40
  end
118
41
  end
119
42
  end
data/lib/steam_id.rb CHANGED
@@ -1,7 +1,20 @@
1
1
  # coding: utf-8
2
2
 
3
3
  require 'steam-id/steam_id'
4
+ require 'steam-id/parser'
4
5
 
6
+ # Handle Steam IDs.
5
7
  module SteamID
6
- # Your code goes here...
8
+ # Create SteamID object based on Steam ID.
9
+ # @param s [String] Steam ID which to use.
10
+ # @param api_key (see Parser#initialize)
11
+ # @return [SteamID]
12
+ # @see Parser#from_string Supported formats of Steam ID.
13
+ # @example
14
+ # id1 = SteamID.from_string('STEAM_0:0:24110655')
15
+ # id2 = SteamID.from_string('gabenewell', api_key: '...')
16
+ # puts "Accounts are equal: #{ id1.account_id == id2.account_id }"
17
+ def from_string(s, api_key: nil)
18
+
19
+ end
7
20
  end
data/steam-id2.gemspec CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "steam-id2"
7
- spec.version = '0.3.0'
7
+ spec.version = '0.4.0'
8
8
  spec.authors = ["Michael Senn"]
9
9
  spec.email = ["michael@morrolan.ch"]
10
10
 
@@ -21,8 +21,9 @@ Gem::Specification.new do |spec|
21
21
  spec.add_runtime_dependency 'steam-condenser', '~> 1.3'
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.12"
24
- spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rake", "~> 12"
25
25
  spec.add_development_dependency "rspec", "~> 3.0"
26
- spec.add_development_dependency "yard"
26
+ spec.add_development_dependency "rspec_junit_formatter", "~> 0.3"
27
+ spec.add_development_dependency "yard", "~> 0.9"
27
28
  end
28
29
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: steam-id2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Senn
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-05-30 00:00:00.000000000 Z
11
+ date: 2017-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: steam-condenser
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '10.0'
47
+ version: '12'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '10.0'
54
+ version: '12'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -66,20 +66,34 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec_junit_formatter
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.3'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: yard
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
- - - ">="
87
+ - - "~>"
74
88
  - !ruby/object:Gem::Version
75
- version: '0'
89
+ version: '0.9'
76
90
  type: :development
77
91
  prerelease: false
78
92
  version_requirements: !ruby/object:Gem::Requirement
79
93
  requirements:
80
- - - ">="
94
+ - - "~>"
81
95
  - !ruby/object:Gem::Version
82
- version: '0'
96
+ version: '0.9'
83
97
  description:
84
98
  email:
85
99
  - michael@morrolan.ch
@@ -87,7 +101,9 @@ executables: []
87
101
  extensions: []
88
102
  extra_rdoc_files: []
89
103
  files:
104
+ - ".circleci/config.yml"
90
105
  - ".gitignore"
106
+ - ".rspec"
91
107
  - CHANGELOG.md
92
108
  - Gemfile
93
109
  - LICENSE.txt
@@ -95,6 +111,7 @@ files:
95
111
  - Rakefile
96
112
  - bin/console
97
113
  - bin/setup
114
+ - lib/steam-id/parser.rb
98
115
  - lib/steam-id/steam_id.rb
99
116
  - lib/steam_id.rb
100
117
  - steam-id2.gemspec