instagram 0.3.2 → 0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) 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 +144 -55
  7. data/Rakefile +21 -9
  8. data/instagram.gemspec +41 -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 +21 -55
  13. data/lib/instagram/api.rb +23 -0
  14. data/lib/instagram/client.rb +19 -0
  15. data/lib/instagram/client/comments.rb +62 -0
  16. data/lib/instagram/client/likes.rb +58 -0
  17. data/lib/instagram/client/locations.rb +59 -0
  18. data/lib/instagram/client/media.rb +63 -0
  19. data/lib/instagram/client/real_time.rb +8 -0
  20. data/lib/instagram/client/subscriptions.rb +141 -0
  21. data/lib/instagram/client/tags.rb +59 -0
  22. data/lib/instagram/client/users.rb +165 -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 +16 -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/followed_by.json +1 -0
  33. data/spec/fixtures/follows.json +1 -0
  34. data/spec/fixtures/location.json +1 -0
  35. data/spec/fixtures/location_recent_media.json +1 -0
  36. data/spec/fixtures/location_search.json +1 -0
  37. data/spec/fixtures/media.json +1 -0
  38. data/spec/fixtures/media_comment.json +1 -0
  39. data/spec/fixtures/media_comment_deleted.json +1 -0
  40. data/spec/fixtures/media_comments.json +1 -0
  41. data/spec/fixtures/media_liked.json +1 -0
  42. data/spec/fixtures/media_likes.json +1 -0
  43. data/spec/fixtures/media_popular.json +1 -0
  44. data/spec/fixtures/media_search.json +1 -0
  45. data/spec/fixtures/media_unliked.json +1 -0
  46. data/spec/fixtures/mikeyk.json +1 -0
  47. data/spec/fixtures/recent_media.json +1 -0
  48. data/spec/fixtures/requested_by.json +12 -0
  49. data/spec/fixtures/shayne.json +1 -0
  50. data/spec/fixtures/subscription.json +12 -0
  51. data/spec/fixtures/subscription_deleted.json +1 -0
  52. data/spec/fixtures/subscription_payload.json +14 -0
  53. data/spec/fixtures/subscriptions.json +22 -0
  54. data/spec/fixtures/tag.json +1 -0
  55. data/spec/fixtures/tag_recent_media.json +1 -0
  56. data/spec/fixtures/tag_search.json +1 -0
  57. data/spec/fixtures/user_media_feed.json +1 -0
  58. data/spec/fixtures/user_search.json +1 -0
  59. data/spec/instagram/api_spec.rb +110 -0
  60. data/spec/instagram/client/comments_spec.rb +71 -0
  61. data/spec/instagram/client/likes_spec.rb +66 -0
  62. data/spec/instagram/client/locations_spec.rb +78 -0
  63. data/spec/instagram/client/media_spec.rb +78 -0
  64. data/spec/instagram/client/real_time_spec.rb +13 -0
  65. data/spec/instagram/client/subscriptions_spec.rb +118 -0
  66. data/spec/instagram/client/tags_spec.rb +78 -0
  67. data/spec/instagram/client/users_spec.rb +237 -0
  68. data/spec/instagram/client_spec.rb +23 -0
  69. data/spec/instagram_spec.rb +97 -0
  70. data/spec/spec_helper.rb +54 -0
  71. metadata +265 -33
  72. data/MIT-LICENSE +0 -18
  73. data/lib/instagram/cached.rb +0 -25
  74. data/lib/instagram/failsafe_store.rb +0 -52
  75. data/lib/instagram/models.rb +0 -163
@@ -0,0 +1,12 @@
1
+ *.gem
2
+ *.rbc
3
+ .DS_Store
4
+ .bundle
5
+ .rvmrc
6
+ .yardoc
7
+ .rake_tasks~
8
+ Gemfile.lock
9
+ coverage/*
10
+ doc/*
11
+ log/*
12
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format=nested
3
+ --backtrace
@@ -0,0 +1,9 @@
1
+ --no-private
2
+ --protected
3
+ --tag format:"Supported formats"
4
+ --tag authenticated:"Requires Authentication"
5
+ --tag rate_limited:"Rate Limited"
6
+ --markup markdown
7
+ -
8
+ HISTORY.mkd
9
+ LICENSE.mkd
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Instagram (Burbn, Inc.)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,55 +1,144 @@
1
- # [Instagram][] Ruby library
2
-
3
- This library acts as a client for the [unofficial Instagram API][wiki]. It was used to create [the missing Instagram web interface][web].
4
-
5
- $ gem install instagram
6
-
7
- With it, you can:
8
-
9
- * fetch popular photos;
10
- * get user info;
11
- * browse photos by a user.
12
-
13
- Caveat: you need to know user IDs; usernames can't be used. However, you can start from the popular feed and drill down from there.
14
-
15
- ## Example usage
16
-
17
- require 'instagram'
18
-
19
- photos = Instagram::popular
20
- photo = photos.first
21
-
22
- photo.caption #=> "Extreme dog closeup"
23
- photo.likes.size #=> 54
24
- photo.filter_name #=> "X-Pro II"
25
-
26
- photo.user.username #=> "johndoe"
27
- photo.user.full_name #=> "John Doe"
28
- photo.comments[1].text #=> "That's so cute"
29
- photo.images.last.width #=> 612
30
-
31
- # available sizes: 150px / 306px / 612px square
32
- photo.image_url(612)
33
- # => "http://distillery.s3.amazonaws.com/media/-.jpg" (612×612px image)
34
-
35
- # fetch extended info for John
36
- john_info = Instagram::user_info(photo.user.id)
37
-
38
- john_info.media_count #=> 32
39
- john_info.follower_count #=> 160
40
-
41
-
42
- # find more photos by John
43
- photos_by_john = Instagram::by_user(photo.user.id)
44
-
45
- To see which models and properties are available, see [models.rb][models].
46
-
47
- ## Credits
48
-
49
- Instagram API documentation and Ruby library written by Mislav Marohnić.
50
-
51
-
52
- [instagram]: http://instagr.am/
53
- [web]: http://instagram.heroku.com
54
- [wiki]: https://github.com/mislav/instagram/wiki "Instagram API"
55
- [models]: https://github.com/mislav/instagram/blob/master/lib/instagram/models.rb
1
+ The Instagram Ruby Gem
2
+ ====================
3
+ A Ruby wrapper for the Instagram REST and Search APIs
4
+
5
+
6
+ Installation
7
+ ------------
8
+ gem install instagram
9
+
10
+
11
+ Follow @instagramapi on Twitter
12
+ ----------------------------
13
+ You should [follow @instagramapi on Twitter](http://twitter.com/#!/instagramapi) for announcements,
14
+ updates, and news about the Instagram gem.
15
+
16
+
17
+ Join the mailing list!
18
+ ----------------------
19
+ <https://groups.google.com/group/instagram-ruby-gem>
20
+
21
+
22
+ Does your project or organization use this gem?
23
+ -----------------------------------------------
24
+ Add it to the [apps](http://github.com/Instagram/instagram-ruby-gem/wiki/apps) wiki!
25
+
26
+
27
+ Sample Application
28
+ ------------------
29
+ require "sinatra"
30
+ require "instagram"
31
+
32
+ enable :sessions
33
+
34
+ CALLBACK_URL = "http://localhost:4567/oauth/callback"
35
+
36
+ Instagram.configure do |config|
37
+ config.client_id = "YOUR_CLIENT_ID"
38
+ config.client_secret = "YOUR_CLIENT_SECRET"
39
+ end
40
+
41
+ get "/" do
42
+ '<a href="/oauth/connect">Connect with Instagram</a>'
43
+ end
44
+
45
+ get "/oauth/connect" do
46
+ redirect Instagram.authorize_url(:redirect_uri => CALLBACK_URL)
47
+ end
48
+
49
+ get "/oauth/callback" do
50
+ response = Instagram.get_access_token(params[:code], :redirect_uri => CALLBACK_URL)
51
+ session[:access_token] = response.access_token
52
+ redirect "/feed"
53
+ end
54
+
55
+ get "/feed" do
56
+ client = Instagram.client(:access_token => session[:access_token])
57
+ user = client.user
58
+
59
+ html = "<h1>#{user.username}'s recent photos</h1>"
60
+ for media_item in client.user_recent_media
61
+ html << "<img src='#{media_item.images.thumbnail.url}'>"
62
+ end
63
+ html
64
+ end
65
+
66
+
67
+ API Usage Examples
68
+ ------------------
69
+ require "rubygems"
70
+ require "instagram"
71
+
72
+ # Get a list of a user's most recent media
73
+ puts Instagram.user_recent_media(777)
74
+
75
+ # Get the currently authenticated user's media feed
76
+ puts Instagram.user_media_feed
77
+
78
+ # Get a list of recent media at a given location, in this case, the Instagram office
79
+ puts Instagram.location_recent_media(514276)
80
+
81
+ # All methods require authentication (either by client ID or access token).
82
+ # To get your Instagram OAuth credentials, register an app at http://instagr.am/oauth/client/register/
83
+ Instagram.configure do |config|
84
+ config.client_id = YOUR_CLIENT_KEY
85
+ config.access_token = YOUR_ACCESS_TOKEN
86
+ end
87
+
88
+ # Get a list of all the users you're following
89
+ puts Instagram.follows
90
+
91
+ # Get a list of media close to a given latitude and longitude
92
+ puts Instagram.media_search("37.7808851,-122.3948632")
93
+
94
+ # Get a list of the overall most popular media items
95
+ puts Instagram.media_popular
96
+
97
+ # Search for users on instagram, by name or username
98
+ puts Instagram.user_search("shayne sweeney")
99
+
100
+
101
+ Contributing
102
+ ------------
103
+ In the spirit of [free software](http://www.fsf.org/licensing/essays/free-sw.html), **everyone** is encouraged to help improve this project.
104
+
105
+ Here are some ways *you* can contribute:
106
+
107
+ * by using alpha, beta, and prerelease versions
108
+ * by reporting bugs
109
+ * by suggesting new features
110
+ * by writing or editing documentation
111
+ * by writing specifications
112
+ * by writing code (**no patch is too small**: fix typos, add comments, clean up inconsistent whitespace)
113
+ * by refactoring code
114
+ * by closing [issues](http://github.com/Instagram/instagram-ruby-gem/issues)
115
+ * by reviewing patches
116
+
117
+
118
+ Submitting an Issue
119
+ -------------------
120
+ We use the [GitHub issue tracker](http://github.com/Instagram/instagram-ruby-gem/issues) to track bugs and
121
+ features. Before submitting a bug report or feature request, check to make sure it hasn't already
122
+ been submitted. You can indicate support for an existing issuse by voting it up. When submitting a
123
+ bug report, please include a [Gist](http://gist.github.com/) that includes a stack trace and any
124
+ details that may be necessary to reproduce the bug, including your gem version, Ruby version, and
125
+ operating system. Ideally, a bug report should include a pull request with failing specs.
126
+
127
+
128
+ Submitting a Pull Request
129
+ -------------------------
130
+ 1. Fork the project.
131
+ 2. Create a topic branch.
132
+ 3. Implement your feature or bug fix.
133
+ 4. Add documentation for your feature or bug fix.
134
+ 5. Run <tt>bundle exec rake doc:yard</tt>. If your changes are not 100% documented, go back to step 4.
135
+ 6. Add specs for your feature or bug fix.
136
+ 7. Run <tt>bundle exec rake spec</tt>. If your changes are not 100% covered, go back to step 6.
137
+ 8. Commit and push your changes.
138
+ 9. Submit a pull request. Please do not include changes to the gemspec, version, or history file. (If you want to create your own version for some reason, please do so in a separate commit.)
139
+
140
+
141
+ Copyright
142
+ ---------
143
+ Copyright (c) 2010 Instagram (Burbn, Inc).
144
+ See [LICENSE](https://github.com/Instagram/instagram-ruby-gem/blob/master/LICENSE.md) for details.
data/Rakefile CHANGED
@@ -1,10 +1,22 @@
1
- task :app do
2
- $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift File.dirname($LOAD_PATH.first)
4
- require 'app'
5
- end
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
6
 
7
- task :clear => :app do
8
- items = CachedInstagram.cache.clear
9
- puts "#{items.size} removed"
10
- end
7
+ task :default => :spec
8
+
9
+ namespace :doc do
10
+ require 'yard'
11
+ YARD::Rake::YardocTask.new do |task|
12
+ task.files = ['HISTORY.mkd', 'LICENSE.mkd', 'lib/**/*.rb']
13
+ task.options = [
14
+ '--protected',
15
+ '--output-dir', 'doc/yard',
16
+ '--tag', 'format:Supported formats',
17
+ '--tag', 'authenticated:Requires Authentication',
18
+ '--tag', 'rate_limited:Rate Limited',
19
+ '--markup', 'markdown',
20
+ ]
21
+ end
22
+ end
@@ -0,0 +1,41 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/instagram/version', __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.add_development_dependency('bundler', '~> 1.0')
6
+ s.add_development_dependency('rake', '~> 0.8')
7
+ s.add_development_dependency('rspec', '~> 2.4')
8
+ s.add_development_dependency('yard', '~> 0.6')
9
+ s.add_development_dependency('simplecov', '~> 0.3')
10
+ s.add_development_dependency('webmock', '~> 1.6')
11
+ s.add_development_dependency('ZenTest', '~> 4.4')
12
+ s.add_runtime_dependency('faraday', '~> 0.5.4')
13
+ s.add_runtime_dependency('faraday_middleware', '~> 0.3.1')
14
+ s.add_runtime_dependency('multi_json', '~> 0.0.5')
15
+ s.add_runtime_dependency('hashie', '~> 1.0.0')
16
+ s.authors = ["Shayne Sweeney"]
17
+ s.description = %q{A Ruby wrapper for the Instagram REST and Search APIs}
18
+ s.post_install_message =<<eos
19
+ ********************************************************************************
20
+
21
+ Follow @instagram on Twitter for announcements, updates, and news.
22
+ https://twitter.com/instagramapi
23
+
24
+ Join the mailing list!
25
+ https://groups.google.com/group/instagram-ruby-gem
26
+
27
+ ********************************************************************************
28
+ eos
29
+ s.email = ['shayne@instagr.am']
30
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
31
+ s.files = `git ls-files`.split("\n")
32
+ s.homepage = 'https://github.com/Instagram/instagramrb'
33
+ s.name = 'instagram'
34
+ s.platform = Gem::Platform::RUBY
35
+ s.require_paths = ['lib']
36
+ s.required_rubygems_version = Gem::Requirement.new('>= 1.3.6') if s.respond_to? :required_rubygems_version=
37
+ s.rubyforge_project = s.name
38
+ s.summary = %q{Ruby wrapper for the Instagram API}
39
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
40
+ s.version = Instagram::VERSION.dup
41
+ end
@@ -0,0 +1,36 @@
1
+ require 'faraday'
2
+
3
+ # @private
4
+ module Faraday
5
+ # @private
6
+ class Request::OAuth2 < Faraday::Middleware
7
+ def call(env)
8
+
9
+ if env[:method] == :get or env[:method] == :delete
10
+ env[:url].query_values = {} if env[:url].query_values.nil?
11
+ if @access_token and not env[:url].query_values["client_secret"]
12
+ env[:url].query_values = env[:url].query_values.merge(:access_token => @access_token)
13
+ env[:request_headers] = env[:request_headers].merge('Authorization' => "Token token=\"#{@access_token}\"")
14
+ elsif @client_id
15
+ env[:url].query_values = env[:url].query_values.merge(:client_id => @client_id)
16
+ end
17
+ else
18
+ if @access_token and not env[:body] && env[:body][:client_secret]
19
+ env[:body] = {} if env[:body].nil?
20
+ env[:body] = env[:body].merge(:access_token => @access_token)
21
+ env[:request_headers] = env[:request_headers].merge('Authorization' => "Token token=\"#{@access_token}\"")
22
+ elsif @client_id
23
+ env[:body] = env[:body].merge(:client_id => @client_id)
24
+ end
25
+ end
26
+
27
+ @app.call env
28
+ end
29
+
30
+ def initialize(app, client_id, access_token=nil)
31
+ @app = app
32
+ @client_id = client_id
33
+ @access_token = access_token
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,37 @@
1
+ require 'faraday'
2
+
3
+ # @private
4
+ module Faraday
5
+ # @private
6
+ class Response::RaiseHttp4xx < Response::Middleware
7
+ def self.register_on_complete(env)
8
+ env[:response].on_complete do |response|
9
+ case response[:status].to_i
10
+ when 400
11
+ raise Instagram::BadRequest, error_message(response)
12
+ when 404
13
+ raise Instagram::NotFound, error_message(response)
14
+ end
15
+ end
16
+ end
17
+
18
+ def initialize(app)
19
+ super
20
+ @parser = nil
21
+ end
22
+
23
+ private
24
+
25
+ def self.error_message(response)
26
+ "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:status]}#{error_body(response[:body])}"
27
+ end
28
+
29
+ def self.error_body(body)
30
+ if body.nil?
31
+ nil
32
+ elsif body['meta'] and not body['meta']['error_message'].blank?
33
+ ": #{body['meta']['error_message']}"
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,29 @@
1
+ require 'faraday'
2
+
3
+ # @private
4
+ module Faraday
5
+ # @private
6
+ class Response::RaiseHttp5xx < Response::Middleware
7
+ def self.register_on_complete(env)
8
+ env[:response].on_complete do |response|
9
+ case response[:status].to_i
10
+ when 500
11
+ raise Instagram::InternalServerError, error_message(response, "Something is technically wrong.")
12
+ when 503
13
+ raise Instagram::ServiceUnavailable, error_message(response, "Instagram is rate limiting your requests.")
14
+ end
15
+ end
16
+ end
17
+
18
+ def initialize(app)
19
+ super
20
+ @parser = nil
21
+ end
22
+
23
+ private
24
+
25
+ def self.error_message(response, body=nil)
26
+ "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{[response[:status].to_s + ':', body].compact.join(' ')}"
27
+ end
28
+ end
29
+ end
@@ -1,60 +1,26 @@
1
- require 'addressable/uri'
2
- require 'addressable/template'
3
- require 'net/http'
4
- require 'instagram/models'
1
+ require File.expand_path('../instagram/error', __FILE__)
2
+ require File.expand_path('../instagram/configuration', __FILE__)
3
+ require File.expand_path('../instagram/api', __FILE__)
4
+ require File.expand_path('../instagram/client', __FILE__)
5
5
 
6
6
  module Instagram
7
-
8
- extend self
9
-
10
- Popular = Addressable::URI.parse 'http://instagr.am/api/v1/feed/popular/'
11
- UserFeed = Addressable::Template.new 'http://instagr.am/api/v1/feed/user/{user_id}/'
12
- UserInfo = Addressable::Template.new 'http://instagr.am/api/v1/users/{user_id}/info/'
13
- SearchTags = Addressable::URI.parse 'http://instagr.am/api/v1/tags/search/'
14
- TagFeed = Addressable::Template.new 'http://instagr.am/api/v1/feed/tag/{tag}/'
15
-
16
- def popular(params = {}, options = {})
17
- parse_response(Popular.dup, params, options.fetch(:parse_with, Timeline))
18
- end
19
-
20
- def by_user(user_id, params = {}, options = {})
21
- url = UserFeed.expand :user_id => user_id
22
- parse_response(url, params, options.fetch(:parse_with, Timeline))
23
- end
24
-
25
- def user_info(user_id, params = {}, options = {})
26
- url = UserInfo.expand :user_id => user_id
27
- parse_response(url, params, options.fetch(:parse_with, UserWrap))
28
- end
29
-
30
- def search_tags(query, params = {}, options = {})
31
- params = {:q => query}.merge(params)
32
- parse_response(SearchTags.dup, params, options.fetch(:parse_with, SearchTagsResults))
33
- end
34
-
35
- def by_tag(tag, params = {}, options = {})
36
- url = TagFeed.expand :tag => tag
37
- parse_response(url, params, options.fetch(:parse_with, Timeline))
7
+ extend Configuration
8
+
9
+ # Alias for Instagram::Client.new
10
+ #
11
+ # @return [Instagram::Client]
12
+ def self.client(options={})
13
+ Instagram::Client.new(options)
38
14
  end
39
-
40
- private
41
-
42
- def parse_response(url, params, parser = nil)
43
- url.query_values = params
44
- body = get_url url
45
- parser ? parser.parse(body) : body
15
+
16
+ # Delegate to Instagram::Client
17
+ def self.method_missing(method, *args, &block)
18
+ return super unless client.respond_to?(method)
19
+ client.send(method, *args, &block)
46
20
  end
47
-
48
- def get_url(url)
49
- response = Net::HTTP.start(url.host, url.port) { |http|
50
- http.get url.request_uri, 'User-agent' => 'Instagram Ruby client'
51
- }
52
-
53
- if Net::HTTPSuccess === response
54
- response.body
55
- else
56
- response.error!
57
- end
21
+
22
+ # Delegate to Instagram::Client
23
+ def self.respond_to?(method)
24
+ return client.respond_to?(method) || super
58
25
  end
59
-
60
- end
26
+ end