instagram-fixed 0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. data/.gitignore +12 -0
  2. data/.rspec +3 -0
  3. data/.yardopts +9 -0
  4. data/Gemfile +3 -0
  5. data/LICENSE.md +20 -0
  6. data/README.md +145 -0
  7. data/Rakefile +27 -0
  8. data/instagram.gemspec +37 -0
  9. data/lib/faraday/oauth2.rb +36 -0
  10. data/lib/faraday/raise_http_4xx.rb +37 -0
  11. data/lib/faraday/raise_http_5xx.rb +29 -0
  12. data/lib/instagram.rb +26 -0
  13. data/lib/instagram/api.rb +23 -0
  14. data/lib/instagram/client.rb +20 -0
  15. data/lib/instagram/client/comments.rb +62 -0
  16. data/lib/instagram/client/geographies.rb +29 -0
  17. data/lib/instagram/client/likes.rb +58 -0
  18. data/lib/instagram/client/locations.rb +59 -0
  19. data/lib/instagram/client/media.rb +63 -0
  20. data/lib/instagram/client/subscriptions.rb +157 -0
  21. data/lib/instagram/client/tags.rb +59 -0
  22. data/lib/instagram/client/users.rb +309 -0
  23. data/lib/instagram/client/utils.rb +15 -0
  24. data/lib/instagram/configuration.rb +90 -0
  25. data/lib/instagram/connection.rb +31 -0
  26. data/lib/instagram/error.rb +19 -0
  27. data/lib/instagram/oauth.rb +27 -0
  28. data/lib/instagram/request.rb +45 -0
  29. data/lib/instagram/version.rb +3 -0
  30. data/spec/faraday/response_spec.rb +28 -0
  31. data/spec/fixtures/access_token.json +9 -0
  32. data/spec/fixtures/approve_user.json +8 -0
  33. data/spec/fixtures/block_user.json +8 -0
  34. data/spec/fixtures/deny_user.json +8 -0
  35. data/spec/fixtures/follow_user.json +8 -0
  36. data/spec/fixtures/followed_by.json +1 -0
  37. data/spec/fixtures/follows.json +1 -0
  38. data/spec/fixtures/geography_recent_media.json +1 -0
  39. data/spec/fixtures/liked_media.json +1 -0
  40. data/spec/fixtures/location.json +1 -0
  41. data/spec/fixtures/location_recent_media.json +1 -0
  42. data/spec/fixtures/location_search.json +1 -0
  43. data/spec/fixtures/media.json +1 -0
  44. data/spec/fixtures/media_comment.json +1 -0
  45. data/spec/fixtures/media_comment_deleted.json +1 -0
  46. data/spec/fixtures/media_comments.json +1 -0
  47. data/spec/fixtures/media_liked.json +1 -0
  48. data/spec/fixtures/media_likes.json +1 -0
  49. data/spec/fixtures/media_popular.json +1 -0
  50. data/spec/fixtures/media_search.json +1 -0
  51. data/spec/fixtures/media_unliked.json +1 -0
  52. data/spec/fixtures/mikeyk.json +1 -0
  53. data/spec/fixtures/recent_media.json +1 -0
  54. data/spec/fixtures/relationship.json +9 -0
  55. data/spec/fixtures/requested_by.json +12 -0
  56. data/spec/fixtures/shayne.json +1 -0
  57. data/spec/fixtures/subscription.json +12 -0
  58. data/spec/fixtures/subscription_deleted.json +1 -0
  59. data/spec/fixtures/subscription_payload.json +14 -0
  60. data/spec/fixtures/subscriptions.json +22 -0
  61. data/spec/fixtures/tag.json +1 -0
  62. data/spec/fixtures/tag_recent_media.json +1 -0
  63. data/spec/fixtures/tag_search.json +1 -0
  64. data/spec/fixtures/unblock_user.json +8 -0
  65. data/spec/fixtures/unfollow_user.json +8 -0
  66. data/spec/fixtures/user_media_feed.json +1 -0
  67. data/spec/fixtures/user_search.json +1 -0
  68. data/spec/instagram/api_spec.rb +110 -0
  69. data/spec/instagram/client/comments_spec.rb +71 -0
  70. data/spec/instagram/client/geography_spec.rb +37 -0
  71. data/spec/instagram/client/likes_spec.rb +66 -0
  72. data/spec/instagram/client/locations_spec.rb +78 -0
  73. data/spec/instagram/client/media_spec.rb +78 -0
  74. data/spec/instagram/client/subscriptions_spec.rb +148 -0
  75. data/spec/instagram/client/tags_spec.rb +78 -0
  76. data/spec/instagram/client/users_spec.rb +400 -0
  77. data/spec/instagram/client_spec.rb +23 -0
  78. data/spec/instagram_spec.rb +97 -0
  79. data/spec/spec_helper.rb +59 -0
  80. metadata +253 -0
@@ -0,0 +1,309 @@
1
+ module Instagram
2
+ class Client
3
+ # Defines methods related to users
4
+ module Users
5
+ # Returns extended information of a given user
6
+ #
7
+ # @overload user(id=nil, options={})
8
+ # @param user [Integer] An Instagram user ID
9
+ # @return [Hashie::Mash] The requested user.
10
+ # @example Return extended information for @shayne
11
+ # Instagram.user(20)
12
+ # @format :json
13
+ # @authenticated false unless requesting it from a protected user
14
+ #
15
+ # If getting this data of a protected user, you must authenticate (and be allowed to see that user).
16
+ # @rate_limited true
17
+ # @see TODO:docs url
18
+ def user(*args)
19
+ id = args.first || 'self'
20
+ response = get("users/#{id}")
21
+ response["data"]
22
+ end
23
+
24
+ # Returns users that match the given query
25
+ #
26
+ # @format :json
27
+ # @authenticated false
28
+ # @rate_limited true
29
+ # @param query [String] The search query to run against user search.
30
+ # @param options [Hash] A customizable set of options.
31
+ # @option options [Integer] :count The number of users to retrieve.
32
+ # @return [Array]
33
+ # @see TODO:doc url
34
+ # @example Return users that match "Shayne Sweeney"
35
+ # Instagram.user_search("Shayne Sweeney")
36
+ def user_search(query, options={})
37
+ response = get('users/search', options.merge(:q => query))
38
+ response["data"]
39
+ end
40
+
41
+ # Returns a list of users whom a given user follows
42
+ #
43
+ # @overload follows(id=nil, options={})
44
+ # @param options [Hash] A customizable set of options.
45
+ # @return [Hashie::Mash]
46
+ # @example Returns a list of users the authenticated user follows
47
+ # Instagram.user_follows
48
+ # @overload follows(id=nil, options={})
49
+ # @param user [Integer] An Instagram user ID.
50
+ # @param options [Hash] A customizable set of options.
51
+ # @option options [Integer] :cursor (nil) Breaks the results into pages. Provide values as returned in the response objects's next_cursor attribute to page forward in the list.
52
+ # @option options [Integer] :count (nil) Limits the number of results returned per page.
53
+ # @return [Hashie::Mash]
54
+ # @example Return a list of users @mikeyk follows
55
+ # Instagram.user_follows(4) # @mikeyk user ID being 4
56
+ # @see TODO:docs url
57
+ # @format :json
58
+ # @authenticated false unless requesting it from a protected user
59
+ #
60
+ # If getting this data of a protected user, you must authenticate (and be allowed to see that user).
61
+ # @rate_limited true
62
+ def user_follows(*args)
63
+ options = args.last.is_a?(Hash) ? args.pop : {}
64
+ id = args.first || "self"
65
+ response = get("users/#{id}/follows", options)
66
+ response["data"]
67
+ end
68
+ end
69
+
70
+ # Returns a list of users whom a given user is followed by
71
+ #
72
+ # @overload user_followed_by(id=nil, options={})
73
+ # @param options [Hash] A customizable set of options.
74
+ # @return [Hashie::Mash]
75
+ # @example Returns a list of users the authenticated user is followed by
76
+ # Instagram.user_followed_by
77
+ # @overload user_followed_by(id=nil, options={})
78
+ # @param user [Integer] An Instagram user ID.
79
+ # @param options [Hash] A customizable set of options.
80
+ # @option options [Integer] :cursor (nil) Breaks the results into pages. Provide values as returned in the response objects's next_cursor attribute to page forward in the list.
81
+ # @option options [Integer] :count (nil) Limits the number of results returned per page.
82
+ # @return [Hashie::Mash]
83
+ # @example Return a list of users @mikeyk is followed by
84
+ # Instagram.user_followed_by(4) # @mikeyk user ID being 4
85
+ # @see TODO:docs url
86
+ # @format :json
87
+ # @authenticated false unless requesting it from a protected user
88
+ #
89
+ # If getting this data of a protected user, you must authenticate (and be allowed to see that user).
90
+ # @rate_limited true
91
+ def user_followed_by(*args)
92
+ options = args.last.is_a?(Hash) ? args.pop : {}
93
+ id = args.first || "self"
94
+ response = get("users/#{id}/followed-by", options)
95
+ response["data"]
96
+ end
97
+
98
+ # Returns a list of users whom a given user is followed by
99
+ #
100
+ # @overload user_requested_by()
101
+ # @param options [Hash] A customizable set of options.
102
+ # @return [Hashie::Mash]
103
+ # @example Returns a list of users awaiting approval of a ollow request, for the authenticated user
104
+ # Instagram.user_requested_by
105
+ # @overload user_requested_by()
106
+ # @return [Hashie::Mash]
107
+ # @example Return a list of users who have requested to follow the authenticated user
108
+ # Instagram.user_requested_by()
109
+ # @see TODO:docs url
110
+ # @format :json
111
+ # @authenticated truei
112
+ # @rate_limited true
113
+ def user_requested_by()
114
+ response = get("users/self/requested-by")
115
+ response["data"]
116
+ end
117
+
118
+ # Returns most recent media items from the currently authorized user's feed.
119
+ #
120
+ # @overload user_media_feed(options={})
121
+ # @param options [Hash] A customizable set of options.
122
+ # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
123
+ # @option options [Integer] :min_id Return media later than this min_id
124
+ # @option options [Integer] :count Specifies the number of records to retrieve, per page.
125
+ # @return [Array]
126
+ # @example Return most recent media images that would appear on @shayne's feed
127
+ # Instagram.user_media_feed() # assuming @shayne is the authorized user
128
+ # @format :json
129
+ # @authenticated true
130
+ # @rate_limited true
131
+ # @see http://instagram.com/developer/endpoints/users/#get_users_feed
132
+ def user_media_feed(*args)
133
+ options = args.first.is_a?(Hash) ? args.pop : {}
134
+ response = get('users/self/feed', options)
135
+ response
136
+ end
137
+
138
+ # Returns a list of recent media items for a given user
139
+ #
140
+ # @overload user_recent_media(options={})
141
+ # @param options [Hash] A customizable set of options.
142
+ # @return [Hashie::Mash]
143
+ # @example Returns a list of recent media items for the currently authenticated user
144
+ # Instagram.user_recent_media
145
+ # @overload user_recent_media(id=nil, options={})
146
+ # @param user [Integer] An Instagram user ID.
147
+ # @param options [Hash] A customizable set of options.
148
+ # @option options [Integer] :max_id (nil) Returns results with an ID less than (that is, older than) or equal to the specified ID.
149
+ # @option options [Integer] :count (nil) Limits the number of results returned per page.
150
+ # @return [Hashie::Mash]
151
+ # @example Return a list of media items taken by @mikeyk
152
+ # Instagram.user_recent_media(4) # @mikeyk user ID being 4
153
+ # @see http://instagram.com/developer/endpoints/users/#get_users_media_recent
154
+ # @format :json
155
+ # @authenticated false unless requesting it from a protected user
156
+ #
157
+ # If getting this data of a protected user, you must authenticate (and be allowed to see that user).
158
+ # @rate_limited true
159
+ def user_recent_media(*args)
160
+ options = args.last.is_a?(Hash) ? args.pop : {}
161
+ id = args.first || "self"
162
+ response = get("users/#{id}/media/recent", options)
163
+ response["data"]
164
+ end
165
+
166
+ # Returns a list of media items liked by the current user
167
+ #
168
+ # @overload user_liked_media(options={})
169
+ # @param options [Hash] A customizable set of options.
170
+ # @option options [Integer] :max_like_id (nil) Returns results with an ID less than (that is, older than) or equal to the specified ID.
171
+ # @option options [Integer] :count (nil) Limits the number of results returned per page.
172
+ # @return [Hashie::Mash]
173
+ # @example Returns a list of media items liked by the currently authenticated user
174
+ # Instagram.user_liked_media
175
+ # @see http://instagram.com/developer/endpoints/users/#get_users_liked_feed
176
+ # @format :json
177
+ # @authenticated true
178
+ # @rate_limited true
179
+ def user_liked_media(options={})
180
+ response = get("users/self/media/liked", options)
181
+ response
182
+ end
183
+
184
+ # Returns information about the current user's relationship (follow/following/etc) to another user
185
+ #
186
+ # @overload user_relationship(id, options={})
187
+ # @param user [Integer] An Instagram user ID.
188
+ # @param options [Hash] An optional options hash
189
+ # @return [Hashie::Mash]
190
+ # @example Return the relationship status between the currently authenticated user and @mikeyk
191
+ # Instagram.user_relationship(4) # @mikeyk user ID being 4
192
+ # @see http://instagram.com/developer/endpoints/relationships/
193
+ # @format :json
194
+ # @authenticated true
195
+ # @rate_limited true
196
+ def user_relationship(id, options={})
197
+ response = get("users/#{id}/relationship", options)
198
+ response["data"]
199
+ end
200
+
201
+ # Create a follows relationship between the current user and the target user
202
+ #
203
+ # @overload follow_user(id, options={})
204
+ # @param user [Integer] An Instagram user ID.
205
+ # @param options [Hash] An optional options hash
206
+ # @return [Hashie::Mash]
207
+ # @example Request the current user to follow the target user
208
+ # Instagram.follow_user(4)
209
+ # @see http://instagram.com/developer/endpoints/relationships/
210
+ # @format :json
211
+ # @authenticated true
212
+ # @rate_limited true
213
+ def follow_user(id, options={})
214
+ options["action"] = "follow"
215
+ response = post("users/#{id}/relationship", options)
216
+ response["data"]
217
+ end
218
+
219
+ # Destroy a follows relationship between the current user and the target user
220
+ #
221
+ # @overload unfollow_user(id, options={})
222
+ # @param user [Integer] An Instagram user ID.
223
+ # @param options [Hash] An optional options hash
224
+ # @return [Hashie::Mash]
225
+ # @example Remove a follows relationship between the current user and the target user
226
+ # Instagram.unfollow_user(4)
227
+ # @see http://instagram.com/developer/endpoints/relationships/
228
+ # @format :json
229
+ # @authenticated true
230
+ # @rate_limited true
231
+ def unfollow_user(id, options={})
232
+ options["action"] = "unfollow"
233
+ response = post("users/#{id}/relationship", options)
234
+ response["data"]
235
+ end
236
+
237
+ # Block a relationship between the current user and the target user
238
+ #
239
+ # @overload unfollow_user(id, options={})
240
+ # @param user [Integer] An Instagram user ID.
241
+ # @param options [Hash] An optional options hash
242
+ # @return [Hashie::Mash]
243
+ # @example Block a relationship between the current user and the target user
244
+ # Instagram.block_user(4)
245
+ # @see http://instagram.com/developer/endpoints/relationships/
246
+ # @format :json
247
+ # @authenticated true
248
+ # @rate_limited true
249
+ def block_user(id, options={})
250
+ options["action"] = "block"
251
+ response = post("users/#{id}/relationship", options)
252
+ response["data"]
253
+ end
254
+
255
+ # Remove a relationship block between the current user and the target user
256
+ #
257
+ # @overload unblock_user(id, options={})
258
+ # @param user [Integer] An Instagram user ID.
259
+ # @param options [Hash] An optional options hash
260
+ # @return [Hashie::Mash]
261
+ # @example Remove a relationship block between the current user and the target user
262
+ # Instagram.unblock_user(4)
263
+ # @see http://instagram.com/developer/endpoints/relationships/
264
+ # @format :json
265
+ # @authenticated true
266
+ # @rate_limited true
267
+ def unblock_user(id, options={})
268
+ options["action"] = "unblock"
269
+ response = post("users/#{id}/relationship", options)
270
+ response["data"]
271
+ end
272
+
273
+ # Approve a relationship request between the current user and the target user
274
+ #
275
+ # @overload approve_user(id, options={})
276
+ # @param user [Integer] An Instagram user ID.
277
+ # @param options [Hash] An optional options hash
278
+ # @return [Hashie::Mash]
279
+ # @example Approve a relationship request between the current user and the target user
280
+ # Instagram.approve_user(4)
281
+ # @see http://instagram.com/developer/endpoints/relationships/
282
+ # @format :json
283
+ # @authenticated true
284
+ # @rate_limited true
285
+ def approve_user(id, options={})
286
+ options["action"] = "approve"
287
+ response = post("users/#{id}/relationship", options)
288
+ response["data"]
289
+ end
290
+
291
+ # Deny a relationship request between the current user and the target user
292
+ #
293
+ # @overload deny_user(id, options={})
294
+ # @param user [Integer] An Instagram user ID.
295
+ # @param options [Hash] An optional options hash
296
+ # @return [Hashie::Mash]
297
+ # @example Deny a relationship request between the current user and the target user
298
+ # Instagram.deny_user(4)
299
+ # @see http://instagram.com/developer/endpoints/relationships/
300
+ # @format :json
301
+ # @authenticated true
302
+ # @rate_limited true
303
+ def deny_user(id, options={})
304
+ options["action"] = "deny"
305
+ response = post("users/#{id}/relationship", options)
306
+ response["data"]
307
+ end
308
+ end
309
+ end
@@ -0,0 +1,15 @@
1
+ module Instagram
2
+ class Client
3
+ # @private
4
+ module Utils
5
+ private
6
+
7
+ # Returns the configured user name or the user name of the authenticated user
8
+ #
9
+ # @return [String]
10
+ def get_username
11
+ @user_name ||= self.user.username
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,90 @@
1
+ require 'faraday'
2
+ require File.expand_path('../version', __FILE__)
3
+
4
+ module Instagram
5
+ # Defines constants and methods related to configuration
6
+ module Configuration
7
+ # An array of valid keys in the options hash when configuring a {Instagram::API}
8
+ VALID_OPTIONS_KEYS = [
9
+ :adapter,
10
+ :client_id,
11
+ :client_secret,
12
+ :access_token,
13
+ :endpoint,
14
+ :format,
15
+ :user_agent,
16
+ :proxy
17
+ ].freeze
18
+
19
+ # An array of valid request/response formats
20
+ #
21
+ # @note Not all methods support the XML format.
22
+ VALID_FORMATS = [
23
+ :json].freeze
24
+
25
+ # The adapter that will be used to connect if none is set
26
+ #
27
+ # @note The default faraday adapter is Net::HTTP.
28
+ DEFAULT_ADAPTER = Faraday.default_adapter
29
+
30
+ # By default, don't set an application ID
31
+ DEFAULT_CLIENT_ID = nil
32
+
33
+ # By default, don't set an application secret
34
+ DEFAULT_CLIENT_SECRET = nil
35
+
36
+ # By default, don't set an application redirect uri
37
+ DEFAULT_REDIRECT_URI = nil
38
+
39
+ # By default, don't set a user access token
40
+ DEFAULT_ACCESS_TOKEN = nil
41
+
42
+ # The endpoint that will be used to connect if none is set
43
+ #
44
+ # @note There is no reason to use any other endpoint at this time
45
+ DEFAULT_ENDPOINT = 'https://api.instagram.com/v1/'.freeze
46
+
47
+ # The response format appended to the path and sent in the 'Accept' header if none is set
48
+ #
49
+ # @note JSON is the only available format at this time
50
+ DEFAULT_FORMAT = :json
51
+
52
+ # By default, don't use a proxy server
53
+ DEFAULT_PROXY = nil
54
+
55
+ # The user agent that will be sent to the API endpoint if none is set
56
+ DEFAULT_USER_AGENT = "Instagram Ruby Gem #{Instagram::VERSION}".freeze
57
+
58
+ # @private
59
+ attr_accessor *VALID_OPTIONS_KEYS
60
+
61
+ # When this module is extended, set all configuration options to their default values
62
+ def self.extended(base)
63
+ base.reset
64
+ end
65
+
66
+ # Convenience method to allow configuration options to be set in a block
67
+ def configure
68
+ yield self
69
+ end
70
+
71
+ # Create a hash of options and their values
72
+ def options
73
+ VALID_OPTIONS_KEYS.inject({}) do |option, key|
74
+ option.merge!(key => send(key))
75
+ end
76
+ end
77
+
78
+ # Reset all configuration options to defaults
79
+ def reset
80
+ self.adapter = DEFAULT_ADAPTER
81
+ self.client_id = DEFAULT_CLIENT_ID
82
+ self.client_secret = DEFAULT_CLIENT_SECRET
83
+ self.access_token = DEFAULT_ACCESS_TOKEN
84
+ self.endpoint = DEFAULT_ENDPOINT
85
+ self.format = DEFAULT_FORMAT
86
+ self.user_agent = DEFAULT_USER_AGENT
87
+ self.proxy = DEFAULT_PROXY
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,31 @@
1
+ require 'faraday_middleware'
2
+ Dir[File.expand_path('../../faraday/*.rb', __FILE__)].each{|f| require f}
3
+
4
+ module Instagram
5
+ # @private
6
+ module Connection
7
+ private
8
+
9
+ def connection(raw=false)
10
+ options = {
11
+ :headers => {'Accept' => "application/#{format}; charset=utf-8", 'User-Agent' => user_agent},
12
+ :proxy => proxy,
13
+ :ssl => {:verify => false},
14
+ :url => endpoint,
15
+ }
16
+
17
+ Faraday::Connection.new(options) do |connection|
18
+ connection.use Faraday::Request::OAuth2, client_id, access_token
19
+ connection.adapter(adapter)
20
+ connection.use Faraday::Response::RaiseHttp5xx
21
+ unless raw
22
+ case format.to_s.downcase
23
+ when 'json' then connection.use Faraday::Response::ParseJson
24
+ end
25
+ end
26
+ connection.use Faraday::Response::RaiseHttp4xx
27
+ connection.use Faraday::Response::Mashify unless raw
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,19 @@
1
+ module Instagram
2
+ # Custom error class for rescuing from all Instagram errors
3
+ class Error < StandardError; end
4
+
5
+ # Raised when Instagram returns the HTTP status code 400
6
+ class BadRequest < Error; end
7
+
8
+ # Raised when Instagram returns the HTTP status code 404
9
+ class NotFound < Error; end
10
+
11
+ # Raised when Instagram returns the HTTP status code 500
12
+ class InternalServerError < Error; end
13
+
14
+ # Raised when Instagram returns the HTTP status code 503
15
+ class ServiceUnavailable < Error; end
16
+
17
+ # Raised when a subscription payload hash is invalid
18
+ class InvalidSignature < Error; end
19
+ end