benschwarz-flickr-rest 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/flickr-rest.gemspec +1 -1
- data/lib/flickr-rest.rb +25 -13
- metadata +1 -1
data/flickr-rest.gemspec
CHANGED
data/lib/flickr-rest.rb
CHANGED
@@ -4,7 +4,7 @@ require 'open-uri'
|
|
4
4
|
|
5
5
|
module Flickr
|
6
6
|
class Query
|
7
|
-
VERSION = "0.2.
|
7
|
+
VERSION = "0.2.2".freeze
|
8
8
|
API_BASE = "http://api.flickr.com/services/rest/".freeze
|
9
9
|
|
10
10
|
class Failure < StandardError; end
|
@@ -19,10 +19,9 @@ module Flickr
|
|
19
19
|
|
20
20
|
def request(api_method, params = {})
|
21
21
|
response = JSON.parse(open(build_query(api_method, params)).read)
|
22
|
-
raise Failure, response["message"] if response.delete("stat") == "fail"
|
23
|
-
|
24
|
-
|
25
|
-
unnest(response)
|
22
|
+
raise Failure, response["message"] if response.delete("stat") == "fail"
|
23
|
+
r = mash(response)
|
24
|
+
unnest(r)
|
26
25
|
end
|
27
26
|
|
28
27
|
private
|
@@ -39,16 +38,29 @@ module Flickr
|
|
39
38
|
return API_BASE + "?" + url.join("&")
|
40
39
|
end
|
41
40
|
|
42
|
-
def
|
43
|
-
|
44
|
-
|
45
|
-
h["_content"]
|
41
|
+
def mash(h)
|
42
|
+
if h.size == 1 and h.is_a? Hash
|
43
|
+
return mash(h.delete(h.keys.first))
|
46
44
|
else
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
45
|
+
return h
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def unnest(h)
|
50
|
+
if h.is_a? Hash
|
51
|
+
if h.keys == ["_content"]
|
52
|
+
h["_content"]
|
53
|
+
else
|
54
|
+
h.inject({}) do |h, kv|
|
55
|
+
k,v = *kv
|
56
|
+
h[k.to_sym] = unnest(v)
|
57
|
+
h
|
58
|
+
end
|
51
59
|
end
|
60
|
+
elsif h.is_a? Array
|
61
|
+
h.collect{|i| unnest(i) }
|
62
|
+
else
|
63
|
+
h
|
52
64
|
end
|
53
65
|
end
|
54
66
|
|