gamer_stats 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +31 -0
- data/Rakefile +12 -0
- data/gamer_stats.gemspec +26 -0
- data/lib/bf3/bf3.rb +79 -0
- data/lib/error/gamer_stats_error.rb +4 -0
- data/lib/gamer_stats/version.rb +3 -0
- data/lib/gamer_stats.rb +4 -0
- data/lib/helper/deep_dup.rb +20 -0
- data/lib/helper/deep_merge.rb +23 -0
- data/lib/helper/hash_path_eval.rb +19 -0
- data/test/bf3_test.rb +43 -0
- data/test/hash_path_eval_test.rb +38 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YjI5ODgxYTI1YWE2ODUzOTUyOWZmZmEzZTE0OTZmZWQwZjg4ZTUxNg==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NjRhNmEwZmVkZDk2NDliODdhNzU4YWYzNzQ4MTQyMDE1OWVmNzRiYQ==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
Nzg3YWNiMTQ2NjMyYjRjNzVjZjY2OTllMzY1MjExZWFjNGJmODNmOWI5M2Vm
|
10
|
+
ZDE0MDI5OWM2YTc0YTA0NDU3MGMwMjY1ZmJkNTdmZWQyNmQ5Njg0OTcwOWZl
|
11
|
+
NmNkOTQyMzc2MzY2MjQ1MWZkOWJmZTllMDU1OTg3N2I2MWMyZTI=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NGZiMzhlNjMwNGNjYzVmYzgyMzg1YjQwMDFlZjU4Y2IyZmRiMjZiMjVjODZj
|
14
|
+
MzFmMDU1Mzg5NWE4YzMyYzA5OWNlNTg5YTAzZjBjZmUxOWFhNWJiOTIxNTZl
|
15
|
+
OTMwYTY4YWJkNGYwMDE0YjhlYTZiMTRhMzE2YzE1OTE1MDA1NTU=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 SeriousM - Bernhard Millauer
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/SeriousM/gamer_stats.png?branch=master)](https://travis-ci.org/SeriousM/gamer_stats)
|
2
|
+
|
3
|
+
# GamerStats
|
4
|
+
|
5
|
+
Provides a way to gather game stats from the http://p-stats.com network
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'gamer_stats'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install gamer_stats
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/gamer_stats.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'gamer_stats/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "gamer_stats"
|
8
|
+
spec.version = GamerStats::VERSION
|
9
|
+
spec.authors = ["SeriousM - Bernhard Millauer"]
|
10
|
+
spec.email = ["bernhard.millauer@gmail.com"]
|
11
|
+
spec.description = %q{Provides a way to gather gamer stats from the http://p-stats.com network}
|
12
|
+
spec.summary = %q{Provides a way to gather gamer stats from the http://p-stats.com network}
|
13
|
+
spec.homepage = "https://github.com/SeriousM/gamer_stats"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
|
24
|
+
spec.add_dependency "httparty"
|
25
|
+
spec.add_dependency "json"
|
26
|
+
end
|
data/lib/bf3/bf3.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
require 'json'
|
3
|
+
require 'gamer_stats'
|
4
|
+
|
5
|
+
module GamerStats
|
6
|
+
module Bf3
|
7
|
+
class Player
|
8
|
+
|
9
|
+
def initialize(name, platform, player = [])
|
10
|
+
@name = name
|
11
|
+
@platform = platform
|
12
|
+
@player = player.deep_dup
|
13
|
+
end
|
14
|
+
|
15
|
+
def current
|
16
|
+
get_current
|
17
|
+
end
|
18
|
+
|
19
|
+
def stats!
|
20
|
+
raw_load
|
21
|
+
get_current 'stats'
|
22
|
+
end
|
23
|
+
|
24
|
+
def stats
|
25
|
+
raw_load unless loaded? 'stats'
|
26
|
+
get_current 'stats'
|
27
|
+
end
|
28
|
+
|
29
|
+
def load!(opt='clear,global')
|
30
|
+
raw_load(opt)
|
31
|
+
get_current
|
32
|
+
end
|
33
|
+
|
34
|
+
def load(opt='clear,global')
|
35
|
+
raw_load(opt) unless loaded?
|
36
|
+
get_current
|
37
|
+
end
|
38
|
+
|
39
|
+
def kdr
|
40
|
+
return get_current('global/kills').to_f / get_current('global/kills')
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def loaded?(path)
|
46
|
+
val = @player.path(path)
|
47
|
+
false if val.nil? or val.empty?
|
48
|
+
end
|
49
|
+
|
50
|
+
def get_current(path)
|
51
|
+
@player.path(path).deep_dup
|
52
|
+
end
|
53
|
+
|
54
|
+
def merge(player)
|
55
|
+
@player.deep_merge player
|
56
|
+
end
|
57
|
+
|
58
|
+
def raw_load(opt='clear,global')
|
59
|
+
body = {
|
60
|
+
player: @name,
|
61
|
+
output: 'json',
|
62
|
+
opt: opt
|
63
|
+
}
|
64
|
+
|
65
|
+
begin
|
66
|
+
response = HTTParty.post("http://api.bf3stats.com/#{@platform}/player/", :body => body, timeout: 5)
|
67
|
+
rescue => e
|
68
|
+
raise GamerStatsError, "Error on loading the player: #{e.message}"
|
69
|
+
end
|
70
|
+
|
71
|
+
if response.code == 200 && response['status'] == "data"
|
72
|
+
@player = merge JSON(response.body)
|
73
|
+
else
|
74
|
+
raise GamerStatsError, "Bf3: #{response['error']}"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
data/lib/gamer_stats.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# this is borrowed from rails
|
2
|
+
|
3
|
+
class Hash
|
4
|
+
# Returns a deep copy of hash.
|
5
|
+
#
|
6
|
+
# hash = { :a => { :b => 'b' } }
|
7
|
+
# dup = hash.deep_dup
|
8
|
+
# dup[:a][:c] = 'c'
|
9
|
+
#
|
10
|
+
# hash[:a][:c] #=> nil
|
11
|
+
# dup[:a][:c] #=> "c"
|
12
|
+
def deep_dup
|
13
|
+
duplicate = self.dup
|
14
|
+
duplicate.each_pair do |k,v|
|
15
|
+
tv = duplicate[k]
|
16
|
+
duplicate[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_dup : v
|
17
|
+
end
|
18
|
+
duplicate
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# this is borrowed from rails
|
2
|
+
|
3
|
+
class Hash
|
4
|
+
# Returns a new hash with +self+ and +other_hash+ merged recursively.
|
5
|
+
#
|
6
|
+
# h1 = {:x => {:y => [4,5,6]}, :z => [7,8,9]}
|
7
|
+
# h2 = {:x => {:y => [7,8,9]}, :z => "xyz"}
|
8
|
+
#
|
9
|
+
# h1.deep_merge(h2) #=> { :x => {:y => [7, 8, 9]}, :z => "xyz" }
|
10
|
+
# h2.deep_merge(h1) #=> { :x => {:y => [4, 5, 6]}, :z => [7, 8, 9] }
|
11
|
+
def deep_merge(other_hash)
|
12
|
+
dup.deep_merge!(other_hash)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Same as +deep_merge+, but modifies +self+.
|
16
|
+
def deep_merge!(other_hash)
|
17
|
+
other_hash.each_pair do |k,v|
|
18
|
+
tv = self[k]
|
19
|
+
self[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_merge(v) : v
|
20
|
+
end
|
21
|
+
self
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class Hash
|
2
|
+
# Returns the element at the end of the defined path
|
3
|
+
#
|
4
|
+
# the path should be in format of "a/:b/c", symbols treated as symbols
|
5
|
+
# then a string interpreation expression is build like
|
6
|
+
# "self['a'][:b][c]"
|
7
|
+
#
|
8
|
+
# known issue: path segments with slashes are not supported
|
9
|
+
def path(hashPath)
|
10
|
+
return self if hashPath.nil? or hashPath.empty?
|
11
|
+
|
12
|
+
hashPath = hashPath.split('/').map{ |v| v.start_with?(":") ? "[#{v}]" : "['#{v}']" }.join
|
13
|
+
hashPath = "self#{hashPath}"
|
14
|
+
|
15
|
+
eval hashPath
|
16
|
+
rescue
|
17
|
+
nil
|
18
|
+
end
|
19
|
+
end
|
data/test/bf3_test.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'gamer_stats'
|
2
|
+
require 'minitest/spec'
|
3
|
+
require 'minitest/autorun'
|
4
|
+
|
5
|
+
include GamerStats
|
6
|
+
|
7
|
+
describe Bf3::Player do
|
8
|
+
let(:player) { Bf3::Player.new 'SeriousM', 'pc' }
|
9
|
+
let(:non_player) { Bf3::Player.new 'xx', 'xx' }
|
10
|
+
|
11
|
+
describe 'getting a player' do
|
12
|
+
it 'should return the player requested' do
|
13
|
+
player = @player.load
|
14
|
+
player['country'].wont_be_nil
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe 'getting a player\'s stats' do
|
19
|
+
it 'should return the player requested' do
|
20
|
+
stats = player.stats
|
21
|
+
stats.wont_be_nil
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should have some global stats' do
|
25
|
+
player.load('clear,global')
|
26
|
+
player.current['global'].wont_be_nil
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe 'some vanity stats' do
|
31
|
+
it 'should return a floating point kill to death ratio' do
|
32
|
+
kdr = player.kdr
|
33
|
+
kdr.wont_be_nil
|
34
|
+
kdr.must_be_instance_of(Float)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe 'will throw an error when the player does not exist' do
|
39
|
+
it 'on load' do
|
40
|
+
->{@non_player.load}.must_raise GamerStatsError
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require_relative '../lib/helper/hash_path_eval'
|
2
|
+
require 'minitest/spec'
|
3
|
+
require 'minitest/autorun'
|
4
|
+
|
5
|
+
describe Hash do
|
6
|
+
let(:h_empty) { {} }
|
7
|
+
let(:h_string) { { 'a' => { 'b' => { 'c' => 1 } } } }
|
8
|
+
let(:h_symbol) { { a: { b: { c: 1 } } } }
|
9
|
+
let(:h_mixed) { { a: { 'b' => { c: 1 } } } }
|
10
|
+
|
11
|
+
it "returns hash without a path specified" do
|
12
|
+
h_empty.path(nil).wont_be_nil
|
13
|
+
end
|
14
|
+
|
15
|
+
it "returns hash on nil path" do
|
16
|
+
h_empty.path(nil).wont_be_nil
|
17
|
+
end
|
18
|
+
|
19
|
+
it "returns hash on empty path" do
|
20
|
+
h_empty.path('').wont_be_nil
|
21
|
+
end
|
22
|
+
|
23
|
+
it "works with pure string hash" do
|
24
|
+
h_string.path('a/b/c').must_equal 1
|
25
|
+
end
|
26
|
+
|
27
|
+
it "works with pure symbol hash" do
|
28
|
+
h_symbol.path(':a/:b/:c').must_equal 1
|
29
|
+
end
|
30
|
+
|
31
|
+
it "works with a mixed hash" do
|
32
|
+
h_mixed.path(':a/b/:c').must_equal 1
|
33
|
+
end
|
34
|
+
|
35
|
+
it "return nil on non existing path" do
|
36
|
+
h_mixed.path(':a/x/:c').must_be_nil
|
37
|
+
end
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gamer_stats
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- SeriousM - Bernhard Millauer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-05-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: httparty
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: json
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Provides a way to gather gamer stats from the http://p-stats.com network
|
70
|
+
email:
|
71
|
+
- bernhard.millauer@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- gamer_stats.gemspec
|
82
|
+
- lib/bf3/bf3.rb
|
83
|
+
- lib/error/gamer_stats_error.rb
|
84
|
+
- lib/gamer_stats.rb
|
85
|
+
- lib/gamer_stats/version.rb
|
86
|
+
- lib/helper/deep_dup.rb
|
87
|
+
- lib/helper/deep_merge.rb
|
88
|
+
- lib/helper/hash_path_eval.rb
|
89
|
+
- test/bf3_test.rb
|
90
|
+
- test/hash_path_eval_test.rb
|
91
|
+
homepage: https://github.com/SeriousM/gamer_stats
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata: {}
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ! '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ! '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 2.0.3
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: Provides a way to gather gamer stats from the http://p-stats.com network
|
115
|
+
test_files:
|
116
|
+
- test/bf3_test.rb
|
117
|
+
- test/hash_path_eval_test.rb
|
118
|
+
has_rdoc:
|