lib/rubytter.rb in jugyo-rubytter-0.4.6 vs lib/rubytter.rb in jugyo-rubytter-0.4.7

- old
+ new

@@ -8,21 +8,21 @@ class Rubytter class APIError < StandardError; end APP_NAME = 'Rubytter' - VERSION = '0.4.6' + VERSION = '0.4.7' HOMEPAGE = 'http://github.com/jugyo/rubytter' attr_reader :login attr_accessor :host, :header - def initialize(login, password, options = {}) + def initialize(login = nil, password = nil, options = {}) @login = login @password = password @host = options[:host] || 'twitter.com' - @header = options[:header] || {'User-Agent', "#{APP_NAME}/#{VERSION} (#{HOMEPAGE})"} + @header = options[:header] || {'User-Agent' => "#{APP_NAME}/#{VERSION} (#{HOMEPAGE})"} @connection = Connection.new(options) end def self.api_settings # method name path for API http method @@ -66,17 +66,17 @@ method, path, http_method = *array http_method ||= 'get' if /%s$/ =~ path eval <<-EOS def #{method}(id, params = {}) - #{http_method}(@host, '#{path}' % id, params) + #{http_method}('#{path}' % id, params) end EOS else eval <<-EOS def #{method}(params = {}) - #{http_method}(@host, '#{path}', params) + #{http_method}('#{path}', params) end EOS end end @@ -86,35 +86,31 @@ def direct_message(user, text, params = {}) send_direct_message(params.merge({:user => user, :text => text})) end - def search(arg) - params = case arg - when String - {:q => arg} - when Hash - arg - end - get("search.#{@host}", '/search', params) + def search(query, params = {}) + path = '/search.json' + param_str = '?' + to_param_str(params.merge({:q => query})) + path = path + param_str unless param_str.empty? + req = create_request(Net::HTTP::Get.new(path), false) + http_request("search.#{@host}", req) end - def get(host, path, params = {}) - host ||= @host + def get(path, params = {}) path += '.json' param_str = '?' + to_param_str(params) path = path + param_str unless param_str.empty? req = create_request(Net::HTTP::Get.new(path)) - http_request(host, req) + http_request(@host, req) end - def post(host, path, params = {}) - host ||= @host + def post(path, params = {}) path += '.json' param_str = to_param_str(params) req = create_request(Net::HTTP::Post.new(path)) - http_request(host, req, param_str) + http_request(@host, req, param_str) end alias delete post def http_request(host, req, param_str = nil) @@ -132,12 +128,12 @@ else raise APIError, struct.error end end - def create_request(req) + def create_request(req, basic_auth = true) @header.each {|k, v| req.add_field(k, v) } - req.basic_auth(@login, @password) + req.basic_auth(@login, @password) if basic_auth req end def json_to_struct(json) case json