lib/pilha.rb in pilha-0.1.5 vs lib/pilha.rb in pilha-0.1.6
- old
+ new
@@ -11,10 +11,11 @@
require 'pilha/stack_overflow/statistics'
require 'pilha/stack_overflow/badge'
require 'pilha/stack_overflow/user'
require 'pilha/stack_overflow/answer'
require 'pilha/stack_overflow/comment'
+require 'pilha/stack_overflow/question'
module StackExchange
module StackOverflow
class Client
@@ -29,11 +30,11 @@
def config &block
options = OpenStruct.new
yield options if block_given?
client = Client.new(options)
- include_client(client, Badge, Statistics, User, Answer, Comment)
+ include_client(client, Badge, Statistics, User, Answer, Comment, Question)
end
def include_client(client, *classes)
classes.each do |klass|
klass.instance_variable_set(:@client, client)
@@ -46,11 +47,11 @@
@api_version = options.api_version || API_VERSION
@api_key = options.api_key
end
def api_method_path(pattern, options = {})
- pattern << '/' unless pattern.end_with? '/'
+ pattern = normalize(pattern)
parts = pattern.split('/').select { |part| part =~ /^:/ }
parts.each do |part|
key = part.sub(':', '').intern
pattern.sub!(part, options[key].to_s)
@@ -64,29 +65,25 @@
root_path + api_method_path(method, options) + query_string(options)
end
def get(url)
stream = open(url) { |stream| Zlib::GzipReader.new(stream).read }
- JSON.parse stream
+ JSON.parse(stream)
end
def root_path
url + api_version
end
def request(path, options)
- get(api_method_url(path, options))
+ get api_method_url(path, options)
end
private
- def key?
- !!@api_key
- end
-
def query_string(options)
- params = options[:query]
- if params
+ if params = options[:query]
params = params.sort_by { |k, v| k.to_s }
+
'?' + params.inject([]) do |arr, (key, value)|
arr << "#{key}=#{value}"
end.join('&')
else
''