Skip to content

Commit

Permalink
Make Twitter::Cursor an Enumerable
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Aug 13, 2012
1 parent 23cfaf9 commit 2582f2e
Show file tree
Hide file tree
Showing 20 changed files with 128 additions and 78 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ Here are some fun facts about the 3.0 release:

* The entire library is implemented in just 2,000 lines of code
* With over 5,000 lines of specs, the spec-to-code ratio is over 2.5:1
* The spec suite contains 672 examples and runs in under 2 seconds on a MacBook
* The spec suite contains 673 examples and runs in under 2 seconds on a MacBook
* This project has 100% C0 code coverage (the tests execute every line of
source code at least once)
* At the time of release, this library is comprehensive: you can request all
Expand Down
24 changes: 12 additions & 12 deletions lib/twitter/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
require 'twitter/core_ext/array'
require 'twitter/core_ext/enumerable'
require 'twitter/core_ext/hash'
require 'twitter/core_ext/kernel'
require 'twitter/cursor'
require 'twitter/direct_message'
require 'twitter/error/forbidden'
require 'twitter/error/not_found'
require 'twitter/language'
require 'twitter/list'
require 'twitter/oembed'
require 'twitter/place'
require 'twitter/rate_limit_status'
require 'twitter/relationship'
Expand Down Expand Up @@ -166,12 +168,10 @@ module API
#
# @raise [ArgumentError] Error raised when supplied argument is not a key in the METHOD_RATE_LIMITED hash.
# @return [Boolean]
# @param method [Symbol]
def rate_limited?(method)
method_rate_limited = METHOD_RATE_LIMITED[method.to_sym]
if method_rate_limited.nil?
raise ArgumentError.new("no method `#{method}' for #{self.class}")
end
# @param method_name [Symbol]
def rate_limited?(method_name)
method_rate_limited = METHOD_RATE_LIMITED[method_name.to_sym]
raise ArgumentError.new("no method `#{method_name}' for #{self.class}") if method_rate_limited.nil?
method_rate_limited
end

Expand Down Expand Up @@ -2612,19 +2612,19 @@ def ids_from_response(request_method, url, args)
options = args.extract_options!
merge_default_cursor!(options)
options.merge_user!(args.pop)
cursor_from_response(:ids, nil, request_method, url, options)
cursor_from_response(:ids, nil, request_method, url, options, {}, calling_method)
end

# @param method [Symbol]
# @param collection_name [Symbol]
# @param klass [Class]
# @param request_method [Symbol]
# @param url [String]
# @param params [Hash]
# @param options [Hash]
# @return [Twitter::Cursor]
def cursor_from_response(method, klass, request_method, url, params={}, options={})
def cursor_from_response(collection_name, klass, request_method, url, params={}, options={}, method_name=calling_method)
response = send(request_method.to_sym, url, params, options)
Twitter::Cursor.from_response(response, method.to_sym, klass)
Twitter::Cursor.from_response(response, collection_name.to_sym, klass, self, method_name, params)
end

# @param request_method [Symbol]
Expand All @@ -2646,7 +2646,7 @@ def lists_from_response(request_method, url, args)
options = args.extract_options!
merge_default_cursor!(options)
options.merge_user!(args.pop)
cursor_from_response(:lists, Twitter::List, request_method, url, options)
cursor_from_response(:lists, Twitter::List, request_method, url, options, {}, calling_method)
end

# @param request_method [Symbol]
Expand Down Expand Up @@ -2686,7 +2686,7 @@ def list_users(request_method, url, args)
merge_default_cursor!(options)
options.merge_list!(args.pop)
options.merge_owner!(args.pop || verify_credentials.screen_name) unless options[:owner_id] || options[:owner_screen_name]
cursor_from_response(:users, Twitter::User, request_method, url, options)
cursor_from_response(:users, Twitter::User, request_method, url, options, {}, calling_method)
end

# @param klass [Class]
Expand Down
4 changes: 4 additions & 0 deletions lib/twitter/core_ext/kernel.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
module Kernel

def calling_method
caller[1][/`([^']*)'/, 1].to_sym
end

# Returns the object's singleton class (exists in Ruby 1.9.2)
def singleton_class; class << self; self; end; end unless method_defined?(:singleton_class)

Expand Down
36 changes: 30 additions & 6 deletions lib/twitter/cursor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,58 @@

module Twitter
class Cursor
include Enumerable
attr_reader :attrs, :collection
alias to_hash attrs

# Initializes a new Cursor object
#
# @param response [Hash]
# @param method [String, Symbol] The name of the method to return the collection
# @param collection_name [String, Symbol] The name of the method to return the collection
# @param klass [Class] The class to instantiate object in the collection
# @param client [Twitter::Client]
# @param method_name [String, Symbol]
# @param method_options [Hash]
# @return [Twitter::Cursor]
def self.from_response(response={}, method=:ids, klass=nil)
new(response[:body], method, klass)
def self.from_response(response, collection_name, klass, client, method_name, method_options)
new(response[:body], collection_name, klass, client, method_name, method_options)
end

# Initializes a new Cursor
#
# @param attrs [Hash]
# @param collection_name [String, Symbol] The name of the method to return the collection
# @param klass [Class] The class to instantiate object in the collection
# @param client [Twitter::Client]
# @param method_name [String, Symbol]
# @param method_options [Hash]
# @return [Twitter::Cursor]
def initialize(attrs={}, method=:ids, klass=nil)
def initialize(attrs, collection_name, klass, client, method_name, method_options)
@attrs = attrs
@collection = Array(attrs[method.to_sym]).map do |item|
@client = client
@method_name = method_name
@method_options = method_options
@collection = Array(attrs[collection_name.to_sym]).map do |item|
if klass
klass.fetch_or_new(item)
else
item
end
end
class_eval do
alias_method(method.to_sym, :collection)
alias_method(collection_name.to_sym, :collection)
end
end

def each
cursor = self
results = collection
until cursor.last?
cursor = @client.send(@method_name.to_sym, @method_options.merge(:cursor => cursor.next_cursor))
results += cursor.collection
end
results.each do |result|
yield result
end
end

Expand Down
3 changes: 1 addition & 2 deletions lib/twitter/status.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require 'twitter/client'
require 'twitter/core_ext/hash'
require 'twitter/creatable'
require 'twitter/entity/hashtag'
Expand All @@ -8,7 +7,6 @@
require 'twitter/identity'
require 'twitter/media_factory'
require 'twitter/metadata'
require 'twitter/oembed'
require 'twitter/place'
require 'twitter/user'

Expand Down Expand Up @@ -68,6 +66,7 @@ def metadata
@metadata ||= Twitter::Metadata.fetch_or_new(@attrs[:metadata])
end

# @deprecated This method will be removed in version 4.
# @return [Twitter::OEmbed]
def oembed(options={})
@oembed ||= Twitter.oembed(@attrs[:id], options)
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/id_list.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions spec/fixtures/id_list2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"previous_cursor":-1305101990888327757,"next_cursor_str":"0","ids":[20009713,23621851,14509199,34113917,46009425,14213563,16860796,44375371,14270494,17151609,15147519,17352388,45523384,27123829,14726450,34540298,22132860,44425795,28983323,43173864,14692528,40331955,7221352,10715872,15083358,27490742,8628172,9931462,7753992,11700172,13064752,44125567,44390468,10293122,40622606,40620387,816166,12981172,10307212,18630206,16161672,6486552,21287624,22849350,42233617,8914,17659719,39078373,14178681,19833105,30164179,26097408,40759535,42114824,19661094,41409768,16467040,25431710,26590279,14582075,24584069,20162454,23033582,6144052,12480172,40461227,5644142,29584530,33044111,4243,25240991,16593287,39043477,12441712,6745812,15378358,14331281,35569718,14989175,19461796,38357782,26430264,19286059,38658437,23924401,19769305,21355783,816612,18665164,38482349,14479695,24053475,30694331,14394112,36788983,14486263,36775002,30433672,36984066,23899781,38794200,1623,14935192,17507985,16557936,1566201,5517522,15700362,5795012,37521114,7524442,11590,14701791,18436296,37630248,18237764,6147222,13130412,37429637,12777302,38228226,946471,9907312,70953,32545982,22202166,15644361,25463806,35953,9980812,33855474,19757068,19186982,17915624,18927603,34985053,6114732,82363,35821109,13118252,16783527,14118149,24572641,14328192,5815992,8610562,14101882,16115338,7267742,1011261,21605705,28058383,6618012,14464510,13604142,24173262,5666142,22776663,16997374,6897732,24425454,12164202,1333081,34007651,34562629,32455401,14301221,20131183,14989787,20879578,22046389,14127807,14143421,14246143,8275402,746743,11551482,36731700,10400102,35835799,14475886,21917950,15661031,14126778,13931352,15572516,36331453,27478163,29965883,18577567,16246192,14800339,17104441,16987758,16100431,19134551,16130949,20902957,1294621,35530826,35857708,23322767,5906542,13643732,16993684,20310864,34398987,18945415,34599558,20369873,15684121,15941004,19603499,17275108,35033596,15151807,26488247,34799968,31415899,25249151,15752142,19506770,16619902,1485431,16075354,23277545,10803502,20847786,23372855,1503071,26701160,11964902,33113199,21213437,29823212,15237920,14147680,7212272,15332589,16492462,6152112,993331,28688647,33643591,33587124,5760732,15612960,18940574,10247222,24380912,19025233,13272072,15022241,18859100,33097847,32041900,31165493,14633814,16554059,8197562,14701473,22824562,24753990,14605495,17767644,22501273,14793171,20773696,19881382,24790223,6508722,5449422,14748680,17987039,28176898,25618053,18990861,17829574,15022895,22640618,15479835,25468321,14542767,5551752,16746457,30935758,23245206,10117892,14122577,15606388,12986052,30327006,18981198,30857286,15539347,22432303,24449950,14726594,6138042,17532309,16549129,21783147,22199928,24337514,30255669,9336642,17970968,16995831,15923445,6164712,3791401,25660475,19320827,21059384,15086185,18434849,27664264,30383644,25122917,28412904,26855072,28604756,774234,19541886,15976584,1652061,23624201,25784035,17786078,18187527,20701384,24412216,30024632,27735210,18804771,15945457,15417432,12387772,21918148,18079697,14562259,16344576,20601108,13157072,7557612,16038571,19311676,17302101,14457722,17543145,16387420,29462028,21540703,15815339,18212369,25789084,17349603,22328258,12661,16174902,28006457,17811921,52593,24035144,5538522,12049142,1261161,14670993,768046,21310473,5777902,2847471,15127864,17067240,22303418,8675322,6477492,15975049,8740302,25238209,20847149,17991183,23900202,26128606,26117425,22259719,18428400,7766772,8332382,14196921,20552489,17393013,16862018,14516972,20573490,18908187,28113481,11864452,12333902,15506002,18340324,12095912,15345111,14213103,16941596,17541865,23046157,5695942,14645869,17813187,20299911,1259861,18940985,16481323,10286,16341831,14352376,7131912,9573202,9966122,7997612,18096834,15794195,645693,18639249,18891726,6111212,15777123,1073271,19877930,5576742,15668072,14595846,28258102,14966703,17910599,19104156,23667572,24701294,14120648,16051808,17800293,23723340,10295842,17934084,20485057,6715402,27500004,15199325,15250836,24951974,14415928,8124952,19771577,14736220,15931258,10413552,16093333,18456014,16116251,18539631,17830624,19312710,2347691,17495624,14604722,14726357,14148091,26449762,14221735,24329646,9214242,26174646,24403391,25983399,19912352,297423,18585360,21432664,14264323,16061710,25618407,11623852,5328352,17010913,20420322,17352686,17909107,2053181,6867422,24990180,20490645,15856366,14307582,7901202,1734381,14258434,1920941,23279212,24693623,24206411,14119960,12368532,872711,23511127,16841184,14804571,18631703,14095083,305853,14374075,18986865,14170535,13934902,9548262,22332826,14920994,13361822,815233,15185380,22286046,15075182,16987118,23561485,19737494,18727004,18693598,14998161,14660636,17632854,15891030,17922476,19259282,18932164,14874772,9686352,6297842,18946409,16824201,19473742,21062351,20505798,10942322,13839892,22400526,880,19307762,18390673,16385861,752193,21524655,713263,724493,14410169,18146109,5700242,14268544,12243992,14115608,813512,14669398,15534028,15746084,813198,21925497,14193132,18921376,19163684,5676102,5502392,13915552,1523901,19749442,21822796,19547405,4025811,16438594,6345962,16288332,20521987,15694286,20092638,18758973,1812341,874,18038678,4267931,18033114,15611876,21104240,21035849,16035749,8633122,5540992,14550267,15613757,21416431,20097342,18215460,19851346,14072222,16674837,8725052,16528978,19711609,14667222,10667662,17618799,16690982,19739126,15049040,14434748,14149271,661703,17979132,16310442,12982472,15268247,71323,20826783,9376412,14314572,16553230,15909778,14060523,18625004,18713667,18451400,5768872,19565929,15843791,14173692,15945604,18051375,17685196,15724505,18250327,16876339,15167134,18867067,20566009,19140083,19581787,18142375,18035142,19556561,6841612,15105269,14334898,20215874,5417132,19888698,15783111,13322212,5210841,17544805,14430060,616173,15991049,12820422,16264702,17009637,16899075,17868497,6782762,13939212,18891923,9508922,13976432,14311823,14624513,19504433,15756049,17139736,18016189,19779154,15504091,17800215,16893494,16173056,16449742,17447650,17089054,14908912,15631747,19462519,14363723,14557339,7219042,18276100,15949816,16032684,5790402,14455548,15829920,17007645,620863,3128101,18558029,18767697,14186439,17630279,14930128,7905122,13218102,6273552,15804503,17242874,6449282,19004266,14224719,15131310,18590391,18350514,17477349,18071434,18970455,15664760,18456416,16124241,18339150,14621598,18761352,17193201,17189654,18358765,15481585,17419869,14343564,17937528,3364,18665948,10908272,15116570,15377515,17622723,11855222,8542882,17591223,14081272,18087450,18579933,18351093,3308171,14306908,15230663,18379357,11071702,15448801,18518686,11340502,18326881,18276723,17310970,15751056,5357812,15161938,17456747,17023154,15394954,6830022,14707949,16103335,14803701,17407828,18169304,17601922,16692771,17243750,17218135,10615232,16539573,17299744,18192243,14800273,18364417,18059559,9153332,15593007,7429102,14604658,17841142,8453452,18176511,16953422,17926619,1363181,18020635,12646442,17738852,17122356,15239437,16494101,18222732,17918869,18141974,18165865,12726012,14361626,17486676,18108403,16653851,17907968,7752402,13673392,14203569,11843312,17037940,16457081,11993412,17996963,17331231,18166778,15753937,16751368,15741724,1101921,18166164,17072217,17850012,10260532,18128281,17093617,14520944,17856218,1002351,17095918,17995774,14110432,14815260,14190551,774909,10376152,5737542,17924945,796409,18063698,17291725,9643452,14228300,17922886,7617722,17192613,14401607,16420912,18012461,15996400,17993906,661253,17315407,17551089,15702490,6975262,17634834,15501073,17640120,16636824,17942260,14370639,15680684,17399741,16340301,16960896,14415941,17938657,6246412,14749294,16054649,17685359,898491,15648361,14190278,17360977,10168082,8349572,17926920,4371691,15601720,17949986,7040932,6242932,8965052,17598293,1409471,835261,16955717,17877399,14212434,16976960,14904437,13002092,16578133,9270712,9462972,14425248,15110706,17782142,5117751,10638482,14205289,17740582,3416,14713941,7742242,17545909,13831362,1589691,10089142,17621118,15899802,15032701,11922692,588073,17610490,11623272,728173,9695352,12621452,12672352,10286072,11445942,14410753,14925463,15005818,16647691,8882412,15916118,15989384,15954730,16502126,4276531,16418470,5493662,905161,14480635,16324597,16288148,14306648,9985382,792038,420783,11088,14355763,659933,639193,10646,14191781,6556972,16140418,6207402,806720,14762734,3492171,10422862,10336172,816954,10676022,1007991,15527007,14109477,15742947,3471901,14131856,14218211,2030231,127583,15336365,15318186,14150637,15255491,348963,14974273,811634,15036079,15081187,14166387,15024429,14997765,14271287,14991489,770545,756264,14955111,874171,14948977,2305631,14690927,8476702,5421112,755512,14109419,38503,6009762],"previous_cursor_str":"-1305101990888327757","next_cursor":0}
12 changes: 6 additions & 6 deletions spec/twitter/api/account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
it "returns the requesting user" do
user = @client.verify_credentials
user.should be_a Twitter::User
user.name.should eq "Erik Michaels-Ober"
user.id.should eq 7505382
end
end

Expand Down Expand Up @@ -71,7 +71,7 @@
it "returns a user" do
user = @client.update_delivery_device("sms")
user.should be_a Twitter::User
user.name.should eq "Erik Michaels-Ober"
user.id.should eq 7505382
end
end

Expand All @@ -90,7 +90,7 @@
it "returns a user" do
user = @client.update_profile(:url => "http://github.com/sferik/")
user.should be_a Twitter::User
user.name.should eq "Erik Michaels-Ober"
user.id.should eq 7505382
end
end

Expand All @@ -107,7 +107,7 @@
it "returns a user" do
user = @client.update_profile_background_image(fixture("we_concept_bg2.png"))
user.should be_a Twitter::User
user.name.should eq "Erik Michaels-Ober"
user.id.should eq 7505382
end
end

Expand All @@ -126,7 +126,7 @@
it "returns a user" do
user = @client.update_profile_colors(:profile_background_color => "000000")
user.should be_a Twitter::User
user.name.should eq "Erik Michaels-Ober"
user.id.should eq 7505382
end
end

Expand All @@ -143,7 +143,7 @@
it "returns a user" do
user = @client.update_profile_image(fixture("me.jpeg"))
user.should be_a Twitter::User
user.name.should eq "Erik Michaels-Ober"
user.id.should eq 7505382
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/twitter/api/blocks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
users = @client.blocking
users.should be_an Array
users.first.should be_a Twitter::User
users.first.name.should eq "Erik Michaels-Ober"
users.first.id.should eq 7505382
end
end

Expand Down Expand Up @@ -81,7 +81,7 @@
users = @client.block("sferik")
users.should be_an Array
users.first.should be_a Twitter::User
users.first.name.should eq "Erik Michaels-Ober"
users.first.id.should eq 7505382
end
end

Expand All @@ -101,7 +101,7 @@
users = @client.unblock("sferik")
users.should be_an Array
users.first.should be_a Twitter::User
users.first.name.should eq "Erik Michaels-Ober"
users.first.id.should eq 7505382
end
end

Expand Down
12 changes: 6 additions & 6 deletions spec/twitter/api/direct_messages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
direct_messages = @client.direct_messages_received
direct_messages.should be_an Array
direct_messages.first.should be_a Twitter::DirectMessage
direct_messages.first.sender.name.should eq "Erik Michaels-Ober"
direct_messages.first.sender.id.should eq 7505382
end
end

Expand All @@ -38,7 +38,7 @@
direct_messages = @client.direct_messages_sent
direct_messages.should be_an Array
direct_messages.first.should be_a Twitter::DirectMessage
direct_messages.first.sender.name.should eq "Erik Michaels-Ober"
direct_messages.first.sender.id.should eq 7505382
end
end

Expand All @@ -56,7 +56,7 @@
direct_messages = @client.direct_message_destroy(1825785544)
direct_messages.should be_an Array
direct_messages.first.should be_a Twitter::DirectMessage
direct_messages.first.sender.name.should eq "Erik Michaels-Ober"
direct_messages.first.sender.id.should eq 7505382
end
end

Expand Down Expand Up @@ -92,7 +92,7 @@
it "returns the specified direct message" do
direct_message = @client.direct_message(1825786345)
direct_message.should be_a Twitter::DirectMessage
direct_message.sender.name.should eq "Erik Michaels-Ober"
direct_message.sender.id.should eq 7505382
end
end

Expand All @@ -111,7 +111,7 @@
direct_messages = @client.direct_messages(1825786345)
direct_messages.should be_an Array
direct_messages.first.should be_a Twitter::DirectMessage
direct_messages.first.sender.name.should eq "Erik Michaels-Ober"
direct_messages.first.sender.id.should eq 7505382
end
end
context "without ids passed" do
Expand All @@ -128,7 +128,7 @@
direct_messages = @client.direct_messages
direct_messages.should be_an Array
direct_messages.first.should be_a Twitter::DirectMessage
direct_messages.first.sender.name.should eq "Erik Michaels-Ober"
direct_messages.first.sender.id.should eq 7505382
end
end
end
Expand Down
Loading

0 comments on commit 2582f2e

Please sign in to comment.