flickr_party 0.2.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_party.rb +74 -0
  3. metadata +65 -0
data/README.markdown ADDED
@@ -0,0 +1,46 @@
1
+ Flickr Party
2
+ ============
3
+
4
+ Wrapper for the Flickr API, with real application authentication, using HTTParty.
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 flickr_party -s http://gemcutter.org
19
+
20
+ Usage
21
+ -----
22
+
23
+ require 'rubygems'
24
+ require 'flickr_party'
25
+
26
+ API_KEY = '...'
27
+ SECRETE = '...'
28
+
29
+ f = FlickrParty.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')
@@ -0,0 +1,74 @@
1
+ require 'rubygems'
2
+ require 'httparty'
3
+ require 'md5'
4
+
5
+ class FlickrParty
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
+ "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,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flickr_party
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Tim Morgan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-20 00:00:00 -05: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_party.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/seven1m/flickr_party
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.3.5
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Lightweight wrapper for Flickr API using HTTParty
64
+ test_files: []
65
+