Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Create a Response object and use it to combine meta and pagination in…

…to the response

data instead of truncating it out. This broke the expectations in a variety of places,
but at least it brings the gem's method signature to be consistent with itself across
the board.
  • Loading branch information...
commit 2149bdf4312528cea864672d6f114dc19cccc3c0 1 parent 29dee1b
@yaauie yaauie authored yaauie committed
View
3  lib/instagram.rb
@@ -2,6 +2,7 @@
require File.expand_path('../instagram/configuration', __FILE__)
require File.expand_path('../instagram/api', __FILE__)
require File.expand_path('../instagram/client', __FILE__)
+require File.expand_path('../instagram/response', __FILE__)
module Instagram
extend Configuration
@@ -23,4 +24,4 @@ def self.method_missing(method, *args, &block)
def self.respond_to?(method)
return client.respond_to?(method) || super
end
-end
+end
View
8 lib/instagram/client/comments.rb
@@ -6,7 +6,7 @@ module Comments
#
# @overload media_comments(id)
# @param id [Integer] An Instagram media item ID
- # @return [Hashie::Mash] The requested comment.
+ # @return [Hashie::Mash] The requested comments.
# @example Returns a list of comments for the media item of ID 1234
# Instagram.media_comments(777)
# @format :json
@@ -17,7 +17,7 @@ module Comments
# @see TODO:docs url
def media_comments(id, *args)
response = get("media/#{id}/comments")
- response["data"]
+ response
end
# Creates a comment for a given media item ID
@@ -36,7 +36,7 @@ def media_comments(id, *args)
# @see TODO:docs url
def create_media_comment(id, text, options={})
response = post("media/#{id}/comments", options.merge(:text => text))
- response["data"]
+ response
end
# Deletes a comment for a given media item ID
@@ -55,7 +55,7 @@ def create_media_comment(id, text, options={})
# @see TODO:docs url
def delete_media_comment(media_id, comment_id, options={})
response = delete("media/#{media_id}/comments/#{comment_id}", options)
- response["data"]
+ response
end
end
end
View
4 lib/instagram/client/geographies.rb
@@ -22,8 +22,8 @@ module Geographies
def geography_recent_media(id, *args)
options = args.last.is_a?(Hash) ? args.pop : {}
response = get("geographies/#{id}/media/recent", options)
- response["data"]
+ response
end
end
end
-end
+end
View
10 lib/instagram/client/likes.rb
@@ -17,14 +17,14 @@ module Likes
# @see TODO:docs url
def media_likes(id, *args)
response = get("media/#{id}/likes")
- response["data"]
+ response
end
# Issues a like by the currently authenticated user, for a given media item ID
#
# @overload like_media(id, text)
# @param id [Integer] An Instagram media item ID
- # @return [nil]
+ # @return [Hashie::Mash] Metadata
# @example Like media item with ID 777
# Instagram.like_media(777)
# @format :json
@@ -35,14 +35,14 @@ def media_likes(id, *args)
# @see TODO:docs url
def like_media(id, options={})
response = post("media/#{id}/likes", options)
- response["data"]
+ response
end
# Removes the like on a givem media item ID for the currently authenticated user
#
# @overload unlike_media(id)
# @param media_id [Integer] An Instagram media item ID.
- # @return [nil]
+ # @return [Hashie::Mash] Metadata
# @example Remove the like for the currently authenticated user on the media item with the ID of 777
# Instagram.unlike_media(777)
# @format :json
@@ -51,7 +51,7 @@ def like_media(id, options={})
# @see TODO:docs url
def unlike_media(id, options={})
response = delete("media/#{id}/likes", options)
- response["data"]
+ response
end
end
end
View
6 lib/instagram/client/locations.rb
@@ -15,7 +15,7 @@ module Locations
# @see TODO:docs url
def location(id, *args)
response = get("locations/#{id}")
- response["data"]
+ response
end
# Returns a list of recent media items for a given Instagram location
@@ -45,7 +45,7 @@ def location_recent_media(id, *args)
# @param lat [String] A given latitude in decimal format
# @param lng [String] A given longitude in decimal format
# @option options [Integer] :count The number of media items to retrieve.
- # @return [Array]
+ # @return [Hashie::Mash] location resultm object, #data is an Array.
# @example 1: Return a location with the Foursquare Venue ID = ()
# Instagram.location_search("3fd66200f964a520c5f11ee3") (Schiller's Liquor Bar, 131 Rivington St., NY, NY 10002)
# @example 2: Return locations around 37.7808851, -122.3948632 (164 S Park, SF, CA USA)
@@ -64,7 +64,7 @@ def location_search(*args)
lat, lng = args
response = get('locations/search', options.merge(:lat => lat, :lng => lng))
end
- response["data"]
+ response
end
end
end
View
6 lib/instagram/client/media.rb
@@ -18,7 +18,7 @@ module Media
def media_item(*args)
id = args.first || 'self'
response = get("media/#{id}")
- response["data"]
+ response
end
# Returns a list of the overall most popular media
@@ -38,7 +38,7 @@ def media_popular(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
id = args.first || "self"
response = get("media/popular", options)
- response["data"]
+ response
end
# Returns media items within proximity of given lat,lng
@@ -47,7 +47,7 @@ def media_popular(*args)
# @param lng [String] A given longitude in decimal format
# @param options [Hash] A customizable set of options.
# @option options [Integer] :count The number of media items to retrieve.
- # @return [Array]
+ # @return [Hashie::Mash] A list of matching media
# @example Return media around 37.7808851, -122.3948632 (164 S Park, SF, CA USA)
# Instagram.media_search("37.7808851", "-122.3948632")
# @see TODO:doc url
View
8 lib/instagram/client/subscriptions.rb
@@ -19,7 +19,7 @@ module Subscriptions
# @see https://api.instagram.com/developer/realtime/
def subscriptions(options={})
response = get("subscriptions", options.merge(:client_secret => client_secret))
- response["data"]
+ response
end
# Creates a real-time subscription
@@ -64,7 +64,7 @@ def create_subscription(*args)
o[:aspect] = aspect || o[:aspect] || "media"
}
response = post("subscriptions", options.merge(:client_secret => client_secret))
- response["data"]
+ response
end
# Deletes a real-time subscription
@@ -79,7 +79,7 @@ def create_subscription(*args)
# @param options [Hash] Addition options and parameters
# @option options [String] :object When specified will remove all subscriptions of this object type, unless an :object_id is also specified (user, tag, location or geography)
# @option options [String, Integer] :object_id When specifying :object, inlcude an :object_id to only remove subscriptions of that object and object_id
- # @return [nil]
+ # @return [Hashie::Mash]
# @example Deletes an application's user change subscription
# Instagram.delete_subscription(:object => "user")
# @format :json
@@ -93,7 +93,7 @@ def delete_subscription(*args)
subscription_id = args.first
options.merge!(:id => subscription_id) if subscription_id
response = delete("subscriptions", options.merge(:client_secret => client_secret))
- response["data"]
+ response
end
# Process a subscription notification JSON payload
View
6 lib/instagram/client/tags.rb
@@ -15,7 +15,7 @@ module Tags
# @see TODO:docs url
def tag(tag, *args)
response = get("tags/#{tag}")
- response["data"]
+ response
end
# Returns a list of recent media items for a given Instagram tag
@@ -46,13 +46,13 @@ def tag_recent_media(id, *args)
# @param query [String] The beginning or complete tag name to search for
# @param options [Hash] A customizable set of options.
# @option options [Integer] :count The number of media items to retrieve.
- # @return [Array]
+ # @return [Hashie::Mash]
# @see TODO:doc url
# @example Return tags that start with "cat"
# Instagram.tag_search("cat")
def tag_search(query, options={})
response = get('tags/search', options.merge(:q => query))
- response["data"]
+ response
end
end
end
View
30 lib/instagram/client/users.rb
@@ -18,7 +18,7 @@ module Users
def user(*args)
id = args.first || 'self'
response = get("users/#{id}")
- response["data"]
+ response
end
# Returns users that match the given query
@@ -29,13 +29,13 @@ def user(*args)
# @param query [String] The search query to run against user search.
# @param options [Hash] A customizable set of options.
# @option options [Integer] :count The number of users to retrieve.
- # @return [Array]
+ # @return [Hashie::Mash]
# @see TODO:doc url
# @example Return users that match "Shayne Sweeney"
# Instagram.user_search("Shayne Sweeney")
def user_search(query, options={})
response = get('users/search', options.merge(:q => query))
- response["data"]
+ response
end
# Returns a list of users whom a given user follows
@@ -63,7 +63,7 @@ def user_follows(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
id = args.first || "self"
response = get("users/#{id}/follows", options)
- response["data"]
+ response
end
end
@@ -92,7 +92,7 @@ def user_followed_by(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
id = args.first || "self"
response = get("users/#{id}/followed-by", options)
- response["data"]
+ response
end
# Returns a list of users whom a given user is followed by
@@ -112,7 +112,7 @@ def user_followed_by(*args)
# @rate_limited true
def user_requested_by()
response = get("users/self/requested-by")
- response["data"]
+ response
end
# Returns most recent media items from the currently authorized user's feed.
@@ -122,7 +122,7 @@ def user_requested_by()
# @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] :min_id Return media later than this min_id
# @option options [Integer] :count Specifies the number of records to retrieve, per page.
- # @return [Array]
+ # @return [Hashie::Mash]
# @example Return most recent media images that would appear on @shayne's feed
# Instagram.user_media_feed() # assuming @shayne is the authorized user
# @format :json
@@ -160,7 +160,7 @@ def user_recent_media(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
id = args.first || "self"
response = get("users/#{id}/media/recent", options)
- response["data"]
+ response
end
# Returns a list of media items liked by the current user
@@ -195,7 +195,7 @@ def user_liked_media(options={})
# @rate_limited true
def user_relationship(id, options={})
response = get("users/#{id}/relationship", options)
- response["data"]
+ response
end
# Create a follows relationship between the current user and the target user
@@ -213,7 +213,7 @@ def user_relationship(id, options={})
def follow_user(id, options={})
options["action"] = "follow"
response = post("users/#{id}/relationship", options)
- response["data"]
+ response
end
# Destroy a follows relationship between the current user and the target user
@@ -231,7 +231,7 @@ def follow_user(id, options={})
def unfollow_user(id, options={})
options["action"] = "unfollow"
response = post("users/#{id}/relationship", options)
- response["data"]
+ response
end
# Block a relationship between the current user and the target user
@@ -249,7 +249,7 @@ def unfollow_user(id, options={})
def block_user(id, options={})
options["action"] = "block"
response = post("users/#{id}/relationship", options)
- response["data"]
+ response
end
# Remove a relationship block between the current user and the target user
@@ -267,7 +267,7 @@ def block_user(id, options={})
def unblock_user(id, options={})
options["action"] = "unblock"
response = post("users/#{id}/relationship", options)
- response["data"]
+ response
end
# Approve a relationship request between the current user and the target user
@@ -285,7 +285,7 @@ def unblock_user(id, options={})
def approve_user(id, options={})
options["action"] = "approve"
response = post("users/#{id}/relationship", options)
- response["data"]
+ response
end
# Deny a relationship request between the current user and the target user
@@ -303,7 +303,7 @@ def approve_user(id, options={})
def deny_user(id, options={})
options["action"] = "deny"
response = post("users/#{id}/relationship", options)
- response["data"]
+ response
end
end
end
View
2  lib/instagram/oauth.rb
@@ -12,7 +12,7 @@ def authorize_url(options={})
def get_access_token(code, options={})
options[:grant_type] ||= "authorization_code"
params = access_token_params.merge(options)
- post("/oauth/access_token/", params.merge(:code => code), raw=false, unformatted=true)
+ post("/oauth/access_token/", params.merge(:code => code), raw=false, unformatted=true, no_response_wrapper=true)
end
private
View
22 lib/instagram/request.rb
@@ -2,29 +2,29 @@ module Instagram
# Defines HTTP request methods
module Request
# Perform an HTTP GET request
- def get(path, options={}, raw=false, unformatted=false)
- request(:get, path, options, raw, unformatted)
+ def get(path, options={}, raw=false, unformatted=false, no_response_wrapper=false)
+ request(:get, path, options, raw, unformatted, no_response_wrapper)
end
# Perform an HTTP POST request
- def post(path, options={}, raw=false, unformatted=false)
- request(:post, path, options, raw, unformatted)
+ def post(path, options={}, raw=false, unformatted=false, no_response_wrapper=false)
+ request(:post, path, options, raw, unformatted, no_response_wrapper)
end
# Perform an HTTP PUT request
- def put(path, options={}, raw=false, unformatted=false)
- request(:put, path, options, raw, unformatted)
+ def put(path, options={}, raw=false, unformatted=false, no_response_wrapper=false)
+ request(:put, path, options, raw, unformatted, no_response_wrapper)
end
# Perform an HTTP DELETE request
- def delete(path, options={}, raw=false, unformatted=false)
- request(:delete, path, options, raw, unformatted)
+ def delete(path, options={}, raw=false, unformatted=false, no_response_wrapper=false)
+ request(:delete, path, options, raw, unformatted, no_response_wrapper)
end
private
# Perform an HTTP request
- def request(method, path, options, raw=false, unformatted=false)
+ def request(method, path, options, raw=false, unformatted=false, no_response_wrapper=false)
response = connection(raw).send(method) do |request|
path = formatted_path(path) unless unformatted
case method
@@ -35,7 +35,9 @@ def request(method, path, options, raw=false, unformatted=false)
request.body = options unless options.empty?
end
end
- raw ? response : response.body
+ return response if raw
+ return response.body if no_response_wrapper
+ return Response.create( response.body )
end
def formatted_path(path)
View
16 lib/instagram/response.rb
@@ -0,0 +1,16 @@
+module Instagram
+ module Response
+ def self.create( response_hash )
+ data = response_hash.data.dup rescue Hashie::Mash.new
+ data.extend( self )
+ data.instance_exec do
+ @pagination = response_hash.pagination
+ @meta = response_hash.meta
+ end
+ data
+ end
+
+ attr_reader :pagination
+ attr_reader :meta
+ end
+end
View
4 spec/instagram/client/locations_spec.rb
@@ -45,8 +45,8 @@
it "should return a list of media taken at a given location" do
media = @client.location_recent_media(514276)
- media.data.should be_a Array
- media.data.first.user.username.should == "josh"
+ media.should be_a Array
+ media.first.user.username.should == "josh"
end
end
View
4 spec/instagram/client/media_spec.rb
@@ -69,8 +69,8 @@
it "should return an array of user search results" do
media_search = @client.media_search("37.7808851", "-122.3948632")
- media_search.data.should be_a Array
- media_search.data.first.user.username.should == "mikeyk"
+ media_search.should be_a Array
+ media_search.first.user.username.should == "mikeyk"
end
end
end
View
2  spec/instagram/client/subscriptions_spec.rb
@@ -145,4 +145,4 @@
end
end
end
-end
+end
View
4 spec/instagram/client/tags_spec.rb
@@ -45,8 +45,8 @@
it "should return a list of media taken at a given location" do
media = @client.tag_recent_media('cat')
- media.data.should be_a Array
- media.data.first.user.username.should == "amandavan"
+ media.should be_a Array
+ media.first.user.username.should == "amandavan"
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.