seven1m-flickr 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.markdown +46 -0
  2. data/lib/flickr.rb +74 -0
  3. metadata +63 -0
data/README.markdown ADDED
@@ -0,0 +1,46 @@
1
+ Flickr API Wrapper
2
+ ==================
3
+
4
+ Wrapper for the Flickr API, with real application authentication.
5
+
6
+ About
7
+ -----
8
+
9
+ This piece of code uses HTTParty for its magic. All Flickr methods should
10
+ be supported, but you'll still need to reference the Flickr API docs heavily
11
+ and understand how they work.
12
+
13
+ All method calls are automatically signed and include the authentication token.
14
+
15
+ Installation
16
+ ------------
17
+
18
+ sudo gem install seven1m-flickr -s http://gems.github.com
19
+
20
+ Usage
21
+ -----
22
+
23
+ require 'rubygems'
24
+ require 'flickr'
25
+
26
+ API_KEY = '...'
27
+ SECRETE = '...'
28
+
29
+ f = Flickr.new(API_KEY, SECRET)
30
+
31
+ # The code supports real application authentication.
32
+ url = f.auth_url # returns the url you should send the user to
33
+ `open #{url}` # open in browser
34
+
35
+ # This is just a way to pause until the user says go.
36
+ # If you're building a gui app or web-based app, you will obviously do something different.
37
+ print 'Press any key once you have authorized the app...'
38
+ require "highline/system_extensions"
39
+ HighLine::SystemExtensions.get_character
40
+
41
+ f.complete_auth
42
+
43
+ # The wrapper uses method_missing to accept any method supported by the API (now or in the future).
44
+ # For instance, the method "flickr.activity.userPhotos" is called with the code below.
45
+
46
+ f.flickr.activity.userPhotos('timeframe' => '10d')
data/lib/flickr.rb ADDED
@@ -0,0 +1,74 @@
1
+ require 'rubygems'
2
+ require 'httparty'
3
+ require 'md5'
4
+
5
+ class Flickr
6
+
7
+ ENDPOINT = 'http://api.flickr.com/services/rest/'
8
+
9
+ class PhotoURLError < RuntimeError; end
10
+
11
+ include HTTParty
12
+ format :xml
13
+
14
+ attr_accessor :token
15
+
16
+ THIRD_LEVEL_METHODS = %w(add browse search delete create find echo login null)
17
+
18
+ def initialize(api_key, secret, method=nil, token=nil)
19
+ @api_key = api_key
20
+ @secret = secret
21
+ @method = method
22
+ @token = token
23
+ end
24
+
25
+ def method_missing(method_name, args={}, test=nil)
26
+ if @method.to_s.count('.') == 2 or method_name =~ /[A-Z]/ or THIRD_LEVEL_METHODS.include?(method_name.to_s)
27
+ args = self.class.stringify_hash_keys(args)
28
+ args.merge!('api_key' => @api_key, 'method' => @method + '.' + method_name.to_s, 'format' => 'rest')
29
+ if @token
30
+ args.merge!('auth_token' => @token)
31
+ end
32
+ args.merge!(:api_sig => MD5.hexdigest(@secret + args.to_a.sort.to_s))
33
+ self.class.post(ENDPOINT, :body => args)
34
+ else
35
+ if @method
36
+ method = @method + '.' + method_name.to_s
37
+ else
38
+ method = method_name.to_s
39
+ end
40
+ self.class.new(@api_key, @secret, method, @token)
41
+ end
42
+ end
43
+
44
+ def auth_url(perms='read')
45
+ @frob = self.flickr.auth.getFrob['rsp']['frob']
46
+ sig = MD5.hexdigest("#{@secret}api_key#{@api_key}frob#{@frob}perms#{perms}")
47
+ puts "http://flickr.com/services/auth/?api_key=#{@api_key}&perms=#{perms}&frob=#{@frob}&api_sig=#{sig}"
48
+ end
49
+
50
+ def complete_auth
51
+ @auth = self.flickr.auth.getToken('frob' => @frob)['rsp']['auth']
52
+ @token = @auth['token']
53
+ end
54
+
55
+ def photo_url(photo_hash, size=nil)
56
+ if %w(m s t b).include?(size)
57
+ "http://farm#{photo_hash['farm']}.static.flickr.com/#{photo_hash['server']}/#{photo_hash['id']}_#{photo_hash['secret']}_#{size}.jpg"
58
+ elsif size == 'o' and photo_hash['originalsecret'] and photo_hash['originalformat']
59
+ "http://farm#{photo_hash['farm']}.static.flickr.com/#{photo_hash['server']}/#{photo_hash['id']}_#{photo_hash['originalsecret']}_#{size}.#{photo_hash['originalformat']}"
60
+ elsif size.nil? or size == '-'
61
+ "http://farm#{photo_hash['farm']}.static.flickr.com/#{photo_hash['server']}/#{photo_hash['id']}_#{photo_hash['secret']}.jpg"
62
+ else
63
+ raise PhotoURLError, "Invalid size or missing keys in photo_hash. Valid sizes are m, s, t, b, o, and nil. For original (o) size, photo_hash must contain both 'originalsecret' and 'originalformat'."
64
+ end
65
+ end
66
+
67
+ def self.stringify_hash_keys(hash)
68
+ hash.inject({}) do |options, (key, value)|
69
+ options[key.to_s] = value
70
+ options
71
+ end
72
+ end
73
+
74
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: seven1m-flickr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tim Morgan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-16 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: httparty
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description:
26
+ email: tim@timmorgan.org
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - README.markdown
35
+ - lib/flickr.rb
36
+ has_rdoc: false
37
+ homepage: http://github.com/seven1m/flickr
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.2.0
59
+ signing_key:
60
+ specification_version: 2
61
+ summary: Lightweight wrapper for Flickr API
62
+ test_files: []
63
+