myflickr 0.0.6 → 0.1.0

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.
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ == 0.1.0 2008-01-06
2
+
3
+ * Added support for memcached so as to reduce the amount of queries actually sent to flickr
4
+
1
5
  == 0.0.6 2007-12-31
2
6
 
3
7
  * Updated gem specs for better understanding of what the gem does
data/lib/myflickr.rb CHANGED
@@ -8,6 +8,6 @@ module Myflickr
8
8
  MAX_THREADS = 100
9
9
  end
10
10
 
11
- %w(query photo tag size set machine_tag version).each do |r|
11
+ %w(query photo tag size set machine_tag cache version).each do |r|
12
12
  require File.join(File.dirname(__FILE__), './myflickr/' + r)
13
13
  end
@@ -1,10 +1,54 @@
1
+ # All memcached references have been stolen straight
2
+ # from the implementation in the `rubyweather` gem.
3
+ # It just seemed too simple to roll in.
4
+
1
5
  module Myflickr
2
6
  class Query #:nodoc:
3
7
  def self.api_call(method, resource_uri='') #:nodoc:
4
8
  resource = "#{API_BASE}/?method=#{method}&api_key=#{API_KEY}&user_id=#{USER_ID}"
5
9
  resource += "&#{resource_uri}" unless resource_uri.empty?
6
- puts "Querying: #{resource}"
7
- Hpricot.XML(open(resource))
10
+
11
+ if cache?
12
+ begin
13
+ document = cache.get "#{USER_ID}:#{method}:#{resource_uri}"
14
+ rescue
15
+ puts "Querying: #{resource} [CACHE MISS]"
16
+ document = false
17
+ end
18
+ end
19
+
20
+ # No cached version exists
21
+ unless document
22
+ puts "Querying: #{resource}"
23
+
24
+ # Query the resource
25
+ document = open(resource).read
26
+
27
+ # Write a new cache if available
28
+ cache.set("#{USER_ID}:#{method}:#{resource_uri}", document, cache_validity) if cache?
29
+ puts "[CACHING...]" if cache?
30
+ end
31
+
32
+ # Create a Hpricot object
33
+ Hpricot.XML(document)
34
+ end
35
+
36
+ @cache = false
37
+
38
+ # Turns on query caching.
39
+ # See Myflickr::Query::Cache
40
+ def self.enable_cache(enable = true)
41
+ if enable
42
+ extend Cache
43
+ @cache = true
44
+ else
45
+ @cache = false
46
+ end
47
+ end
48
+
49
+ # True if caching is enabled and at least one memcached server is alive, false otherwise.
50
+ def self.cache?
51
+ @cache and cache.active? and servers = cache.instance_variable_get(:@servers) and servers.collect{|s| s.alive?}.include?(true)
8
52
  end
9
53
  end
10
54
  end
@@ -1,8 +1,8 @@
1
1
  module Myflickr
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
- MINOR = 0
5
- TINY = 6
4
+ MINOR = 1
5
+ TINY = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
data/spec/query_spec.rb CHANGED
@@ -4,4 +4,13 @@ describe Query, "class" do
4
4
  it "should return a Hpricot Document object response" do
5
5
  Query.api_call('flickr.test.echo').should be_an_instance_of Hpricot::Doc
6
6
  end
7
+ end
8
+
9
+ describe Query, "with memcached caching for queries" do
10
+ it "should return a Hpricot Document object response" do
11
+ Query.enable_cache true
12
+ Query.cache.servers = ["127.0.0.1:11211"]
13
+ Query.api_call('flickr.test.echo').should be_an_instance_of Hpricot::Doc
14
+ end
15
+
7
16
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: myflickr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Schwarz
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2007-12-31 00:00:00 +11:00
12
+ date: 2008-01-06 00:00:00 +11:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency