lib/pilha.rb in pilha-0.2.0 vs lib/pilha.rb in pilha-0.2.1

- old
+ new

@@ -1,10 +1,8 @@ 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 'ostruct' @@ -18,47 +16,47 @@ require 'pilha/stack_overflow/question' require 'pilha/stack_overflow/statistics' module StackExchange module StackOverflow + + VERSION = '0.2.1' + class Client URL = 'http://api.stackoverflow.com/' API_VERSION = '1.0' attr_reader :url attr_reader :api_version attr_reader :api_key class << self - def config &block + + def config(&block) options = OpenStruct.new yield options if block_given? - init_client! Client.new(options) + @client = Client.new(options) end - def init_client!(client) - base_eigenclass = class << Base; self; end - base_eigenclass.send :define_method, :client do - @client = client - end - client + def instance + @client end + end def initialize(options = OpenStruct.new) - @url = normalize(options.url || URL ) + @url = normalize(options.url || URL) @api_version = options.api_version || API_VERSION @api_key = options.api_key end def api_method_path(pattern, options = {}) pattern = normalize(pattern) - parts = pattern.split('/').select { |part| part =~ /^:/ } - parts.each do |part| - key = part.sub(':', '').intern - pattern.sub!(part, options[key].to_s) + pattern.scan(/:(\w+)/).each do |part| + val = part.first + pattern.sub!(":" + val, options[val.to_sym].to_s) end pattern end @@ -66,12 +64,11 @@ options.merge! :api_key => api_key if api_key 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(Zlib::GzipReader.new(open(url)).read) end def root_path url + api_version end @@ -84,10 +81,10 @@ def query_string(options) params = options[:query] || options[:conditions] return '' unless params params = params.sort_by { |k, v| k.to_s } - pairs = params.map { |key, value| "#{key}=#{value}" } + pairs = params.map { |k, v| "#{k}=#{v}" } '?' + pairs.join('&') end def normalize(url)