Skip to content

Commit

Permalink
Updated since option to use HTTP header, and added the option on time…
Browse files Browse the repository at this point in the history
…line() and replies().
  • Loading branch information
danielmorrison committed Mar 29, 2008
1 parent 40686b2 commit 90b5b5e
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions lib/twitter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ def initialize(email, password)
# Returns an array of statuses for a timeline;
# Available timelines are determined from the @@timelines variable
# Defaults to your friends timeline
def timeline(which=:friends)
def timeline(which=:friends, since=nil)
raise UnknownTimeline unless @@timelines.include?(which)
auth = which.to_s.include?('public') ? false : true
statuses(call("#{which}_timeline", :auth => auth))
statuses(call("#{which}_timeline", :auth => auth, :since => since))
end

# Returns an array of users who are in your friends list
Expand Down Expand Up @@ -59,8 +59,8 @@ def user(id_or_screenname)
end

# Returns an array of statuses that are replies
def replies
statuses(call(:replies))
def replies(since=nil)
statuses(call(:replies, :since => since))
end

# Destroys a status by id
Expand All @@ -79,8 +79,7 @@ def featured
# TODO: allow since_id and page as well for direct messages
def direct_messages(since=nil)
path = 'direct_messages.xml'
since.nil? ? 1 : path << "?since=#{CGI.escape(since.to_s)}"
doc = request(path, { :auth => true })
doc = request(path, { :auth => true, :since => since })
(doc/:direct_message).inject([]) { |dms, dm| dms << DirectMessage.new_from_xml(dm); dms }
end
alias :received_messages :direct_messages
Expand All @@ -89,8 +88,7 @@ def direct_messages(since=nil)
# TODO: allow since_id and page as well for sent messages
def sent_messages(since=nil)
path = 'direct_messages/sent.xml'
since.nil? ? 1 : path << "?since=#{CGI.escape(since.to_s)}"
doc = request(path, { :auth => true })
doc = request(path, { :auth => true, :since => since })
(doc/:direct_message).inject([]) { |dms, dm| dms << DirectMessage.new_from_xml(dm); dms }
end

Expand Down Expand Up @@ -165,14 +163,19 @@ def call(method, options={})

def request(path, options={})
options.reverse_merge!({:headers => { "User-Agent" => @config[:email] }})
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_url, 80) do |http|
req = Net::HTTP::Get.new('/' + path, options[:headers])
req.basic_auth(@config[:email], @config[:password]) if options[:auth]
http.request(req)
end

raise BadResponse unless response.message == 'OK'
raise BadResponse unless response.message == 'OK' || response.message == 'Not Modified'
parse(response.body)
rescue
raise CantConnect
Expand Down

0 comments on commit 90b5b5e

Please sign in to comment.