lib/pilha.rb in pilha-0.1.6 vs lib/pilha.rb in pilha-0.1.7
- old
+ new
@@ -1,24 +1,25 @@
path = File.expand_path(File.dirname(__FILE__))
$LOAD_PATH.unshift(path) unless $LOAD_PATH.include?(path)
require 'rubygems'
+
require 'zlib'
require 'json'
require 'open-uri'
require 'forwardable'
require 'pilha/stack_overflow/base'
-require 'pilha/stack_overflow/statistics'
-require 'pilha/stack_overflow/badge'
+require 'pilha/stack_overflow/tag'
require 'pilha/stack_overflow/user'
+require 'pilha/stack_overflow/badge'
require 'pilha/stack_overflow/answer'
require 'pilha/stack_overflow/comment'
require 'pilha/stack_overflow/question'
+require 'pilha/stack_overflow/statistics'
module StackExchange
-
module StackOverflow
class Client
URL = 'http://api.stackoverflow.com/'
API_VERSION = '0.8'
@@ -28,19 +29,19 @@
class << self
def config &block
options = OpenStruct.new
yield options if block_given?
-
- client = Client.new(options)
- include_client(client, Badge, Statistics, User, Answer, Comment, Question)
+ init_client! Client.new(options)
end
- def include_client(client, *classes)
- classes.each do |klass|
- klass.instance_variable_set(:@client, client)
+ def init_client!(client)
+ base_eigenclass = class << Base; self; end
+ base_eigenclass.send :define_method, :client do
+ @client = client
end
+ client
end
end
def initialize(options = OpenStruct.new)
@url = normalize(options.url || URL )
@@ -48,11 +49,11 @@
@api_key = options.api_key
end
def api_method_path(pattern, options = {})
pattern = normalize(pattern)
- parts = pattern.split('/').select { |part| part =~ /^:/ }
+ parts = pattern.split('/').select { |part| part =~ /^:/ }
parts.each do |part|
key = part.sub(':', '').intern
pattern.sub!(part, options[key].to_s)
end
@@ -78,18 +79,16 @@
get api_method_url(path, options)
end
private
def query_string(options)
- if params = options[:query]
- params = params.sort_by { |k, v| k.to_s }
+ params = options[:query] || options[:conditions]
+ return '' unless params
- '?' + params.inject([]) do |arr, (key, value)|
- arr << "#{key}=#{value}"
- end.join('&')
- else
- ''
- end
+ params = params.sort_by { |k, v| k.to_s }
+ pairs = params.map { |key, value| "#{key}=#{value}" }
+
+ '?' + pairs.join('&')
end
def normalize(url)
url.end_with?('/') ? url : url + '/'
end