gamertag 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'json'
4
+ gem 'hashie'
5
+ gem 'nokogiri'
6
+
7
+ # Specify your gem's dependencies in gamertag.gemspec
8
+ gemspec
data/README.mdown ADDED
@@ -0,0 +1,75 @@
1
+ # Gamertag
2
+
3
+ An interface for accessing your xbox live profile and game data.
4
+
5
+ ## Install
6
+ Will add when finished.
7
+
8
+ ## Usage
9
+ This gem fetches data from two independent sources, which has several implications such as one of the sources not being available, or an increased overhead when you only want to access some of the data. To this end the interface will allow you to access all data, just the profile data or just the played games data.
10
+
11
+
12
+ Example: Obtaining your profile and played games. Methods are those available to the simple profile and played games.
13
+
14
+ profile = Gamertag::Profile.new('Belial1984')
15
+ profile.gamertag
16
+ profile.recent_games {|game| game.title }
17
+ profile.played_games {|game| game.title }
18
+
19
+
20
+ Example: Obtaining your profile and recent games only
21
+
22
+ profile = Gamertag::SimpleProfile.new('Belial1984')
23
+
24
+ profile.gamertag
25
+ profile.profile_link
26
+ profile.account_status
27
+ profile.gender
28
+ profile.is_cheater
29
+ profile.online
30
+ profile.online_status
31
+ profile.reputation
32
+ profile.gamerscore
33
+ profile.location
34
+ profile.motto
35
+ profile.name
36
+ profile.bio
37
+ profile.avatars.small_gamerpic
38
+ profile.avatars.large_gamerpic
39
+ profile.avatars.body_gamerpic
40
+ profile.recent_games.each do |game|
41
+ game.title
42
+ game.tid # Probably some arbitrary title id
43
+ game.marketplace_url
44
+ game.compare_url
45
+ game.image
46
+ game.last_played
47
+ game.earned_gamerscore
48
+ game.available_gamerscore
49
+ game.earned_achievements
50
+ game.avilable_achievements
51
+ game.percentage_complete
52
+ end
53
+
54
+ Example: Obtaining your played games only
55
+
56
+ played_games = Gamertag::PlayedGames.new('Belial1984')
57
+ played_games.each do |game|
58
+ game.image
59
+ game.last_played
60
+ game.earned_gamerscore
61
+ game.available_gamerscore
62
+ game.earned_achievements
63
+ game.available_achievements
64
+ game.average_gamerscore
65
+ game.relative_gamerscore
66
+ end
67
+
68
+
69
+ ## Note on Patches/Pull Requests
70
+
71
+ * Not finished, so I wouldn't advise it
72
+
73
+ ## Copyright
74
+
75
+ Copyright (c) 2011 Baris Balic.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/gamertag.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("lib", __FILE__)
3
+ require "gamertag/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "gamertag"
7
+ s.version = Gamertag::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Baris Balic"]
10
+ s.email = ["baris@webloch.co.uk"]
11
+ s.homepage = ""
12
+ s.summary = %q{A gem for retrieving your xbox live gamertag info}
13
+ s.description = %q{A gem for retrieving your xbox live gamertag info}
14
+
15
+ s.rubyforge_project = "gamertag"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,44 @@
1
+ module Gamertag
2
+ class PlayedGames
3
+
4
+ def initialize(gamertag)
5
+ @played_games = fetch(gamertag)
6
+ end
7
+
8
+ def method_missing(method_name, args = nil)
9
+ [:first, :last, :each, :count].each {|method| return @played_games.send(method) if method_name == method }
10
+ @played_games[method_name.to_s]
11
+ end
12
+
13
+ private
14
+ def fetch(gamertag)
15
+ uri = URI.parse("http://www.xboxgamertag.com/search/#{gamertag}/")
16
+ response = Net::HTTP.get_response(uri)
17
+ document = Nokogiri::HTML(response.body)
18
+ parse_rows(document)
19
+ end
20
+
21
+ def parse_rows(document)
22
+ document.css("[id='recentGamesTable']").css('tr').map do |table_row|
23
+ cells = table_row.css('td')
24
+
25
+ naming = cells[1].css("p")
26
+ gamerscore = cells[2].css("div[class='percentage-container']").first.text.strip!.split('/')
27
+ achievements = (cells[2].css("div[class='percentage-container']").last.text).strip!.split('/')
28
+ average_gamerscore = cells[3].css('span').text.strip!
29
+ relative_gamerscore = (cells[3].css('span').attribute('title').text.include?('above average') ? :AboveAverage : :BelowAverage)
30
+
31
+ Hashie::Mash.new({'image' => cells[0].css('img').attribute('src').text,
32
+ 'title' => naming.first.text,
33
+ 'last_played' => naming.last.text,
34
+ 'earned_gamerscore' => gamerscore.first,
35
+ 'available_gamerscore' => gamerscore.last,
36
+ 'earned_achievements' => achievements.first,
37
+ 'available_achievements' => achievements.last,
38
+ 'average_gamerscore' => average_gamerscore,
39
+ 'relative_gamerscore' => relative_gamerscore
40
+ })
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,10 @@
1
+ module Gamertag
2
+ class Profile < SimpleProfile
3
+ attr_reader :played_games
4
+
5
+ def initialize(gamertag)
6
+ super(gamertag)
7
+ @played_games = PlayedGames.new(gamertag)
8
+ end
9
+ end
10
+ end
File without changes
@@ -0,0 +1,30 @@
1
+ module Gamertag
2
+ class SimpleProfile
3
+ def initialize(gamertag)
4
+ @profile = fetch(gamertag)
5
+ end
6
+
7
+ def method_missing(method_name, args = nil)
8
+ @profile[method_name.to_s]
9
+ end
10
+
11
+ def recent_games
12
+ @profile['recent_games'].map do |k, v|
13
+ g = v.merge({'last_played' => Time.at(v['last_played'].to_i)})
14
+ Hashie::Mash.new(g)
15
+ end
16
+ end
17
+
18
+ def avatars
19
+ Hashie::Mash.new(@profile['avatars'])
20
+ end
21
+
22
+ private
23
+ def fetch(gamertag)
24
+ uri = URI.parse("http://api.xboxleaders.com/newapi.php?gamertag=#{gamertag}&version=2.0&format=json")
25
+ response = Net::HTTP.get_response(uri)
26
+ hash = JSON.parse(response.body)
27
+ hash['user']
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module Gamertag
2
+ VERSION = '0.0.1'
3
+ end
data/lib/gamertag.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'net/http'
2
+ require 'date'
3
+ require 'JSON'
4
+ require 'hashie'
5
+ require 'nokogiri'
6
+
7
+ require 'gamertag/simple_profile'
8
+ require 'gamertag/played_games'
9
+ require 'gamertag/profile'
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe Gamertag::PlayedGames do
4
+
5
+ before(:each) do
6
+ end
7
+
8
+ it "should return played games" do
9
+ played_games = Gamertag::PlayedGames.new('Belial1984')
10
+ played_games.first.title.should eql("The Orange Box")
11
+ played_games.count.should == 85
12
+ played_games.each {|game| game.title.nil?.should be_false}
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe Gamertag::Profile do
4
+ it "should return composite details" do
5
+ profile = Gamertag::Profile.new('Belial1984')
6
+
7
+ profile.gamertag.should eql('belial1984')
8
+ profile.online_status.include?('SUPER STREETFIGHTER IV').should eql(true)
9
+ profile.recent_games.count.should eql(5)
10
+ profile.played_games.count.should eql(84)
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe Gamertag::SimpleProfile, "#fetch" do
4
+ it "should return profile data" do
5
+ profile = Gamertag::SimpleProfile.new('Belial1984')
6
+ profile.name.should =~ /baris balic/i
7
+ profile.gamertag.should =~ /Belial1984/i
8
+ profile.avatars.count.should eql(3)
9
+ profile.avatars.small_gamerpic.should == 'http://avatar.xboxlive.com/avatar/belial1984/avatarpic-s.png'
10
+ end
11
+
12
+ it "should return the recent games data" do
13
+ profile = Gamertag::SimpleProfile.new('Belial1984')
14
+ profile.recent_games.count.should eql(5)
15
+ profile.recent_games.first.title.should eql('SUPER STREETFIGHTER IV')
16
+ profile.recent_games.first.available_gamerscore.should eql(1000)
17
+ profile.recent_games.first.last_played.class.should == Time
18
+ end
19
+ end
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'rspec'
3
+
4
+ require File.join(File.dirname(__FILE__), %w{ .. lib gamertag})
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gamertag
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Baris Balic
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-25 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: A gem for retrieving your xbox live gamertag info
18
+ email:
19
+ - baris@webloch.co.uk
20
+ executables: []
21
+
22
+ extensions: []
23
+
24
+ extra_rdoc_files: []
25
+
26
+ files:
27
+ - Gemfile
28
+ - README.mdown
29
+ - Rakefile
30
+ - gamertag.gemspec
31
+ - lib/gamertag.rb
32
+ - lib/gamertag/played_games.rb
33
+ - lib/gamertag/profile.rb
34
+ - lib/gamertag/recent_games.rb
35
+ - lib/gamertag/simple_profile.rb
36
+ - lib/gamertag/version.rb
37
+ - spec/played_games_spec.rb
38
+ - spec/profile_spec.rb
39
+ - spec/simple_profile_spec.rb
40
+ - spec/spec_helper.rb
41
+ has_rdoc: true
42
+ homepage: ""
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ requirements: []
63
+
64
+ rubyforge_project: gamertag
65
+ rubygems_version: 1.6.2
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: A gem for retrieving your xbox live gamertag info
69
+ test_files:
70
+ - spec/played_games_spec.rb
71
+ - spec/profile_spec.rb
72
+ - spec/simple_profile_spec.rb
73
+ - spec/spec_helper.rb