ruby-flickr 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/flickr/auth.rb +69 -0
- data/lib/flickr/base.rb +39 -7
- data/lib/flickr/photos.rb +20 -2
- data/lib/ruby_flickr.rb +7 -3
- metadata +13 -4
data/lib/flickr/auth.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
class Flickr::Auth < Flickr::Base
|
2
|
+
def initialize(flickr)
|
3
|
+
@flickr = flickr
|
4
|
+
end
|
5
|
+
|
6
|
+
# get or return a frob to use for authentication
|
7
|
+
def frob
|
8
|
+
@frob ||= get_frob
|
9
|
+
end
|
10
|
+
|
11
|
+
# generates the authorization url to allow access to a flickr account.
|
12
|
+
#
|
13
|
+
# Params
|
14
|
+
# * perms (Optional)
|
15
|
+
# sets the permision level to grant on the flickr account.
|
16
|
+
# :read - permission to read private information (DEFAULT)
|
17
|
+
# :write - permission to add, edit and delete photo metadata (includes 'read')
|
18
|
+
# :delete - permission to delete photos (includes 'write' and 'read')
|
19
|
+
#
|
20
|
+
def url(perms = :read)
|
21
|
+
options = {:api_key => @flickr.api_key, :perms => perms, :frob => self.frob}
|
22
|
+
@flickr.sign_request(options)
|
23
|
+
Flickr::Base::AUTH_ENDPOINT + "?" + options.collect{|k,v| "#{k}=#{v}"}.join('&')
|
24
|
+
end
|
25
|
+
|
26
|
+
# gets the token for the current frob
|
27
|
+
#
|
28
|
+
# Params
|
29
|
+
# * pass_through (Optional)
|
30
|
+
# Boolean value that determines if a call will be made to flickr to find a taken for the current frob if empty
|
31
|
+
#
|
32
|
+
def token(pass_through = true)
|
33
|
+
@token ||= get_token(pass_through) rescue nil
|
34
|
+
end
|
35
|
+
|
36
|
+
# saves the current token to the cache file if token exists
|
37
|
+
#
|
38
|
+
# Param
|
39
|
+
# * filename (Optional)
|
40
|
+
# filename of the cache file. defaults to the file passed into Flickr.new
|
41
|
+
#
|
42
|
+
def cache_token(filename = @flickr.token_cache)
|
43
|
+
if filename and self.token
|
44
|
+
cache_file = File.open(filename, 'w+')
|
45
|
+
cache_file.print self.token
|
46
|
+
cache_file.close
|
47
|
+
true
|
48
|
+
else
|
49
|
+
false
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
def get_frob
|
55
|
+
rsp = @flickr.send_request('flickr.auth.getFrob')
|
56
|
+
|
57
|
+
rsp.frob.to_s
|
58
|
+
end
|
59
|
+
|
60
|
+
def get_token(pass_through)
|
61
|
+
if @flickr.token_cache and File.exists?(@flickr.token_cache)
|
62
|
+
File.open(@flickr.token_cache, 'r').read
|
63
|
+
elsif pass_through
|
64
|
+
rsp = @flickr.send_request('flickr.auth.getToken', {:frob => self.frob})
|
65
|
+
|
66
|
+
rsp.auth.token.to_s
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/lib/flickr/base.rb
CHANGED
@@ -4,24 +4,50 @@ module Flickr
|
|
4
4
|
end
|
5
5
|
|
6
6
|
class Base
|
7
|
-
|
7
|
+
attr_reader :api_key, :api_secret, :token_cache
|
8
|
+
|
9
|
+
REST_ENDPOINT = 'http://api.flickr.com/services/rest/'
|
10
|
+
AUTH_ENDPOINT = 'http://flickr.com/services/auth/'
|
8
11
|
|
9
12
|
# create a new flickr object
|
10
13
|
#
|
11
14
|
# Params
|
12
15
|
# * api_key (Required)
|
16
|
+
# The api key given to you by flickr.
|
13
17
|
# * api_secret (Optional)
|
14
|
-
|
18
|
+
# The api secret given to you by flickr. This is used to generate a signiture for signed request.
|
19
|
+
# * token_cache (Optional)
|
20
|
+
# File path to a cache file that holds a flickr token.
|
21
|
+
#
|
22
|
+
def initialize(api_key, api_secret = nil, token_cache = nil)
|
15
23
|
@api_key = api_key
|
16
24
|
@api_secret = api_secret
|
25
|
+
@token_cache = token_cache
|
17
26
|
end
|
18
27
|
|
19
|
-
|
28
|
+
# sends a request to the flcikr REST api
|
29
|
+
#
|
30
|
+
# Params
|
31
|
+
# * method (Required)
|
32
|
+
# name of the flickr method (ex. flickr.photos.search)
|
33
|
+
# * options (Optional)
|
34
|
+
# hash of query parameters, you do not need to include api_key, api_sig or auth_token because these are added automatically
|
35
|
+
# * http_method (Optional)
|
36
|
+
# choose between a GET and POST http request. Valid options are:
|
37
|
+
# :get (DEFAULT)
|
38
|
+
# :post
|
39
|
+
#
|
40
|
+
def send_request(method, options = {}, http_method = :get)
|
20
41
|
options.merge!(:api_key => @api_key, :method => method)
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
42
|
+
sign_request(options)
|
43
|
+
|
44
|
+
if http_method == :get
|
45
|
+
api_call = REST_ENDPOINT + "?" + options.collect{|k,v| "#{k}=#{v}"}.join('&')
|
46
|
+
rsp = Net::HTTP.get(URI.parse(api_call))
|
47
|
+
else
|
48
|
+
rsp = Net::HTTP.post_form(URI.parse(REST_ENDPOINT), options).body
|
49
|
+
end
|
50
|
+
|
25
51
|
xm = XmlMagic.new(rsp)
|
26
52
|
|
27
53
|
if xm[:stat] == 'ok'
|
@@ -30,7 +56,13 @@ module Flickr
|
|
30
56
|
raise "#{xm.err[:code]}: #{xm.err[:msg]}"
|
31
57
|
end
|
32
58
|
end
|
59
|
+
|
60
|
+
def sign_request(options, authorize = true)
|
61
|
+
options.merge!(:auth_token => self.auth.token(false)) if authorize and self.auth.token(false)
|
62
|
+
options.merge!(:api_sig => Digest::MD5.hexdigest(@api_secret + options.keys.sort_by{|k| k.to_s}.collect{|k| k.to_s + options[k].to_s}.join)) if @api_secret
|
63
|
+
end
|
33
64
|
|
34
65
|
def photos() @photos ||= Photos.new(self) end
|
66
|
+
def auth() @auth ||= Auth.new(self) end
|
35
67
|
end
|
36
68
|
end
|
data/lib/flickr/photos.rb
CHANGED
@@ -133,7 +133,13 @@ class Flickr::Photos < Flickr::Base
|
|
133
133
|
|
134
134
|
rsp = @flickr.send_request('flickr.photos.search', options)
|
135
135
|
|
136
|
-
returning PhotoResponse.new(:page => rsp.photos[:page]
|
136
|
+
returning PhotoResponse.new(:page => rsp.photos[:page].to_i,
|
137
|
+
:pages => rsp.photos[:pages].to_i,
|
138
|
+
:per_page => rsp.photos[:perpage].to_i,
|
139
|
+
:total => rsp.photos[:total].to_i,
|
140
|
+
:photos => [], :api => self,
|
141
|
+
:method => 'flickr.photos.search',
|
142
|
+
:options => options) do |photos|
|
137
143
|
rsp.photos.photo.each do |photo|
|
138
144
|
all_attributes = {:id => photo[:id],
|
139
145
|
:owner => photo[:owner],
|
@@ -155,7 +161,7 @@ class Flickr::Photos < Flickr::Base
|
|
155
161
|
:tags => photo[:tags],
|
156
162
|
:machine_tags => photo[:machine_tags],
|
157
163
|
:o_dims => photo[:o_dims],
|
158
|
-
:views => photo[:views]}
|
164
|
+
:views => photo[:views].to_i}
|
159
165
|
|
160
166
|
used_attributes = {}
|
161
167
|
|
@@ -271,6 +277,18 @@ class Flickr::Photos < Flickr::Base
|
|
271
277
|
true
|
272
278
|
end
|
273
279
|
end
|
280
|
+
|
281
|
+
# Add tags to a photo.
|
282
|
+
#
|
283
|
+
# Params
|
284
|
+
# * tags (Required)
|
285
|
+
# comma seperated list of tags
|
286
|
+
#
|
287
|
+
def add_tags(tags)
|
288
|
+
rsp = @flickr.send_request('flickr.photos.addTags', {:photo_id => self.id, :tags => tags}, :post)
|
289
|
+
|
290
|
+
true
|
291
|
+
end
|
274
292
|
|
275
293
|
def description # :nodoc:
|
276
294
|
attach_info
|
data/lib/ruby_flickr.rb
CHANGED
@@ -1,10 +1,14 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'xml_magic'
|
3
|
-
require '
|
3
|
+
require 'net/http'
|
4
|
+
require 'uri'
|
4
5
|
require 'digest/md5'
|
5
|
-
include CommonThread::XML
|
6
6
|
|
7
|
-
|
7
|
+
require File.join(File.dirname(__FILE__), 'flickr', 'base')
|
8
|
+
require File.join(File.dirname(__FILE__), 'flickr', 'auth')
|
9
|
+
require File.join(File.dirname(__FILE__), 'flickr', 'photos')
|
10
|
+
|
11
|
+
include CommonThread::XML
|
8
12
|
|
9
13
|
class Object
|
10
14
|
# returning allows you to pass an object to a block that you can manipulate returning the manipulated object
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-flickr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Wyrosdick
|
@@ -9,10 +9,18 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-03-
|
12
|
+
date: 2008-03-31 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: xml-magic
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: "0"
|
23
|
+
version:
|
16
24
|
description:
|
17
25
|
email: ben@commonthread.com
|
18
26
|
executables: []
|
@@ -23,6 +31,7 @@ extra_rdoc_files: []
|
|
23
31
|
|
24
32
|
files:
|
25
33
|
- lib/flickr
|
34
|
+
- lib/flickr/auth.rb
|
26
35
|
- lib/flickr/base.rb
|
27
36
|
- lib/flickr/photos.rb
|
28
37
|
- lib/ruby_flickr.rb
|