lib/rubytter.rb in jugyo-rubytter-0.4.4 vs lib/rubytter.rb in jugyo-rubytter-0.4.5
- old
+ new
@@ -8,11 +8,11 @@
class Rubytter
class APIError < StandardError; end
APP_NAME = 'Rubytter'
- VERSION = '0.4.4'
+ VERSION = '0.4.5'
HOMEPAGE = 'http://github.com/jugyo/rubytter'
def initialize(login, password, options = {})
@login = login
@password = password
@@ -62,17 +62,17 @@
method, path, http_method = *array
http_method ||= 'get'
if /%s$/ =~ path
eval <<-EOS
def #{method}(id, params = {})
- #{http_method}('#{path}' % id, params)
+ #{http_method}(@host, '#{path}' % id, params)
end
EOS
else
eval <<-EOS
def #{method}(params = {})
- #{http_method}('#{path}', params)
+ #{http_method}(@host, '#{path}', params)
end
EOS
end
end
@@ -89,34 +89,33 @@
when String
{:q => arg}
when Hash
arg
end
- path = '/search.json?' + to_param_str(params)
- req = create_request(Net::HTTP::Post.new(path))
- http_request(req, nil, "search.#{@host}")
+ get("search.#{@host}", '/search', params)
end
- def get(path, params = {})
+ def get(host, path, params = {})
+ host ||= @host
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(req)
+ http_request(host, req)
end
- def post(path, params = {})
+ def post(host, path, params = {})
+ host ||= @host
path += '.json'
param_str = to_param_str(params)
req = create_request(Net::HTTP::Post.new(path))
- http_request(req, param_str)
+ http_request(host, req, param_str)
end
alias delete post
- def http_request(req, param_str = nil, host = nil)
- host ||= @host
+ def http_request(host, req, param_str = nil)
res = @connection.start(host) do |http|
if param_str
http.request(req, param_str)
else
http.request(req)
@@ -158,8 +157,9 @@
json
end
end
def to_param_str(hash)
+ raise ArgumentError, 'Argument must be a Hash object' unless hash.is_a?(Hash)
hash.to_a.map{|i| i[0].to_s + '=' + CGI.escape(i[1].to_s) }.join('&')
end
end