lib/rubytter.rb in jugyo-rubytter-0.2.1 vs lib/rubytter.rb in jugyo-rubytter-0.2.2

- old
+ new

@@ -1,10 +1,12 @@ require 'rubygems' require 'json' require 'net/https' require 'cgi' +require 'rubytter/connection' + class Rubytter APP_NAME = self.to_s def initialize(login, password, options = {}) @login = login @@ -68,67 +70,31 @@ def get(path, params = {}) path += '.json' param_str = '?' + params.to_a.map{|i| i[0].to_s + '=' + CGI.escape(i[1]) }.join('&') path = path + param_str unless param_str.empty? - - req = Net::HTTP::Get.new(path) - req.add_field('User-Agent', 'Rubytter http://github.com/jugyo/rubytter') - req.basic_auth(@login, @password) + req = prepare_request(Net::HTTP::Get.new(path)) res_text = @connection.start(@host) do |http| http.request(req).body end return JSON.parse(res_text) end def post(path, params = {}) path += '.json' param_str = params.to_a.map{|i| i[0].to_s + '=' + CGI.escape(i[1]) }.join('&') - - req = Net::HTTP::Post.new(path) - req.add_field('User-Agent', 'Rubytter http://github.com/jugyo/rubytter') - req.basic_auth(@login, @password) + req = prepare_request(Net::HTTP::Post.new(path)) @connection.start(@host) do |http| http.request(req, param_str).body end end def delete(path, params = {}) post(path, params) end - class Connection - attr_reader :protocol, :port, :proxy_uri - - def initialize(options = {}) - @proxy_host = options[:proxy_host] - @proxy_port = options[:proxy_port] - @proxy_user = options[:proxy_user_name] - @proxy_password = options[:proxy_password] - @proxy_uri = nil - @enable_ssl = options[:enable_ssl] - - if @proxy_host - @http_class = Net::HTTP::Proxy(@proxy_host, @proxy_port, - @proxy_user, @proxy_password) - @proxy_uri = "http://" + @proxy_host + ":" + @proxy_port + "/" - else - @http_class = Net::HTTP - end - - if @enable_ssl - @protocol = "https" - @port = 443 - else - @protocol = "http" - @port = 80 - end - end - - def start(host, port = nil, &block) - http = @http_class.new(host, port || @port) - http.use_ssl = @enable_ssl - http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl? - http.start(&block) - end + def prepare_request(req) + req.add_field('User-Agent', 'Rubytter http://github.com/jugyo/rubytter') + req.basic_auth(@login, @password) + return req end end