benschwarz-flickr-rest 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/flickr-rest.gemspec +1 -1
  2. data/lib/flickr-rest.rb +25 -13
  3. metadata +1 -1
data/flickr-rest.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "flickr-rest"
3
- s.version = "0.2.1"
3
+ s.version = "0.2.2"
4
4
  s.date = "2008-11-17"
5
5
  s.summary = "A light interface to call flickr 'restful' api methods"
6
6
  s.email = "ben@germanforblack.com"
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.1".freeze
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
- name, response = response.to_a.first if response.size == 1
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 unnest(h)
43
- return h unless h.is_a? Hash
44
- if h.keys == ["_content"]
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
- h.inject({}) do |h, kv|
48
- k,v = *kv
49
- h[k.to_sym] = unnest(v)
50
- h
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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: benschwarz-flickr-rest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Schwarz