Permalink
Browse files

Handle characters with rich name

  • Loading branch information...
fracek committed Jan 3, 2013
2 parents eb6d470 + 8502bfb commit 9a9837d8fed0c1dedc1b68ddd126d42289cc54be
View
1 .rspec
@@ -1 +1,2 @@
--color
+--order random
View
@@ -1,3 +1,3 @@
-source "http://rubygems.org"
+source 'https://rubygems.org'
gemspec
View
@@ -1,5 +1,19 @@
-require 'rspec/core/rake_task'
+require 'bundler'
+Bundler::GemHelper.install_tasks
-desc "Run RSpec tests"
+require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
-task :default => :spec
+
+task test: :spec
+task default: :spec
+
+namespace :doc do
+ require 'yard'
+ YARD::Rake::YardocTask.new do |t|
+ t.files = ['README.md', 'LICENSE.txt', 'lib/**/*.rb']
+ t.options = [
+ '--output-dir', 'doc/yard',
+ '--markup', 'markdown'
+ ]
+ end
+end
View
@@ -2,14 +2,25 @@ require File.expand_path('../lib/armory_api/version', __FILE__)
Gem::Specification.new do |s|
s.name = 'armory_api'
- s.version = ArmoryApi::Version
- s.summary = 'Ruby wrapper for the World of Warcraft API'
- s.description = 'Ruby wrapper for the official World of Warcraft REST APIs'
+ s.version = ArmoryApi::VERSION
+ s.description = 'Wrapper for the World of Warcraft API'
+ s.summary = s.description
s.authors = ['Francesco Ceccon']
- s.files = ['lib/armory_api.rb']
+ s.email = ['francesco@ceccon.me']
+ s.homepage = 'https://github.com/fracek/armory_api'
s.platform = Gem::Platform::RUBY
+ s.require_path = ['lib']
+ s.files = `git ls-files`.split("\n")
+ s.add_dependency 'faraday', '~> 0.8'
+ s.add_dependency 'faraday_middleware', '~> 0.8'
+ s.add_dependency 'hashie', '~> 1.2'
+ s.add_dependency 'multi_json', '~> 1.3'
+
+ s.add_development_dependency 'json', '~> 1.7'
s.add_development_dependency 'rake'
- s.add_development_dependency 'rspec', '~> 2.11.0'
- s.add_development_dependency 'faraday'
+ s.add_development_dependency 'rspec'
+ s.add_development_dependency 'webmock'
+ s.add_development_dependency 'simplecov'
+ s.add_development_dependency 'yard'
end
View
@@ -1,15 +1,28 @@
+require 'armory_api/configuration'
require 'armory_api/client'
-require 'armory_api/configurable'
+require 'armory_api/error'
module ArmoryApi
+ extend Configuration
class << self
- include ArmoryApi::Configurable
- def client
- @client = ArmoryApi::Client.new unless defined?(@client)
- @client
+ # Shortcut for ArmoryApi::Client.new
+ #
+ # @return [ArmoryApi::Client]
+ def new(options={})
+ ArmoryApi::Client.new(options)
+ end
+
+ # Delegate to ArmoryApi::Client.new
+ def method_missing(method, *args, &block)
+ return super unless new.respond_to?(method)
+ new.send(method, *args, &block)
+ end
+
+ def respond_to?(method, include_private=false)
+ new.respond_to?(method, include_private) || super(method, include_private)
end
end
end
-ArmoryApi.setup
+ArmoryApi.reset
View
@@ -1,40 +1,52 @@
-require 'armory_api/api'
-require 'armory_api/configurable'
+require 'faraday'
require 'uri'
+require 'armory_api/client/achievement'
+require 'armory_api/client/character'
+require 'armory_api/client/item'
+
module ArmoryApi
class Client
- include ArmoryApi::API
- include ArmoryApi::Configurable
+ attr_accessor(*Configuration::VALID_OPTIONS_KEYS)
# Initializes a new Client object
#
# @param [Hash] options
# @return [ArmoryApi::Client]
def initialize(options={})
- ArmoryApi::Configurable.keys.each do |k|
- instance_variable_set(:"@#{k}",
- options[k] || ArmoryApi.instance_variable_get(:"@#{k}"))
+ options = ArmoryApi.options.merge(options)
+ Configuration::VALID_OPTIONS_KEYS.each do |key|
+ send("#{key}=", options[key])
end
end
- # Perform an HTTP GET request
+ # Performs an HTTP GET request
def get(path, params={}, options={})
request(:get, path, params, options)
end
+
+ include ArmoryApi::Client::Achievement
+ include ArmoryApi::Client::Character
+ include ArmoryApi::Client::Item
private
+ def endpoint
+ "http://#{@region}.battle.net"
+ end
+ # TODO: don't make a new connection on each request
def connection
- @connection ||= Faraday.new(endpoint, @connection_options.merge(builder: @middleware))
+ @connection = Faraday.new(endpoint,
+ @connection_options.merge(builder: @middleware))
end
def request(method, path, params={}, options={})
uri = options[:endpoint] || endpoint
uri = URI(uri) unless uri.respond_to?(:host)
uri += path
request_headers = {}
+ params ||= {}
connection.url_prefix = options[:endpoint] || endpoint
- response = connection.run_request(method.to_sym, path, nil, request_headers) do |req|
+ res = connection.run_request(method.to_sym, path, nil, request_headers) do |req|
unless params.empty?
case req.method
when :post, :put
@@ -43,9 +55,8 @@ def request(method, path, params={}, options={})
req.params.update(params)
end
end
- yield req if block_given?
- end.env
- response
+ end
+ res.body
end
end
end
@@ -0,0 +1,14 @@
+module ArmoryApi
+ class Client
+ module Achievement
+
+ # Retrieves the achievement `id` from the Armory
+ #
+ # @param [Integer] id the achievement id
+ # @return [Hash] the achievement
+ def achievement(id)
+ get("/api/wow/achievement/#{id}")
+ end
+ end
+ end
+end
@@ -0,0 +1,27 @@
+require 'uri'
+
+module ArmoryApi
+ class Client
+ module Character
+
+ # Retrieves a character from the Armory
+ #
+ # @param [String] name
+ # @param [String] realm
+ # @param [Array] fields optional fields, a list can be found on the
+ # [official page](http://blizzard.github.com/api-wow-docs/#character-profile-api)
+ # @return [Hash] the character hash
+ def character(name, realm=nil, fields=[])
+ if realm.class == Array
+ fields = realm
+ realm = nil
+ end
+ realm ||= @realm
+ raise ArmoryApi::RealmNotFound if realm.nil?
+ fields ||= []
+ params = { fields: fields.join(',') } unless fields.empty?
+ get("/api/wow/character/#{realm}/#{URI.escape(name)}", params)
+ end
+ end
+ end
+end
@@ -0,0 +1,28 @@
+module ArmoryApi
+ class Client
+ module Item
+
+ # Retrieve an item
+ #
+ # @param [Integer] id the item id
+ # @return [Hash] the item
+ def item(id)
+ get("/api/wow/item/#{id}")
+ end
+
+ # Retrieve an item set.
+ # If full is set to true ArmoryApi will retrieve each item of the set.
+ #
+ # @param [Integer] id the set id
+ # @param [Boolean] full also retrieve each item of the set
+ # @return [Hash] the item set
+ def item_set(id, full=false)
+ res = get("/api/wow/item/set/#{id}")
+ if full
+ res.items.map! {|i| item(i)}
+ end
+ res
+ end
+ end
+ end
+end
@@ -0,0 +1,63 @@
+require 'faraday'
+require 'faraday_middleware'
+require 'faraday/response/raise_armory_api_error'
+require 'armory_api/version'
+
+module ArmoryApi
+ module Configuration
+ VALID_OPTIONS_KEYS = [
+ :region,
+ :locale,
+ :realm,
+ :endpoint,
+ :user_agent,
+ :connection_options,
+ :middleware
+ ].freeze
+
+ DEFAULT_REGION = 'us'
+ DEFAULT_LOCALE = 'en_US'
+ DEFAULT_ENDPOINT = "http://#{DEFAULT_REGION}.battle.net"
+ DEFAULT_USER_AGENT = "ArmoryApi Ruby Gem #{ArmoryApi::VERSION}".freeze
+ DEFAULT_CONNECTION_OPTIONS = {
+ headers: {
+ accept: 'application/json',
+ user_agent: DEFAULT_USER_AGENT
+ },
+ raw: true
+ }
+ DEFAULT_MIDDLEWARE = Faraday::Builder.new do |builder|
+ builder.request :json
+
+ builder.use Faraday::Response::RaiseArmoryApiError
+ builder.use FaradayMiddleware::Mashify
+ builder.use FaradayMiddleware::ParseJson
+
+ builder.adapter Faraday.default_adapter
+ end
+
+ attr_accessor(*VALID_OPTIONS_KEYS)
+
+ def self.extended(base)
+ base.reset
+ end
+
+ def configure
+ yield self
+ end
+
+ def options
+ VALID_OPTIONS_KEYS.inject({}){|o,k| o.merge!(k => send(k)) }
+ end
+
+ def reset
+ self.region = DEFAULT_REGION
+ self.locale = DEFAULT_LOCALE
+ self.realm = nil
+ self.endpoint = DEFAULT_ENDPOINT
+ self.user_agent = DEFAULT_USER_AGENT
+ self.connection_options = DEFAULT_CONNECTION_OPTIONS
+ self.middleware = DEFAULT_MIDDLEWARE
+ end
+ end
+end
View
@@ -0,0 +1,13 @@
+module ArmoryApi
+ # Custom error class for rescuing from all Armory errors
+ class Error < StandardError; end
+
+ # Raised when the Armory returned a 404 status code
+ class NotFound < Error; end
+
+ # Raised when the Armory return a 500 status code
+ class InternalServerError < Error; end
+
+ # Raised when the realm is not set
+ class RealmNotFound < Error; end
+end
View
@@ -1,16 +1,3 @@
module ArmoryApi
- class Version
- MAJOR = 0
- MINOR = 1
- PATCH = 0
- PRE = nil
-
- class << self
-
- # @return [String] the version number
- def to_s
- [MAJOR, MINOR, PATCH, PRE].compact.join('.')
- end
- end
- end
+ VERSION = "0.1.0" unless defined?(ArmoryApi::VERSION)
end
@@ -0,0 +1,19 @@
+require 'faraday'
+
+# @api private
+module Faraday
+ class Response::RaiseArmoryApiError < Response::Middleware
+ def on_complete(res)
+ case res[:status].to_i
+ when 404
+ raise ArmoryApi::NotFound, error_message(res)
+ when 500
+ raise ArmoryApi::InternalServerError, error_message(res)
+ end
+ end
+
+ def error_message(res)
+ "#{res[:method].to_s.upcase} #{res[:url].to_s}: #{res[:body]["reason"]}"
+ end
+ end
+end
@@ -0,0 +1,16 @@
+require 'helper'
+
+describe ArmoryApi::Client::Achievement do
+ describe ".achievement" do
+ before do
+ @client = ArmoryApi::Client.new
+ end
+
+ it "returns the specified achievement" do
+ stub_request(:get, 'http://us.battle.net/api/wow/achievement/2144')
+ .to_return(body: fixture('achievement/achievement.json'))
+ achievement = @client.achievement(2144)
+ expect(achievement.id).to eq 2144
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 9a9837d

Please sign in to comment.