-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
client.rb
2622 lines (2510 loc) · 147 KB
/
client.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require 'twitter/action_factory'
require 'twitter/authenticatable'
require 'twitter/config'
require 'twitter/configuration'
require 'twitter/connection'
require 'twitter/core_ext/enumerable'
require 'twitter/core_ext/hash'
require 'twitter/cursor'
require 'twitter/direct_message'
require 'twitter/error/forbidden'
require 'twitter/error/not_found'
require 'twitter/favorite'
require 'twitter/follow'
require 'twitter/language'
require 'twitter/list'
require 'twitter/mention'
require 'twitter/metadata'
require 'twitter/oembed'
require 'twitter/photo'
require 'twitter/place'
require 'twitter/point'
require 'twitter/polygon'
require 'twitter/rate_limit_status'
require 'twitter/relationship'
require 'twitter/reply'
require 'twitter/request'
require 'twitter/retweet'
require 'twitter/saved_search'
require 'twitter/search_results'
require 'twitter/settings'
require 'twitter/size'
require 'twitter/status'
require 'twitter/suggestion'
require 'twitter/trend'
require 'twitter/user'
module Twitter
# Wrapper for the Twitter REST API
#
# @note All methods have been separated into modules and follow the same grouping used in {http://dev.twitter.com/doc the Twitter API Documentation}.
# @see http://dev.twitter.com/pages/every_developer
class Client
include Twitter::Authenticatable
include Twitter::Connection
include Twitter::Request
attr_accessor *Config::VALID_OPTIONS_KEYS
MAX_USERS_PER_REQUEST = 100
# Initializes a new API object
#
# @param attrs [Hash]
# @return [Twitter::Client]
def initialize(attrs={})
attrs = Twitter.options.merge(attrs)
Config::VALID_OPTIONS_KEYS.each do |key|
instance_variable_set("@#{key}".to_sym, attrs[key])
end
end
# Returns the configured screen name or the screen name of the authenticated user
#
# @return [Twitter::User]
def current_user
@current_user ||= Twitter::User.new(self.verify_credentials.attrs)
end
# Returns the remaining number of API requests available to the requesting user
#
# @see https://dev.twitter.com/docs/api/1/get/account/rate_limit_status
# @rate_limited No
# @requires_authentication No
#
# This will return the requesting IP's rate limit status. If you want the authenticating user's rate limit status you must authenticate.
# @param options [Hash] A customizable set of options.
# @return [Twitter::RateLimitStatus]
# @example Return the remaining number of API requests available to the requesting user
# Twitter.rate_limit_status
def rate_limit_status(options={})
rate_limit_status = get("/1/account/rate_limit_status.json", options)
Twitter::RateLimitStatus.new(rate_limit_status)
end
# Returns the requesting user if authentication was successful, otherwise raises {Twitter::Error::Unauthorized}
#
# @see https://dev.twitter.com/docs/api/1/get/account/verify_credentials
# @rate_limited Yes
# @requires_authentication Yes
# @param options [Hash] A customizable set of options.
# @return [Twitter::User] The authenticated user.
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Return the requesting user if authentication was successful
# Twitter.verify_credentials
def verify_credentials(options={})
user = get("/1/account/verify_credentials.json", options)
Twitter::User.new(user)
end
# Ends the session of the authenticating user
#
# @see https://dev.twitter.com/docs/api/1/post/account/end_session
# @rate_limited No
# @requires_authentication Yes
# @param options [Hash] A customizable set of options.
# @return [Hash]
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example End the session of the authenticating user
# Twitter.end_session
def end_session(options={})
post("/1/account/end_session.json", options)
end
# Sets which device Twitter delivers updates to for the authenticating user
#
# @see https://dev.twitter.com/docs/api/1/post/account/update_delivery_device
# @rate_limited No
# @requires_authentication Yes
# @param device [String] Must be one of: 'sms', 'none'.
# @param options [Hash] A customizable set of options.
# @return [Twitter::User] The authenticated user.
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Turn SMS updates on for the authenticating user
# Twitter.update_delivery_device('sms')
def update_delivery_device(device, options={})
user = post("/1/account/update_delivery_device.json", options.merge(:device => device))
Twitter::User.new(user)
end
# Sets values that users are able to set under the "Account" tab of their settings page
#
# @see https://dev.twitter.com/docs/api/1/post/account/update_profile
# @note Only the options specified will be updated.
# @rate_limited No
# @requires_authentication Yes
# @param options [Hash] A customizable set of options.
# @option options [String] :name Full name associated with the profile. Maximum of 20 characters.
# @option options [String] :url URL associated with the profile. Will be prepended with "http://" if not present. Maximum of 100 characters.
# @option options [String] :location The city or country describing where the user of the account is located. The contents are not normalized or geocoded in any way. Maximum of 30 characters.
# @option options [String] :description A description of the user owning the account. Maximum of 160 characters.
# @return [Twitter::User] The authenticated user.
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Set authenticating user's name to Erik Michaels-Ober
# Twitter.update_profile(:name => "Erik Michaels-Ober")
def update_profile(options={})
user = post("/1/account/update_profile.json", options)
Twitter::User.new(user)
end
# Updates the authenticating user's profile background image
#
# @see https://dev.twitter.com/docs/api/1/post/account/update_profile_background_image
# @rate_limited No
# @requires_authentication Yes
# @param image [String] The background image for the profile. Must be a valid GIF, JPG, or PNG image of less than 800 kilobytes in size. Images with width larger than 2048 pixels will be scaled down.
# @param options [Hash] A customizable set of options.
# @option options [Boolean] :tile Whether or not to tile the background image. If set to true the background image will be displayed tiled. The image will not be tiled otherwise.
# @return [Twitter::User] The authenticated user.
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Update the authenticating user's profile background image
# Twitter.update_profile_background_image(File.new("we_concept_bg2.png"))
def update_profile_background_image(image, options={})
user = post("/1/account/update_profile_background_image.json", options.merge(:image => image))
Twitter::User.new(user)
end
# Sets one or more hex values that control the color scheme of the authenticating user's profile
#
# @see https://dev.twitter.com/docs/api/1/post/account/update_profile_colors
# @rate_limited No
# @requires_authentication Yes
# @param options [Hash] A customizable set of options.
# @option options [String] :profile_background_color Profile background color.
# @option options [String] :profile_text_color Profile text color.
# @option options [String] :profile_link_color Profile link color.
# @option options [String] :profile_sidebar_fill_color Profile sidebar's background color.
# @option options [String] :profile_sidebar_border_color Profile sidebar's border color.
# @return [Twitter::User] The authenticated user.
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Set authenticating user's profile background to black
# Twitter.update_profile_colors(:profile_background_color => '000000')
def update_profile_colors(options={})
user = post("/1/account/update_profile_colors.json", options)
Twitter::User.new(user)
end
# Updates the authenticating user's profile image
#
# @see https://dev.twitter.com/docs/api/1/post/account/update_profile_image
# @note This method asynchronously processes the uploaded file before updating the user's profile image URL. You can either update your local cache the next time you request the user's information, or, at least 5 seconds after uploading the image, ask for the updated URL using {Twitter::Client::User#profile_image}.
# @rate_limited No
# @requires_authentication Yes
# @param image [String] The avatar image for the profile. Must be a valid GIF, JPG, or PNG image of less than 700 kilobytes in size. Images with width larger than 500 pixels will be scaled down. Animated GIFs will be converted to a static GIF of the first frame, removing the animation.
# @param options [Hash] A customizable set of options.
# @return [Twitter::User] The authenticated user.
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Update the authenticating user's profile image
# Twitter.update_profile_image(File.new("me.jpeg"))
def update_profile_image(image, options={})
user = post("/1/account/update_profile_image.json", options.merge(:image => image))
Twitter::User.new(user)
end
# Updates the authenticating user's settings.
# Or, if no options supplied, returns settings (including current trend, geo and sleep time information) for the authenticating user.
#
# @see https://dev.twitter.com/docs/api/1/post/account/settings
# @see https://dev.twitter.com/docs/api/1/get/account/settings
# @rate_limited Yes
# @requires_authentication Yes
# @param options [Hash] A customizable set of options.
# @option options [Integer] :trend_location_woeid The Yahoo! Where On Earth ID to use as the user's default trend location. Global information is available by using 1 as the WOEID. The woeid must be one of the locations returned by {https://dev.twitter.com/docs/api/1/get/trends/available GET trends/available}.
# @option options [Boolean, String, Integer] :sleep_time_enabled When set to true, 't' or 1, will enable sleep time for the user. Sleep time is the time when push or SMS notifications should not be sent to the user.
# @option options [Integer] :start_sleep_time The hour that sleep time should begin if it is enabled. The value for this parameter should be provided in {http://en.wikipedia.org/wiki/ISO_8601 ISO8601} format (i.e. 00-23). The time is considered to be in the same timezone as the user's time_zone setting.
# @option options [Integer] :end_sleep_time The hour that sleep time should end if it is enabled. The value for this parameter should be provided in {http://en.wikipedia.org/wiki/ISO_8601 ISO8601} format (i.e. 00-23). The time is considered to be in the same timezone as the user's time_zone setting.
# @option options [String] :time_zone The timezone dates and times should be displayed in for the user. The timezone must be one of the {http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html Rails TimeZone} names.
# @option options [String] :lang The language which Twitter should render in for this user. The language must be specified by the appropriate two letter ISO 639-1 representation. Currently supported languages are provided by {https://dev.twitter.com/docs/api/1/get/help/languages GET help/languages}.
# @return [Twitter::Settings]
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Return the settings for the authenticating user.
# Twitter.settings
def settings(options={})
settings = if options.size.zero?
get("/1/account/settings.json", options)
else
post("/1/account/settings.json", options)
end
Twitter::Settings.new(settings)
end
# Returns activity about me
#
# @note Undocumented
# @rate_limited Yes
# @requires_authentication Yes
# @param options [Hash] A customizable set of options.
# @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 100.
# @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
# @return [Array] An array of actions
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Return activity about me
# Twitter.activity_about_me
def activity_about_me(options={})
get("/i/activity/about_me.json", options).map do |action|
Twitter::ActionFactory.new(action)
end
end
# Returns activity by friends
#
# @note Undocumented
# @rate_limited Yes
# @requires_authentication Yes
# @param options [Hash] A customizable set of options.
# @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 100.
# @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
# @return [Array] An array of actions
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid./
# @example Return activity by friends
# Twitter.activity_by_friends
def activity_by_friends(options={})
get("/i/activity/by_friends.json", options).map do |action|
Twitter::ActionFactory.new(action)
end
end
# Returns an array of user objects that the authenticating user is blocking
#
# @see https://dev.twitter.com/docs/api/1/get/blocks/blocking
# @rate_limited Yes
# @requires_authentication Yes
# @param options [Hash] A customizable set of options.
# @option options [Integer] :page Specifies the page of results to retrieve.
# @return [Array<Twitter::User>] User objects that the authenticating user is blocking.
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Return an array of user objects that the authenticating user is blocking
# Twitter.blocking
def blocking(options={})
get("/1/blocks/blocking.json", options).map do |user|
Twitter::User.new(user)
end
end
# Returns an array of numeric user ids the authenticating user is blocking
#
# @see https://dev.twitter.com/docs/api/1/get/blocks/blocking/ids
# @rate_limited Yes
# @requires_authentication Yes
# @param options [Hash] A customizable set of options.
# @return [Array] Numeric user ids the authenticating user is blocking.
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Return an array of numeric user ids the authenticating user is blocking
# Twitter.blocking_ids
def blocked_ids(options={})
get("/1/blocks/blocking/ids.json", options)
end
# Returns true if the authenticating user is blocking a target user
#
# @see https://dev.twitter.com/docs/api/1/get/blocks/exists
# @requires_authentication Yes
# @rate_limited Yes
# @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object.
# @param options [Hash] A customizable set of options.
# @return [Boolean] true if the authenticating user is blocking the target user, otherwise false.
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Check whether the authenticating user is blocking @sferik
# Twitter.block?('sferik')
# Twitter.block?(7505382) # Same as above
def block?(user, options={})
options.merge_user!(user)
get("/1/blocks/exists.json", options, :raw => true)
true
rescue Twitter::Error::NotFound
false
end
# Blocks the user specified by the authenticating user
#
# @see https://dev.twitter.com/docs/api/1/post/blocks/create
# @note Destroys a friendship to the blocked user if it exists.
# @rate_limited Yes
# @requires_authentication Yes
# @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object.
# @param options [Hash] A customizable set of options.
# @return [Twitter::User] The blocked user.
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Block and unfriend @sferik as the authenticating user
# Twitter.block('sferik')
# Twitter.block(7505382) # Same as above
def block(user, options={})
options.merge_user!(user)
user = post("/1/blocks/create.json", options)
Twitter::User.new(user)
end
# Un-blocks the user specified by the authenticating user
#
# @see https://dev.twitter.com/docs/api/1/post/blocks/destroy
# @rate_limited No
# @requires_authentication Yes
# @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object.
# @param options [Hash] A customizable set of options.
# @return [Twitter::User] The un-blocked user.
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Un-block @sferik as the authenticating user
# Twitter.unblock('sferik')
# Twitter.unblock(7505382) # Same as above
def unblock(user, options={})
options.merge_user!(user)
user = delete("/1/blocks/destroy.json", options)
Twitter::User.new(user)
end
# Returns the 20 most recent direct messages sent to the authenticating user
#
# @see https://dev.twitter.com/docs/api/1/get/direct_messages
# @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.
# @rate_limited Yes
# @requires_authentication Yes
# @param options [Hash] A customizable set of options.
# @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
# @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
# @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200.
# @option options [Integer] :page Specifies the page of results to retrieve.
# @return [Array<Twitter::DirectMessage>] Direct messages sent to the authenticating user.
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Return the 20 most recent direct messages sent to the authenticating user
# Twitter.direct_messages
def direct_messages(options={})
get("/1/direct_messages.json", options).map do |direct_message|
Twitter::DirectMessage.new(direct_message)
end
end
# Returns the 20 most recent direct messages sent by the authenticating user
#
# @see https://dev.twitter.com/docs/api/1/get/direct_messages/sent
# @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.
# @rate_limited Yes
# @requires_authentication Yes
# @param options [Hash] A customizable set of options.
# @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
# @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
# @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200.
# @option options [Integer] :page Specifies the page of results to retrieve.
# @return [Array<Twitter::DirectMessage>] Direct messages sent by the authenticating user.
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Return the 20 most recent direct messages sent by the authenticating user
# Twitter.direct_messages_sent
def direct_messages_sent(options={})
get("/1/direct_messages/sent.json", options).map do |direct_message|
Twitter::DirectMessage.new(direct_message)
end
end
# Destroys a direct message
#
# @see https://dev.twitter.com/docs/api/1/post/direct_messages/destroy/:id
# @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.
# @rate_limited No
# @requires_authentication Yes
# @param id [Integer] The ID of the direct message to delete.
# @param options [Hash] A customizable set of options.
# @return [Twitter::DirectMessage] The deleted message.
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Destroys the direct message with the ID 1825785544
# Twitter.direct_message_destroy(1825785544)
def direct_message_destroy(id, options={})
direct_message = delete("/1/direct_messages/destroy/#{id}.json", options)
Twitter::DirectMessage.new(direct_message)
end
# Sends a new direct message to the specified user from the authenticating user
#
# @see https://dev.twitter.com/docs/api/1/post/direct_messages/new
# @rate_limited No
# @requires_authentication Yes
# @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object.
# @param text [String] The text of your direct message, up to 140 characters.
# @param options [Hash] A customizable set of options.
# @return [Twitter::DirectMessage] The sent message.
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Send a direct message to @sferik from the authenticating user
# Twitter.direct_message_create('sferik', "I'm sending you this message via @gem!")
# Twitter.direct_message_create(7505382, "I'm sending you this message via @gem!") # Same as above
def direct_message_create(user, text, options={})
options.merge_user!(user)
direct_message = post("/1/direct_messages/new.json", options.merge(:text => text))
Twitter::DirectMessage.new(direct_message)
end
alias :d :direct_message_create
# Returns a single direct message, specified by id.
#
# @see https://dev.twitter.com/docs/api/1/get/direct_messages/show/%3Aid
# @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.
# @rate_limited Yes
# @requires_authentication Yes
# @param id [Integer] The ID of the direct message to retrieve.
# @param options [Hash] A customizable set of options.
# @return [Twitter::DirectMessage] The requested message.
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Return the direct message with the id 1825786345
# Twitter.direct_message(1825786345)
def direct_message(id, options={})
direct_message = get("/1/direct_messages/show/#{id}.json", options)
Twitter::DirectMessage.new(direct_message)
end
# @see https://dev.twitter.com/docs/api/1/get/favorites
# @rate_limited Yes
# @requires_authentication No
# @overload favorites(options={})
# Returns the 20 most recent favorite statuses for the authenticating user
#
# @param options [Hash] A customizable set of options.
# @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 100.
# @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
# @return [Array<Twitter::Status>] favorite statuses.
# @example Return the 20 most recent favorite statuses for the authenticating user
# Twitter.favorites
# @overload favorites(user, options={})
# Returns the 20 most recent favorite statuses for the specified user
#
# @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object.
# @param options [Hash] A customizable set of options.
# @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 100.
# @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
# @return [Array<Twitter::Status>] favorite statuses.
# @example Return the 20 most recent favorite statuses for @sferik
# Twitter.favorites('sferik')
def favorites(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
if user = args.pop
get("/1/favorites/#{user}.json", options)
else
get("/1/favorites.json", options)
end.map do |status|
Twitter::Status.new(status)
end
end
# Favorites the specified status as the authenticating user
#
# @see https://dev.twitter.com/docs/api/1/post/favorites/create/:id
# @rate_limited No
# @requires_authentication Yes
# @param id [Integer] The numerical ID of the desired status.
# @param options [Hash] A customizable set of options.
# @return [Twitter::Status] The favorited status.
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Favorite the status with the ID 25938088801
# Twitter.favorite(25938088801)
def favorite(id, options={})
status = post("/1/favorites/create/#{id}.json", options)
Twitter::Status.new(status)
end
alias :favorite_create :favorite
# Un-favorites the specified status as the authenticating user
#
# @see https://dev.twitter.com/docs/api/1/post/favorites/destroy/:id
# @rate_limited No
# @requires_authentication Yes
# @param id [Integer] The numerical ID of the desired status.
# @param options [Hash] A customizable set of options.
# @return [Twitter::Status] The un-favorited status.
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Un-favorite the status with the ID 25938088801
# Twitter.unfavorite(25938088801)
def unfavorite(id, options={})
status = delete("/1/favorites/destroy/#{id}.json", options)
Twitter::Status.new(status)
end
alias :favorite_destroy :unfavorite
# @see https://dev.twitter.com/docs/api/1/get/followers/ids
# @rate_limited Yes
# @requires_authentication No unless requesting it from a protected user
#
# If getting this data of a protected user, you must authenticate (and be allowed to see that user).
# @overload follower_ids(options={})
# Returns an array of numeric IDs for every user following the authenticated user
#
# @param options [Hash] A customizable set of options.
# @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list.
# @return [Twitter::Cursor]
# @example Return the authenticated user's followers' IDs
# Twitter.follower_ids
# @overload follower_ids(user, options={})
# Returns an array of numeric IDs for every user following the specified user
#
# @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object.
# @param options [Hash] A customizable set of options.
# @option options [Integer] :cursor (-1) Breaks the results into pages. This is recommended for users who are following many users. Provide a value of -1 to begin paging. Provide values as returned in the response body's next_cursor and previous_cursor attributes to page back and forth in the list.
# @return [Twitter::Cursor]
# @example Return @sferik's followers' IDs
# Twitter.follower_ids('sferik')
# Twitter.follower_ids(7505382) # Same as above
def follower_ids(*args)
options = {:cursor => -1}
options.merge!(args.last.is_a?(Hash) ? args.pop : {})
user = args.pop
options.merge_user!(user)
cursor = get("/1/followers/ids.json", options)
Twitter::Cursor.new(cursor, 'ids')
end
# @see https://dev.twitter.com/docs/api/1/get/friends/ids
# @rate_limited Yes
# @requires_authentication No unless requesting it from a protected user
#
# If getting this data of a protected user, you must authenticate (and be allowed to see that user).
# @overload friend_ids(options={})
# Returns an array of numeric IDs for every user the authenticated user is following
#
# @param options [Hash] A customizable set of options.
# @option options [Integer] :cursor (-1) Breaks the results into pages. This is recommended for users who are following many users. Provide a value of -1 to begin paging. Provide values as returned in the response body's next_cursor and previous_cursor attributes to page back and forth in the list.
# @return [Twitter::Cursor]
# @example Return the authenticated user's friends' IDs
# Twitter.friend_ids
# @overload friend_ids(user, options={})
# Returns an array of numeric IDs for every user the specified user is following
#
# @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object.
# @param options [Hash] A customizable set of options.
# @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list.
# @return [Twitter::Cursor]
# @example Return @sferik's friends' IDs
# Twitter.friend_ids('sferik')
# Twitter.friend_ids(7505382) # Same as above
def friend_ids(*args)
options = {:cursor => -1}
options.merge!(args.last.is_a?(Hash) ? args.pop : {})
user = args.pop
options.merge_user!(user)
cursor = get("/1/friends/ids.json", options)
Twitter::Cursor.new(cursor, 'ids')
end
# Test for the existence of friendship between two users
#
# @see https://dev.twitter.com/docs/api/1/get/friendships/exists
# @note Consider using {Twitter::Client::FriendsAndFollowers#friendship} instead of this method.
# @rate_limited Yes
# @requires_authentication No unless user_a or user_b is protected
# @param user_a [Integer, String, Twitter::User] The Twitter user ID, screen name, or object of the subject user.
# @param user_b [Integer, String, Twitter::User] The Twitter user ID, screen name, or object of the user to test for following.
# @param options [Hash] A customizable set of options.
# @return [Boolean] true if user_a follows user_b, otherwise false.
# @example Return true if @sferik follows @pengwynn
# Twitter.friendship?('sferik', 'pengwynn')
# Twitter.friendship?('sferik', 14100886) # Same as above
# Twitter.friendship?(7505382, 14100886) # Same as above
def friendship?(user_a, user_b, options={})
options.merge_user!(user_a, nil, "a")
options.merge_user!(user_b, nil, "b")
get("/1/friendships/exists.json", options)
end
# Returns an array of numeric IDs for every user who has a pending request to follow the authenticating user
#
# @see https://dev.twitter.com/docs/api/1/get/friendships/incoming
# @rate_limited Yes
# @requires_authentication Yes
# @param options [Hash] A customizable set of options.
# @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list.
# @return [Twitter::Cursor]
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Return an array of numeric IDs for every user who has a pending request to follow the authenticating user
# Twitter.friendships_incoming
def friendships_incoming(options={})
options = {:cursor => -1}.merge(options)
cursor = get("/1/friendships/incoming.json", options)
Twitter::Cursor.new(cursor, 'ids')
end
# Returns an array of numeric IDs for every protected user for whom the authenticating user has a pending follow request
#
# @see https://dev.twitter.com/docs/api/1/get/friendships/outgoing
# @rate_limited Yes
# @requires_authentication Yes
# @param options [Hash] A customizable set of options.
# @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list.
# @return [Twitter::Cursor]
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Return an array of numeric IDs for every protected user for whom the authenticating user has a pending follow request
# Twitter.friendships_outgoing
def friendships_outgoing(options={})
options = {:cursor => -1}.merge(options)
cursor = get("/1/friendships/outgoing.json", options)
Twitter::Cursor.new(cursor, 'ids')
end
# Returns detailed information about the relationship between two users
#
# @see https://dev.twitter.com/docs/api/1/get/friendships/show
# @rate_limited Yes
# @requires_authentication No
# @param source [Integer, String, Twitter::User] The Twitter user ID, screen name, or object of the source user.
# @param target [Integer, String, Twitter::User] The Twitter user ID, screen name, or object of the target user.
# @param options [Hash] A customizable set of options.
# @return [Twitter::Relationship]
# @example Return the relationship between @sferik and @pengwynn
# Twitter.friendship('sferik', 'pengwynn')
# Twitter.friendship('sferik', 14100886) # Same as above
# Twitter.friendship(7505382, 14100886) # Same as above
def friendship(source, target, options={})
options.merge_user!(source, "source")
options[:source_id] = options.delete(:source_user_id) unless options[:source_user_id].nil?
options.merge_user!(target, "target")
options[:target_id] = options.delete(:target_user_id) unless options[:target_user_id].nil?
relationship = get("/1/friendships/show.json", options)['relationship']
Twitter::Relationship.new(relationship)
end
alias :friendship_show :friendship
alias :relationship :friendship
# Allows the authenticating user to follow the specified users, unless they are already followed
#
# @see https://dev.twitter.com/docs/api/1/post/friendships/create
# @rate_limited No
# @requires_authentication Yes
# @param users [Array<Integer, String, Twitter::User>, Set<Integer, String, Twitter::User>] An array of Twitter user IDs, screen names, or objects.
# @param options [Hash] A customizable set of options.
# @option options [Boolean] :follow (false) Enable notifications for the target user.
# @return [Twitter::User] The followed user.
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Follow @sferik
# Twitter.follow('sferik')
def follow(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
# Twitter always turns on notifications if the "follow" option is present, even if it's set to false
# so only send follow if it's true
options.merge!(:follow => true) if options.delete(:follow)
friend_ids = self.friend_ids.ids
user_ids = self.users(args).map(&:id)
(user_ids - friend_ids).threaded_map do |user|
begin
user = post("/1/friendships/create.json", options.merge_user(user))
Twitter::User.new(user)
rescue Twitter::Error::Forbidden
# This error will be raised if the user doesn't have permission to
# follow list_member, for whatever reason.
end
end.compact
end
alias :friendship_create :follow
# Allows the authenticating user to follow the specified users
#
# @see https://dev.twitter.com/docs/api/1/post/friendships/create
# @rate_limited No
# @requires_authentication Yes
# @param users [Array<Integer, String, Twitter::User>, Set<Integer, String, Twitter::User>] An array of Twitter user IDs, screen names, or objects.
# @param options [Hash] A customizable set of options.
# @option options [Boolean] :follow (false) Enable notifications for the target user.
# @return [Array<Twitter::User>] The followed users.
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Follow @sferik
# Twitter.follow!('sferik')
def follow!(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
# Twitter always turns on notifications if the "follow" option is present, even if it's set to false
# so only send follow if it's true
options.merge!(:follow => true) if options.delete(:follow)
args.threaded_map do |user|
begin
user = post("/1/friendships/create.json", options.merge_user(user))
Twitter::User.new(user)
rescue Twitter::Error::Forbidden
# This error will be raised if the user doesn't have permission to
# follow list_member, for whatever reason.
end
end.compact
end
alias :friendship_create! :follow!
# Allows the authenticating user to unfollow the specified users
#
# @see https://dev.twitter.com/docs/api/1/post/friendships/destroy
# @rate_limited No
# @requires_authentication Yes
# @param users [Array<Integer, String, Twitter::User>, Set<Integer, String, Twitter::User>] An array of Twitter user IDs, screen names, or objects.
# @param options [Hash] A customizable set of options.
# @return [Array<Twitter::User>] The unfollowed users.
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Unfollow @sferik
# Twitter.unfollow('sferik')
def unfollow(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
args.threaded_map do |user|
user = delete("/1/friendships/destroy.json", options.merge_user(user))
Twitter::User.new(user)
end
end
alias :friendship_destroy :unfollow
# Returns the relationship of the authenticating user to the comma separated list of up to 100 screen_names or user_ids provided. Values for connections can be: following, following_requested, followed_by, none.
#
# @see https://dev.twitter.com/docs/api/1/get/friendships/lookup
# @rate_limited Yes
# @requires_authentication Yes
# @param options [Hash] A customizable set of options.
# @return [Twitter::Relationship]
# @overload friendships(*users, options={})
# @param users [Array<Integer, String, Twitter::User>, Set<Integer, String, Twitter::User>] An array of Twitter user IDs, screen names, or objects.
# @param options [Hash] A customizable set of options.
# @return [Array<Twitter::User>] The requested users.
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Return extended information for @sferik and @pengwynn
# Twitter.friendships('sferik', 'pengwynn')
# Twitter.friendships('sferik', 14100886) # Same as above
# Twitter.friendships(7505382, 14100886) # Same as above
def friendships(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
users = args
options.merge_users!(Array(users))
get("/1/friendships/lookup.json", options).map do |user|
Twitter::User.new(user)
end
end
# Allows one to enable or disable retweets and device notifications from the specified user.
#
# @see https://dev.twitter.com/docs/api/1/post/friendships/update
# @rate_limited No
# @requires_authentication Yes
# @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object.
# @param options [Hash] A customizable set of options.
# @option options [Boolean] :device Enable/disable device notifications from the target user.
# @option options [Boolean] :retweets Enable/disable retweets from the target user.
# @return [Twitter::Relationship]
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Enable rewteets and devise notifications for @sferik
# Twitter.friendship_update('sferik', :device => true, :retweets => true)
def friendship_update(user, options={})
options.merge_user!(user)
relationship = post("/1/friendships/update.json", options)['relationship']
Twitter::Relationship.new(relationship)
end
# Returns an array of user_ids that the currently authenticated user does not want to see retweets from.
#
# @see https://dev.twitter.com/docs/api/1/get/friendships/no_retweet_ids
# @rate_limited Yes
# @requires_authentication Yes
# @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object.
# @param options [Hash] A customizable set of options.
# @option options [Boolean] :stringify_ids Many programming environments will not consume our ids due to their size. Provide this option to have ids returned as strings instead. Read more about Twitter IDs, JSON and Snowflake.
# @return [Array<Integer>]
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Enable rewteets and devise notifications for @sferik
# Twitter.no_retweet_ids
def no_retweet_ids(options={})
get("/1/friendships/no_retweet_ids.json", options)
end
# Allows the authenticating user to accept the specified user's follow request
#
# @note Undocumented
# @rate_limited No
# @requires_authentication Yes
# @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object.
# @param options [Hash] A customizable set of options.
# @return [Twitter::User] The accepted user.
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Accept @sferik's follow request
# Twitter.accept('sferik')
def accept(user, options={})
options.merge_user!(user)
user = post("/1/friendships/accept.json", options)
Twitter::User.new(user)
end
# Allows the authenticating user to deny the specified user's follow request
#
# @note Undocumented
# @rate_limited No
# @requires_authentication Yes
# @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object.
# @param options [Hash] A customizable set of options.
# @return [Twitter::User] The denied user.
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Deny @sferik's follow request
# Twitter.deny('sferik')
def deny(user, options={})
options.merge_user!(user)
user = post("/1/friendships/deny.json", options)
Twitter::User.new(user)
end
# Returns the current configuration used by Twitter
#
# @see https://dev.twitter.com/docs/api/1/get/help/configuration
# @rate_limited Yes
# @requires_authentication No
# @return [Twitter::Configuration] Twitter's configuration.
# @example Return the current configuration used by Twitter
# Twitter.configuration
def configuration(options={})
configuration = get("/1/help/configuration.json", options)
Twitter::Configuration.new(configuration)
end
# Returns the list of languages supported by Twitter
#
# @see https://dev.twitter.com/docs/api/1/get/help/languages
# @rate_limited Yes
# @requires_authentication No
# @return [Array<Twitter::Language>]
# @example Return the list of languages Twitter supports
# Twitter.languages
def languages(options={})
get("/1/help/languages.json", options).map do |language|
Twitter::Language.new(language)
end
end
# Returns {https://twitter.com/privacy Twitter's Privacy Policy}
#
# @see https://dev.twitter.com/docs/api/1/get/legal/privacy
# @rate_limited Yes
# @requires_authentication No
# @return [String]
# @example Return {https://twitter.com/privacy Twitter's Privacy Policy}
# Twitter.privacy
def privacy(options={})
get("/1/legal/privacy.json", options)['privacy']
end
# Returns {https://twitter.com/tos Twitter's Terms of Service}
#
# @see https://dev.twitter.com/docs/api/1/get/legal/tos
# @rate_limited Yes
# @requires_authentication No
# @return [String]
# @example Return {https://twitter.com/tos Twitter's Terms of Service}
# Twitter.tos
def tos(options={})
get("/1/legal/tos.json", options)['tos']
end
# Returns all lists the authenticating or specified user subscribes to, including their own
#
# @see https://dev.twitter.com/docs/api/1/get/lists/all
# @rate_limited Yes
# @requires_authentication Supported
# @overload lists_subscribed_to(options={})
# @param options [Hash] A customizable set of options.
# @return [Array<Twitter::Status>]
# @example Return all lists the authenticating user subscribes to
# Twitter.lists_subscribed_to
# @overload lists_subscribed_to(user, options={})
# @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object.
# @param options [Hash] A customizable set of options.
# @return [Array<Twitter::List>]
# @example Return all lists the specified user subscribes to
# Twitter.lists_subscribed_to('sferik')
# Twitter.lists_subscribed_to(8863586)
def lists_subscribed_to(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
if user = args.pop
options.merge_user!(user)
end
get("/1/lists/all.json", options).map do |list|
Twitter::List.new(list)
end
end
# Show tweet timeline for members of the specified list
#
# @see https://dev.twitter.com/docs/api/1/get/lists/statuses
# @rate_limited Yes
# @requires_authentication No
# @overload list_timeline(list, options={})
# @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object.
# @param options [Hash] A customizable set of options.
# @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
# @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
# @option options [Integer] :per_page The number of results to retrieve.
# @return [Array<Twitter::Status>]
# @example Show tweet timeline for members of the authenticated user's "presidents" list
# Twitter.list_timeline('presidents')
# Twitter.list_timeline(8863586)
# @overload list_timeline(user, list, options={})
# @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object.
# @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object.
# @param options [Hash] A customizable set of options.
# @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
# @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
# @option options [Integer] :per_page The number of results to retrieve.
# @return [Array<Twitter::Status>]
# @example Show tweet timeline for members of @sferik's "presidents" list
# Twitter.list_timeline('sferik', 'presidents')
# Twitter.list_timeline('sferik', 8863586)
# Twitter.list_timeline(7505382, 'presidents')
# Twitter.list_timeline(7505382, 8863586)
def list_timeline(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
list = args.pop
options.merge_list!(list)
unless options[:owner_id] || options[:owner_screen_name]
owner = args.pop || self.current_user.screen_name
options.merge_owner!(owner)
end
get("/1/lists/statuses.json", options).map do |status|
Twitter::Status.new(status)
end
end
# Removes the specified member from the list
#
# @see https://dev.twitter.com/docs/api/1/post/lists/members/destroy
# @rate_limited No
# @requires_authentication Yes
# @overload list_remove_member(list, user_to_remove, options={})
# @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object.
# @param user_to_remove [Integer, String] The user id or screen name of the list member to remove.
# @param options [Hash] A customizable set of options.
# @return [Twitter::List] The list.
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Remove @BarackObama from the authenticated user's "presidents" list
# Twitter.list_remove_member('presidents', 813286)
# Twitter.list_remove_member('presidents', 'BarackObama')
# Twitter.list_remove_member(8863586, 'BarackObama')
# @overload list_remove_member(user, list, user_to_remove, options={})
# @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object.
# @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object.
# @param user_to_remove [Integer, String] The user id or screen name of the list member to remove.
# @param options [Hash] A customizable set of options.
# @return [Twitter::List] The list.
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Remove @BarackObama from @sferik's "presidents" list
# Twitter.list_remove_member('sferik', 'presidents', 813286)
# Twitter.list_remove_member('sferik', 'presidents', 'BarackObama')
# Twitter.list_remove_member('sferik', 8863586, 'BarackObama')
# Twitter.list_remove_member(7505382, 'presidents', 813286)
def list_remove_member(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
user_to_remove = args.pop
options.merge_user!(user_to_remove)
list = args.pop
options.merge_list!(list)
unless options[:owner_id] || options[:owner_screen_name]
owner = args.pop || self.current_user.screen_name
options.merge_owner!(owner)
end
list = post("/1/lists/members/destroy.json", options)
Twitter::List.new(list)
end
# List the lists the specified user has been added to
#
# @see https://dev.twitter.com/docs/api/1/get/lists/memberships
# @rate_limited Yes
# @requires_authentication Supported
# @overload memberships(options={})
# @param options [Hash] A customizable set of options.
# @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list.