objectiveflickr 0.9.1 → 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +46 -0
- data/Manifest.txt +1 -0
- data/lib/objectiveflickr/flickr_invocation.rb +26 -52
- data/lib/objectiveflickr/flickr_photo.rb +75 -0
- data/lib/objectiveflickr/flickr_response.rb +5 -5
- data/lib/objectiveflickr/version.rb +1 -1
- data/test/objectiveflickr_test.rb +36 -0
- metadata +3 -2
data/History.txt
CHANGED
@@ -0,0 +1,46 @@
|
|
1
|
+
2006-12-27 (0.9.2):
|
2
|
+
* Added support for Flickr's new "farm" part newly found in its photo URLs
|
3
|
+
|
4
|
+
* Your code needs no change, everything will just work, including
|
5
|
+
those photo URL/div id helpers, but on the other hand...
|
6
|
+
|
7
|
+
* Deprecated methods in FlickrInvocation:
|
8
|
+
|
9
|
+
* photo_url(params)
|
10
|
+
* photo_div_id(params, prefix='photo')
|
11
|
+
* photo_url_from_div_id(uid)
|
12
|
+
* photo_info_from_div_id(uid)
|
13
|
+
|
14
|
+
Those are methods that should never go into an invocation object.
|
15
|
+
Instead they belong to a helper, so:
|
16
|
+
|
17
|
+
* Created a FlickrPhoto module
|
18
|
+
|
19
|
+
FlickrPhoto.url_from_hash(params)
|
20
|
+
FlickrPhoto.unique_id_from_hash(params, prefix)
|
21
|
+
FlickrPhoto.url_from_unique_id(uid)
|
22
|
+
FlickrPhoto.hash_from_unique_id(uid)
|
23
|
+
|
24
|
+
Those helper functions now take place of their deprecated
|
25
|
+
predecessors.
|
26
|
+
|
27
|
+
* Before :server_id and :server were both accepted hash key
|
28
|
+
for the "param" in those helper functions, now only :server
|
29
|
+
is accepted. This is in accordance with Flickr's own
|
30
|
+
naming convention.
|
31
|
+
|
32
|
+
* You no longer need to set :auth=>true if you already supply
|
33
|
+
an :auth_token parameter when you make Flickr method calls.
|
34
|
+
Since :auth_token always implies that the call must be
|
35
|
+
authenticated, ObjectiveFlickr is happy to save a few strokes
|
36
|
+
for you. :)
|
37
|
+
|
38
|
+
|
39
|
+
2006-12-12 (0.9.1):
|
40
|
+
Added default_* methods into FlickrInvocation
|
41
|
+
Removed History.txt
|
42
|
+
|
43
|
+
|
44
|
+
2006-12-10 (0.9.0):
|
45
|
+
Initial release.
|
46
|
+
|
data/Manifest.txt
CHANGED
@@ -7,16 +7,16 @@
|
|
7
7
|
# Copyright:: Copyright (c) 2006 Lukhnos D. Liu
|
8
8
|
# License:: Distributed under the New BSD License
|
9
9
|
|
10
|
-
# This class plays the major role of the package. Named "FlickrInvocation"
|
11
|
-
# to allude to the making of an RPC call.
|
12
|
-
|
13
10
|
require 'rubygems'
|
14
11
|
require 'net/http'
|
15
12
|
require 'jcode'
|
16
13
|
require 'digest/md5'
|
14
|
+
|
17
15
|
$KCODE = 'UTF8'
|
18
16
|
|
19
|
-
# FlickrInvocation
|
17
|
+
# This class plays the major role of the package. Named "FlickrInvocation"
|
18
|
+
# to allude to the making of an RPC call.
|
19
|
+
|
20
20
|
class FlickrInvocation
|
21
21
|
@@default_api_key = ''
|
22
22
|
@@default_shared_secret = ''
|
@@ -25,7 +25,6 @@ class FlickrInvocation
|
|
25
25
|
SHARED_SECRET = ''
|
26
26
|
AUTH_ENDPOINT = 'http://flickr.com/services/auth/'
|
27
27
|
REST_ENDPOINT = 'http://api.flickr.com/services/rest/'
|
28
|
-
PHOTOURL_BASE = 'http://static.flickr.com/'
|
29
28
|
|
30
29
|
# Initializes the instance with the api_key (required) and an
|
31
30
|
# optional shared_secret (required only if you need to make
|
@@ -42,6 +41,10 @@ class FlickrInvocation
|
|
42
41
|
# if you want the method call to be signed (required when you
|
43
42
|
# make authenticaed calls, e.g. flickr.auth.getFrob or
|
44
43
|
# any method call that requires an auth token)
|
44
|
+
#
|
45
|
+
# NOTE: If you supply :auth_token in the params hash, your API
|
46
|
+
# call will automatically be signed, and the call will be
|
47
|
+
# treated by Flickr as an authenticated call
|
45
48
|
def call(method, params=nil)
|
46
49
|
url = method_url(method, params)
|
47
50
|
rsp = FlickrResponse.new Net::HTTP.get(URI.parse(url))
|
@@ -62,31 +65,24 @@ class FlickrInvocation
|
|
62
65
|
url = "#{AUTH_ENDPOINT}?api_key=#{@api_key}&perms=#{permission}&api_sig=#{sig}"
|
63
66
|
end
|
64
67
|
|
65
|
-
#
|
66
|
-
# the keys :server_id, :id, :secret, :size and :type
|
68
|
+
# DEPRECATED--Use FlickrPhoto.url_from_hash(params)
|
67
69
|
def photo_url(params)
|
68
|
-
|
70
|
+
FlickrPhoto.url_from_hash(params)
|
69
71
|
end
|
70
|
-
|
71
|
-
#
|
72
|
-
# one gets the real URL of a photo) into a photo id that you can
|
73
|
-
# use in a div
|
72
|
+
|
73
|
+
# DEPRECATED--Use FlickrPhoto.unique_id_from_hash(params, prefix)
|
74
74
|
def photo_div_id(params, prefix='photo')
|
75
|
-
|
76
|
-
[prefix, p[:server_id], p[:id], p[:secret], p[:size], p[:type]].join("-")
|
75
|
+
FlickrPhoto.unique_id_from_hash(params, prefix)
|
77
76
|
end
|
78
|
-
|
79
|
-
#
|
80
|
-
|
81
|
-
|
82
|
-
photo_url(photo_info_from_div_id(params))
|
77
|
+
|
78
|
+
# DEPRECATED--Use FlickrPhoto.url_from_unique_id(uid)
|
79
|
+
def photo_url_from_div_id(uid)
|
80
|
+
FlickrPhoto.url_from_unique_id(uid)
|
83
81
|
end
|
84
82
|
|
85
|
-
#
|
86
|
-
|
87
|
-
|
88
|
-
p = params.split("-")
|
89
|
-
{ :server_id=>p[1], :id=>p[2], :secret=>p[3], :size=>p[4], :type=>p[5] }
|
83
|
+
# DEPRECATED--Use FlickrPhoto.hash_from_unique_id(uid)
|
84
|
+
def photo_info_from_div_id(uid)
|
85
|
+
FlickrPhoto.hash_from_unique_id(uid)
|
90
86
|
end
|
91
87
|
|
92
88
|
# set the default API key
|
@@ -98,7 +94,8 @@ class FlickrInvocation
|
|
98
94
|
def self.default_shared_secret(s)
|
99
95
|
@@default_shared_secret=s
|
100
96
|
end
|
101
|
-
|
97
|
+
|
98
|
+
# set the default options, e.g. :raise_exception_on_error=>true
|
102
99
|
def self.default_options(o)
|
103
100
|
@@default_options = o
|
104
101
|
end
|
@@ -111,7 +108,7 @@ class FlickrInvocation
|
|
111
108
|
p[:nojsoncallback] = 1
|
112
109
|
|
113
110
|
url = "#{REST_ENDPOINT}?api_key=#{@api_key}&method=#{method}"
|
114
|
-
if p[:auth] || p["auth"]
|
111
|
+
if p[:auth] || p["auth"] || p[:auth_token] || p["auth_token"]
|
115
112
|
p.delete(:auth)
|
116
113
|
p.delete("auth")
|
117
114
|
sigp = p
|
@@ -124,36 +121,13 @@ class FlickrInvocation
|
|
124
121
|
url
|
125
122
|
end
|
126
123
|
|
127
|
-
private
|
128
|
-
def photo_url_form(p)
|
129
|
-
url = "#{PHOTOURL_BASE}#{p[:server_id]}/#{p[:id]}_#{p[:secret]}"
|
130
|
-
if p[:size].length > 0
|
131
|
-
url += "_#{p[:size]}"
|
132
|
-
end
|
133
|
-
|
134
|
-
url += ".#{p[:type]}"
|
135
|
-
end
|
136
|
-
|
137
|
-
private
|
138
|
-
def photo_params(params)
|
139
|
-
{
|
140
|
-
:server_id => params[:server] || params["server"] || params[:server_id] || params["server_id"] || "",
|
141
|
-
:id => params[:id] || params["id"] || "",
|
142
|
-
:secret => params[:secret] || params["secret"] || "",
|
143
|
-
:size => params[:size] || params["size"] || "",
|
144
|
-
:type => params[:type] || params["type"] || "jpg"
|
145
|
-
}
|
146
|
-
end
|
147
|
-
|
148
124
|
private
|
149
125
|
def api_sig(params)
|
150
126
|
sigstr = @shared_secret
|
151
|
-
params.keys.sort { |x, y| x.to_s <=> y.to_s }.each
|
127
|
+
params.keys.sort { |x, y| x.to_s <=> y.to_s }.each do |k|
|
152
128
|
sigstr += k.to_s
|
153
129
|
sigstr += params[k].to_s
|
154
|
-
|
155
|
-
|
156
|
-
# print "sigstr=#{sigstr}, md5str=#{md5str}\n"
|
157
|
-
md5str
|
130
|
+
end
|
131
|
+
Digest::MD5.hexdigest(sigstr)
|
158
132
|
end
|
159
133
|
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# objectiveflickr is a minimalistic Flickr API library that uses REST-style calls
|
2
|
+
# and receives JSON response blocks, resulting in very concise code. Named so in
|
3
|
+
# order to echo another Flickr library of mine, under the same name, developed
|
4
|
+
# for Objective-C.
|
5
|
+
#
|
6
|
+
# Author:: Lukhnos D. Liu (mailto:lukhnos@gmail.com)
|
7
|
+
# Copyright:: Copyright (c) 2006 Lukhnos D. Liu
|
8
|
+
# License:: Distributed under the New BSD License
|
9
|
+
|
10
|
+
# This helper module provides a set of utility methods for Flickr photos.
|
11
|
+
# Methods such as unique_id_from_hash and url_from_unique_id are
|
12
|
+
# helpful when you try to move the photo id data to and fro your web
|
13
|
+
# apps.
|
14
|
+
|
15
|
+
module FlickrPhoto
|
16
|
+
@photo_url_base = 'static.flickr.com'
|
17
|
+
|
18
|
+
# Set the default photo base URL, without the http:// part
|
19
|
+
def self.default_photo_url_base(b)
|
20
|
+
@photo_url_base = b
|
21
|
+
end
|
22
|
+
|
23
|
+
# This utility method returns the URL of a Flickr photo using
|
24
|
+
# the keys :farm, :server, :id, :secret, :size and :type
|
25
|
+
def self.url_from_hash(params)
|
26
|
+
self.url_from_normalized_hash(self.normalize_parameter(params))
|
27
|
+
end
|
28
|
+
|
29
|
+
# This utility method combines the Flickr photo keys (from which
|
30
|
+
# one gets the real URL of a photo) into a photo id that you can
|
31
|
+
# use in a div
|
32
|
+
def self.unique_id_from_hash(params, prefix='photo')
|
33
|
+
p = self.normalize_parameter(params)
|
34
|
+
[prefix, p[:server], p[:id], p[:secret], p[:farm], p[:size], p[:type]].join("-")
|
35
|
+
end
|
36
|
+
|
37
|
+
# This utility method breaks apart the photo id into Flickr photo
|
38
|
+
# keys and returns the photo URL
|
39
|
+
def self.url_from_unique_id(uid)
|
40
|
+
self.url_from_normalized_hash(self.hash_from_unique_id(uid))
|
41
|
+
end
|
42
|
+
|
43
|
+
# This utility method breaks apart the photo id into Flickr photo
|
44
|
+
# keys and returns a hash of the photo information
|
45
|
+
#
|
46
|
+
# NOTE: No sanitation check here
|
47
|
+
def self.hash_from_unique_id(uid)
|
48
|
+
p = uid.split("-")
|
49
|
+
{
|
50
|
+
:server=>p[1], :id=>p[2], :secret=>p[3],
|
51
|
+
:farm=>p[4], :size=>p[5], :type=>p[6]
|
52
|
+
}
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
def self.url_from_normalized_hash(p)
|
57
|
+
urlbase = (p[:farm] && p[:farm].to_s.length > 0) ? "http://farm#{p[:farm]}." : "http://"
|
58
|
+
url = "#{urlbase}#{@photo_url_base}/#{p[:server]}/#{p[:id]}_#{p[:secret]}"
|
59
|
+
url += "_#{p[:size]}" if p[:size].length > 0
|
60
|
+
url += ".#{p[:type]}"
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
def self.normalize_parameter(params)
|
65
|
+
{
|
66
|
+
:farm => (params[:farm] || params["farm"] || "").to_s,
|
67
|
+
:server => (params[:server] || params["server"] || "").to_s,
|
68
|
+
:id => (params[:id] || params["id"] || "").to_s,
|
69
|
+
:secret => (params[:secret] || params["secret"] || "").to_s,
|
70
|
+
:size => (params[:size] || params["size"] || "").to_s,
|
71
|
+
:type => (params[:type] || params["type"] || "jpg").to_s
|
72
|
+
}
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
@@ -7,16 +7,16 @@
|
|
7
7
|
# Copyright:: Copyright (c) 2006 Lukhnos D. Liu
|
8
8
|
# License:: Distributed under the New BSD License
|
9
9
|
|
10
|
-
# This class encapusulates Flickr's JSON response block. Error code
|
11
|
-
# and error messsage are read from the response block. This class
|
12
|
-
# is intended to be simple and minimalistic, since the data body
|
13
|
-
# can be extracted very easily.
|
14
|
-
|
15
10
|
require 'rubygems'
|
16
11
|
require 'json'
|
17
12
|
require 'jcode'
|
18
13
|
$KCODE = 'UTF8'
|
19
14
|
|
15
|
+
# This class encapusulates Flickr's JSON response block. Error code
|
16
|
+
# and error messsage are read from the response block. This class
|
17
|
+
# is intended to be simple and minimalistic, since the data body
|
18
|
+
# can be extracted very easily.
|
19
|
+
|
20
20
|
class FlickrResponse
|
21
21
|
attr_reader :data
|
22
22
|
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
require 'yaml'
|
2
3
|
|
3
4
|
class ObjectiveFlickrTest < Test::Unit::TestCase
|
4
5
|
|
@@ -38,4 +39,39 @@ class ObjectiveFlickrTest < Test::Unit::TestCase
|
|
38
39
|
assert(e.message.error?, "response should be an error")
|
39
40
|
assert_equal(e.message.error_message, "Invalid API Key (Key not found)", "error message should be 'invalid API key'")
|
40
41
|
end
|
42
|
+
|
43
|
+
def test_deprecated_photo_helpers
|
44
|
+
f = FlickrInvocation.new
|
45
|
+
params = {:server=>1234, :id=>5678, :secret=>90, :farm=>321}
|
46
|
+
assert_equal(f.photo_url(params), "http://farm321.static.flickr.com/1234/5678_90.jpg", "URL helper failed")
|
47
|
+
uid = f.photo_div_id(params)
|
48
|
+
assert_equal(uid, "photo-1234-5678-90-321--jpg", "UID failed")
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_photo_helpers
|
52
|
+
params = {:server=>"1234", :id=>"5678", :secret=>"90" }
|
53
|
+
|
54
|
+
assert_equal(FlickrPhoto.url_from_hash(params), "http://static.flickr.com/1234/5678_90.jpg", "URL helper failed")
|
55
|
+
params[:farm] = "321"
|
56
|
+
assert_equal(FlickrPhoto.url_from_hash(params), "http://farm321.static.flickr.com/1234/5678_90.jpg", "URL helper failed")
|
57
|
+
params[:farm] = nil
|
58
|
+
|
59
|
+
uid = FlickrPhoto.unique_id_from_hash(params, 'blah')
|
60
|
+
assert_equal(uid, "blah-1234-5678-90---jpg", "UID failed")
|
61
|
+
|
62
|
+
params[:farm] = "321"
|
63
|
+
params[:size] = 'b'
|
64
|
+
uid = FlickrPhoto.unique_id_from_hash(params, 'blah')
|
65
|
+
assert_equal(uid, "blah-1234-5678-90-321-b-jpg", "UID failed")
|
66
|
+
|
67
|
+
|
68
|
+
assert_equal(FlickrPhoto.url_from_unique_id(uid), "http://farm321.static.flickr.com/1234/5678_90_b.jpg", "URL helper failed")
|
69
|
+
|
70
|
+
params[:type] = 'jpg'
|
71
|
+
h = FlickrPhoto.hash_from_unique_id(uid)
|
72
|
+
assert_equal(h, params, "hash_from_unique_id failed")
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
|
41
77
|
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
|
|
3
3
|
specification_version: 1
|
4
4
|
name: objectiveflickr
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.9.
|
7
|
-
date: 2006-12-
|
6
|
+
version: 0.9.2
|
7
|
+
date: 2006-12-27 00:00:00 +08:00
|
8
8
|
summary: objectiveflickr is a minimalistic Flickr API library that uses REST-style calls and receives JSON response blocks, resulting in very concise code. Named so in order to echo another Flickr library of mine, under the same name, developed for Objective-C.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -37,6 +37,7 @@ files:
|
|
37
37
|
- lib/objectiveflickr.rb
|
38
38
|
- lib/objectiveflickr/version.rb
|
39
39
|
- lib/objectiveflickr/flickr_invocation.rb
|
40
|
+
- lib/objectiveflickr/flickr_photo.rb
|
40
41
|
- lib/objectiveflickr/flickr_response.rb
|
41
42
|
- test/test_helper.rb
|
42
43
|
- test/objectiveflickr_test.rb
|