Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First Commit

  • Loading branch information...
commit 5ac440ede06031ea58ca68e33b8351d7c0237e55 0 parents
Rodrigo Navarro authored
1  .gitignore
@@ -0,0 +1 @@
+nbproject
0  README
No changes.
9 lib/wow.rb
@@ -0,0 +1,9 @@
+require "hpricot"
+require "open-uri"
+
+require "#{File.dirname(__FILE__)}/wow/core_extensions"
+require "#{File.dirname(__FILE__)}/wow/base"
+require "#{File.dirname(__FILE__)}/wow/character"
+
+require "#{File.dirname(__FILE__)}/wow/parsers/base"
+require "#{File.dirname(__FILE__)}/wow/parsers/xml_parser"
14 lib/wow/base.rb
@@ -0,0 +1,14 @@
+module Wow
+ class Base
+ # To-do
+ @@default_zone = "us"
+
+ def self.default_realm=(realm)
+ @@default_realm = realm
+ end
+
+ def self.default_zone=(zone)
+ @@default_zone = zone
+ end
+ end
+end
23 lib/wow/character.rb
@@ -0,0 +1,23 @@
+module Wow
+ class Character < Base
+ FILE_NAME = "character-sheet.xml"
+
+ attr_accessor :name, :level
+
+ def initialize(xml)
+ self.name = (xml/:character).first["name"]
+ self.level = (xml/:character).first["level"].to_i
+ end
+
+ def self.find(params = {})
+ params[:r] = params.delete(:realm) || @@default_realm
+ params[:cn] = params.delete(:name)
+
+ self.new Wow::Parsers::XmlParser.parse("http://#{@@default_zone}.wowarmory.com/#{FILE_NAME}?#{params.to_query}")
+ end
+
+ def to_s
+ name
+ end
+ end
+end
67 lib/wow/core_extensions.rb
@@ -0,0 +1,67 @@
+module Wow
+ module IntegerExtensions
+ def gold
+ self * 10000
+ end
+
+ def silver
+ self * 100
+ end
+
+ def copper
+ self
+ end
+ end
+
+ # From: http://stackoverflow.com/questions/798710/how-to-turn-a-ruby-hash-into-http-params
+ # Thanks Juliem
+ module HashExtensions
+ def to_query
+ params = ''
+ stack = []
+
+ each do |k, v|
+ if v.is_a?(Hash)
+ stack << [k,v]
+ elsif v.is_a?(Array)
+ stack << [k,Hash.from_array(v)]
+ else
+ params << "#{k}=#{v}&"
+ end
+ end
+
+ stack.each do |parent, hash|
+ hash.each do |k, v|
+ if v.is_a?(Hash)
+ stack << ["#{parent}[#{k}]", v]
+ else
+ params << "#{parent}[#{k}]=#{v}&"
+ end
+ end
+ end
+
+ params.chop!
+ params
+ end
+
+ def self.from_array(array = [])
+ h = Hash.new
+ array.size.times do |t|
+ h[t] = array[t]
+ end
+ h
+ end
+ end
+end
+
+class Fixnum
+ include Wow::IntegerExtensions
+end
+
+class Bignum
+ include Wow::IntegerExtensions
+end
+
+class Hash
+ include Wow::HashExtensions
+end
7 lib/wow/parsers/base.rb
@@ -0,0 +1,7 @@
+module Wow
+ module Parsers
+ class Base
+ USER_AGENT = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-GB; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4"
+ end
+ end
+end
10 lib/wow/parsers/xml_parser.rb
@@ -0,0 +1,10 @@
+module Wow
+ module Parsers
+ class XmlParser < Base
+ def self.parse(url)
+ puts "Opening #{url}"
+ Hpricot.XML open(url, "User-Agent" => USER_AGENT)
+ end
+ end
+ end
+end
33 wow.gemspec
@@ -0,0 +1,33 @@
+Gem::Specification.new do |s|
+ s.name = %q{wow}
+ s.version = "0.1.0"
+ s.rubygems_version = %q{1.3.5}
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+
+ s.date = %q{2009-02-07}
+ s.summary = %q{A world of warcraft armory API.}
+ s.description = %q{A world of warcraft armory API.}
+
+ s.files = ["wow.gemspec","lib/wow.rb"]
+ s.require_paths = ["lib"]
+ s.rubyforge_project = %q{wow}
+
+ s.authors = ["Rodrigo Navarro"]
+ s.email = ["rnavarro1@gmail.com"]
+ s.homepage = "http://github.com/reu/wow"
+
+ s.has_rdoc = false
+
+ if s.respond_to? :specification_version
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0')
+ s.add_runtime_dependency %q<hpricot>, [">= 0.8.2"]
+ else
+ s.add_dependency %q<hpricot>, [">= 0.8.2"]
+ end
+ else
+ s.add_dependency %q<hpricot>, [">= 0.8.2"]
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.