Skip to content

Commit

Permalink
Support for identi.ca and other api's that mirror twitter added. Upda…
Browse files Browse the repository at this point in the history
…ted several api calls to use post instead of get as twitter changed those in the past few days.
  • Loading branch information
jnunemaker committed Aug 3, 2008
1 parent f5477dc commit ed06aaf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
4 changes: 4 additions & 0 deletions History.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
0.3.3 - August 3, 2008
* Now has option for host when initializing to support identi.ca (Dustin Sallings)
* Twitter changed a bunch of methods to POST only so I updated those to now work

0.3.2 - July 26, 2008
* added the CLI gems as dependencies for now until I separate out the CLI from the API wrapper
* cleaner CLI errors for no active account or no accounts at all
Expand Down
33 changes: 18 additions & 15 deletions lib/twitter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
# 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')
def initialize(email, password, options={})
@config, @config[:email], @config[:password] = {}, email, password
@api_host = host
@api_host = options.delete(:api_host) || 'twitter.com'
end

# Returns an array of statuses for a timeline; Defaults to your friends timeline.
Expand Down Expand Up @@ -97,12 +96,12 @@ def d(user, text)

# 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.
Expand All @@ -113,22 +112,22 @@ def friendship_exists?(user_a, user_b)

# 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
Expand All @@ -138,22 +137,22 @@ def favorites(options={})

# 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.
Expand Down Expand Up @@ -199,15 +198,19 @@ def call(method, options={})

# 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

begin
response = Net::HTTP.start(@api_host, 80) do |http|
req = Net::HTTP::Get.new('/' + path, options[:headers])
klass = Net::HTTP.const_get options[:method].to_s.downcase.capitalize
req = klass.new('/' + path, options[:headers])
req.basic_auth(@config[:email], @config[:password]) if options[:auth]
http.request(req)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Twitter #:nodoc:
module VERSION #:nodoc:
MAJOR = 0
MINOR = 3
TINY = 2
TINY = 3

STRING = [MAJOR, MINOR, TINY].join('.')
end
Expand Down

0 comments on commit ed06aaf

Please sign in to comment.