lib/twitter/base.rb in dustin-twitter-0.3.2.2 vs lib/twitter/base.rb in dustin-twitter-0.3.7

- old
+ new

@@ -3,16 +3,19 @@ # # For complete documentation on the options, check out the twitter api docs. # http://groups.google.com/group/twitter-development-talk/web/api-documentation module Twitter class Base - # Initializes the configuration for making requests to twitter - def initialize(email, password, host='twitter.com', path='') + # Twitter example: + # Twitter.new('email/username', 'password') + # + # Identi.ca example: + # Twitter.new('email/username', 'password', :api_host => 'identi.ca/api') + def initialize(email, password, options={}) @config, @config[:email], @config[:password] = {}, email, password - @api_host = host - @api_path = path + @api_host = options.delete(:api_host) || 'twitter.com' end # Returns an array of statuses for a timeline; Defaults to your friends timeline. def timeline(which=:friends, options={}) raise UnknownTimeline unless [:friends, :public, :user].include?(which) @@ -81,101 +84,91 @@ (doc/:direct_message).inject([]) { |dms, dm| dms << DirectMessage.new_from_xml(dm); dms } end # destroys a give direct message by id if the auth user is a recipient def destroy_direct_message(id) - request("direct_messages/destroy/#{id}.xml", :auth => true) + DirectMessage.new_from_xml(request("direct_messages/destroy/#{id}.xml", :auth => true, :method => :post)) end # Sends a direct message <code>text</code> to <code>user</code> def d(user, text) - url = URI.parse("http://#{@api_host}#{@api_path}/direct_messages/new.xml") - req = Net::HTTP::Post.new(url.path) - req.basic_auth(@config[:email], @config[:password]) - req.set_form_data({'text' => text, 'user' => user}) - response = Net::HTTP.new(url.host, url.port).start { |http| http.request(req) } - DirectMessage.new_from_xml(parse(response.body).at('direct_message')) + DirectMessage.new_from_xml(request('direct_messages/new.xml', :auth => true, :method => :post, :form_data => {'text' => text, 'user' => user})) end # Befriends id_or_screenname for the auth user def create_friendship(id_or_screenname) - users(request("friendships/create/#{id_or_screenname}.xml", :auth => true)).first + users(request("friendships/create/#{id_or_screenname}.xml", :auth => true, :method => :post)).first end # Defriends id_or_screenname for the auth user def destroy_friendship(id_or_screenname) - users(request("friendships/destroy/#{id_or_screenname}.xml", :auth => true)).first + users(request("friendships/destroy/#{id_or_screenname}.xml", :auth => true, :method => :post)).first end # Returns true if friendship exists, false if it doesn't. def friendship_exists?(user_a, user_b) doc = request(build_path("friendships/exists.xml", {:user_a => user_a, :user_b => user_b}), :auth => true) doc.at('friends').innerHTML == 'true' ? true : false end # Updates your location and returns Twitter::User object def update_location(location) - users(request(build_path('account/update_location.xml', {'location' => location}), :auth => true)).first + users(request(build_path('account/update_location.xml', {'location' => location}), :auth => true, :method => :post)).first end # Updates your deliver device and returns Twitter::User object def update_delivery_device(device) - users(request(build_path('account/update_delivery_device.xml', {'device' => device}), :auth => true)).first + users(request(build_path('account/update_delivery_device.xml', {'device' => device}), :auth => true, :method => :post)).first end # Turns notifications by id_or_screenname on for auth user. def follow(id_or_screenname) - users(request("notifications/follow/#{id_or_screenname}.xml", :auth => true)).first + users(request("notifications/follow/#{id_or_screenname}.xml", :auth => true, :method => :post)).first end # Turns notifications by id_or_screenname off for auth user. def leave(id_or_screenname) - users(request("notifications/leave/#{id_or_screenname}.xml", :auth => true)).first + users(request("notifications/leave/#{id_or_screenname}.xml", :auth => true, :method => :post)).first end # Returns the most recent favorite statuses for the autenticating user def favorites(options={}) statuses(request(build_path('favorites.xml', parse_options(options)), :auth => true)) end # Favorites the status specified by id for the auth user def create_favorite(id) - statuses(request("favorites/create/#{id}.xml", :auth => true)).first + statuses(request("favorites/create/#{id}.xml", :auth => true, :method => :post)).first end # Un-favorites the status specified by id for the auth user def destroy_favorite(id) - statuses(request("favorites/destroy/#{id}.xml", :auth => true)).first + statuses(request("favorites/destroy/#{id}.xml", :auth => true, :method => :post)).first end # Blocks the user specified by id for the auth user def block(id) - users(request("blocks/create/#{id}.xml", :auth => true)).first + users(request("blocks/create/#{id}.xml", :auth => true, :method => :post)).first end # Unblocks the user specified by id for the auth user def unblock(id) - users(request("blocks/destroy/#{id}.xml", :auth => true)).first + users(request("blocks/destroy/#{id}.xml", :auth => true, :method => :post)).first end # Posts a new update to twitter for auth user. def post(status, options={}) form_data = {'status' => status} - form_data.merge({'source' => options[:source]}) if options[:source] - url = URI.parse("http://#{@api_host}#{@api_path}/statuses/update.xml") - req = Net::HTTP::Post.new(url.path) - req.basic_auth(@config[:email], @config[:password]) - req.set_form_data(form_data) - response = Net::HTTP.new(url.host, url.port).start { |http| http.request(req) } - Status.new_from_xml(parse(response.body).at('status')) + form_data.merge!({'source' => options[:source]}) if options[:source] + Status.new_from_xml(request('statuses/update.xml', :auth => true, :method => :post, :form_data => form_data)) end alias :update :post # Verifies the credentials for the auth user. # raises Twitter::CantConnect on failure. def verify_credentials - request('account/verify_credentials', :auth => true) + request('account/verify_credentials.xml', :auth => true) end private # Converts an hpricot doc to an array of statuses def statuses(doc) @@ -198,19 +191,28 @@ request(build_path("statuses/#{method.to_s}.xml", args), options) end # Makes a request to twitter. def request(path, options={}) - options.reverse_merge!({:headers => { "User-Agent" => @config[:email] }}) + options.reverse_merge!({ + :headers => { "User-Agent" => @config[:email] }, + :method => :get + }) unless options[:since].blank? since = options[:since].kind_of?(Date) ? options[:since].strftime('%a, %d-%b-%y %T GMT') : options[:since].to_s options[:headers]["If-Modified-Since"] = since end + uri = URI.parse("http://#{@api_host}") + begin - response = Net::HTTP.start(@api_host, 80) do |http| - req = Net::HTTP::Get.new('/' + path, options[:headers]) + response = Net::HTTP.start(uri.host, 80) do |http| + klass = Net::HTTP.const_get options[:method].to_s.downcase.capitalize + req = klass.new("#{uri.path}/#{path}", options[:headers]) req.basic_auth(@config[:email], @config[:password]) if options[:auth] + if options[:method].to_s == 'post' && options[:form_data] + req.set_form_data(options[:form_data]) + end http.request(req) end rescue => error raise CantConnect, error.message end \ No newline at end of file