Module: TwitterApi

Defined in:
lib/old/twitter_api.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

GROK_URI_RE =

Regular expression to grok resource from uri

%r{http://twitter.com/(\w+/\w+)/(\w+)\.json\?page=(\d+)}
RESOURCE_PATH_FROM_CONTEXT =

Context <=> resource mapping

aka. repairing the non-REST uri’s

{
  :user            => 'users/show',
  :followers_ids   => 'followers/ids',
  :friends_ids     => 'friends/ids',
  :followers       => 'statuses/followers',
  :friends         => 'statuses/friends',
  :favorites       => 'favorites',
  :timeline        => 'statuses/user_timeline',
  :user_timeline   => 'statuses/user_timeline',
  :public_timeline => 'statuses/public_timeline'
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



69
70
71
# File 'lib/old/twitter_api.rb', line 69

def self.included base
  base.extend ClassMethods
end

.pages(context, thing) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/old/twitter_api.rb', line 47

def self.pages context, thing
  case context
  when :favorites       then pages_from_count( 20, thing.favourites_count, 20)
  when :friends         then pages_from_count(100, thing.friends_count,    10)
  when :followers       then pages_from_count(100, thing.followers_count,  10)
  when :followers_ids   then thing.followers_count == 0 ? 0 : 1
  when :friends_ids     then thing.friends_count   == 0 ? 0 : 1
  when :user            then 1
  when :public_timeline then 1
  when :user_timeline   then pages_from_count(200, thing.statuses_count,   20)
  when :search          then pages_from_count(100, 1500)
  else raise "need to define pages for context #{context}"
  end
end

.pages_from_count(per_page, count, max = nil) ⇒ Object



43
44
45
46
# File 'lib/old/twitter_api.rb', line 43

def self.pages_from_count per_page, count, max=nil
  num = [ (count.to_f / per_page.to_f).ceil, 0 ].max
  [num, max].compact.min
end

Instance Method Details

#gen_urlObject

The URI for a given resource



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/old/twitter_api.rb', line 5

def gen_url
  case context
  when :user, :followers, :friends, :favorites, :timeline
    "http://twitter.com/#{resource_path}/#{identifier}.json?page=#{page}"
  when :followers_ids, :friends_ids
    "http://twitter.com/#{resource_path}/#{identifier}.json"
  when :user_timeline
    "http://twitter.com/#{resource_path}/#{identifier}.json?page=#{page}&count=200"
  # when :public_timeline
  # when :search
  #  "http://search.twitter.com/search.json?q=#{query}"
  else
    raise "Don't know how to retrieve #{context} yet"
  end
end

#resource_pathObject

Get url resource for context



39
40
41
# File 'lib/old/twitter_api.rb', line 39

def resource_path
  RESOURCE_PATH_FROM_CONTEXT[context.to_sym]
end