httparty_sober 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,31 @@
1
1
  = httparty_sober
2
2
 
3
- Description goes here.
3
+ makes you not get drunken when httpartying.
4
+
5
+ == Description
6
+
7
+ httparty_sober integrates mloughran's APICache into HTTParty. This allows you to easily cache your HTTP requests.
8
+ Have a look at the APICache readme for the cache details:
9
+
10
+ http://github.com/mloughran/api_cache
11
+
12
+ == Usage Example:
13
+
14
+ class CacheParty
15
+ include HTTParty
16
+ include HTTParty::Sober
17
+
18
+ # setup APICache.
19
+ # the cache method accepts a :store parameter and default APICache options (like :valid, :timeout,...)
20
+ # you can also use the bang method cache! which overwrites the default HTTParty#get method and forces caching.
21
+ cache :store => Moneta::Memcache.new(:server => "localhost")
22
+ end
23
+
24
+ CacheParty.get_with_caching "/foo"
25
+ # get_with_caching acts like HTTParty's #get but caches the result
26
+
27
+
28
+
4
29
 
5
30
  == Note on Patches/Pull Requests
6
31
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
@@ -1,3 +1,4 @@
1
+ require 'digest/md5'
1
2
  module HTTParty
2
3
  module Sober
3
4
  def self.included(base)
@@ -24,7 +25,8 @@ module HTTParty
24
25
 
25
26
  def get_with_caching(path, options={})
26
27
  cache_options = (@@cache_options || {}).merge(options[:cache] || {})
27
- key = "#{path.to_s}#{options[:query].to_s}"
28
+ query = options[:query] ||= {}
29
+ key = Digest::MD5.hexdigest("#{path.to_s}-#{query.collect{|e| e.join("=")}.join("&")}")
28
30
  APICache.get(key,cache_options) do
29
31
  perform_request(Net::HTTP::Get, path, options)
30
32
  end
@@ -34,7 +34,8 @@ class TestHttpartySober < Test::Unit::TestCase
34
34
  CacheKey.cache
35
35
  url = "/params"
36
36
  query = {:hey=> "ho", :lets => "go"}
37
- key = "#{url.to_s}#{query.to_s}"
37
+
38
+ key = md5_key(url,query)
38
39
  APICache.expects(:get).with(key,{})
39
40
  CacheKey.get_with_caching url, {:query => query}
40
41
  end
@@ -42,7 +43,7 @@ class TestHttpartySober < Test::Unit::TestCase
42
43
  should "save and use default caching options" do
43
44
  options = {:cache =>123, :valid => 123}
44
45
  CacheParty.cache options
45
- APICache.expects(:get).with("url", options)
46
+ APICache.expects(:get).with(md5_key("url"), options)
46
47
  CacheParty.get_with_caching "url"
47
48
  end
48
49
 
@@ -50,7 +51,7 @@ class TestHttpartySober < Test::Unit::TestCase
50
51
  should "alias get to get_with_caching" do
51
52
  klass = class OverwriteGet; include HTTParty; include HTTParty::Sober; end
52
53
  klass.cache!
53
- APICache.expects(:get).with("url", {}).returns("response")
54
+ APICache.expects(:get).with(md5_key("url"), {}).returns("response")
54
55
  assert_equal "response", klass.get("url")
55
56
  end
56
57
 
@@ -63,4 +64,7 @@ class TestHttpartySober < Test::Unit::TestCase
63
64
  end
64
65
  end
65
66
 
67
+ def md5_key(url,query={})
68
+ Digest::MD5.hexdigest("#{url.to_s}-#{query.collect{|e| e.join("=")}.join("&")}")
69
+ end
66
70
  end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: httparty_sober
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 1
9
+ version: 0.2.1
5
10
  platform: ruby
6
11
  authors:
7
12
  - Michael Bumann
@@ -9,49 +14,59 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-03-20 00:00:00 +01:00
17
+ date: 2010-05-12 00:00:00 +02:00
13
18
  default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: thoughtbot-shoulda
17
- type: :development
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
20
24
  requirements:
21
25
  - - ">="
22
26
  - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
23
29
  version: "0"
24
- version:
30
+ type: :development
31
+ version_requirements: *id001
25
32
  - !ruby/object:Gem::Dependency
26
33
  name: mocha
27
- type: :development
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
30
36
  requirements:
31
37
  - - ">="
32
38
  - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
33
41
  version: "0"
34
- version:
42
+ type: :development
43
+ version_requirements: *id002
35
44
  - !ruby/object:Gem::Dependency
36
45
  name: httparty
37
- type: :runtime
38
- version_requirement:
39
- version_requirements: !ruby/object:Gem::Requirement
46
+ prerelease: false
47
+ requirement: &id003 !ruby/object:Gem::Requirement
40
48
  requirements:
41
49
  - - ">="
42
50
  - !ruby/object:Gem::Version
51
+ segments:
52
+ - 0
43
53
  version: "0"
44
- version:
54
+ type: :runtime
55
+ version_requirements: *id003
45
56
  - !ruby/object:Gem::Dependency
46
57
  name: api_cache
47
- type: :runtime
48
- version_requirement:
49
- version_requirements: !ruby/object:Gem::Requirement
58
+ prerelease: false
59
+ requirement: &id004 !ruby/object:Gem::Requirement
50
60
  requirements:
51
61
  - - ">="
52
62
  - !ruby/object:Gem::Version
63
+ segments:
64
+ - 0
65
+ - 2
66
+ - 0
53
67
  version: 0.2.0
54
- version:
68
+ type: :runtime
69
+ version_requirements: *id004
55
70
  description: Cache responses in HTTParty using APICache
56
71
  email: michael@railslove.com
57
72
  executables: []
@@ -83,18 +98,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
83
98
  requirements:
84
99
  - - ">="
85
100
  - !ruby/object:Gem::Version
101
+ segments:
102
+ - 0
86
103
  version: "0"
87
- version:
88
104
  required_rubygems_version: !ruby/object:Gem::Requirement
89
105
  requirements:
90
106
  - - ">="
91
107
  - !ruby/object:Gem::Version
108
+ segments:
109
+ - 0
92
110
  version: "0"
93
- version:
94
111
  requirements: []
95
112
 
96
113
  rubyforge_project:
97
- rubygems_version: 1.3.5
114
+ rubygems_version: 1.3.6
98
115
  signing_key:
99
116
  specification_version: 3
100
117
  summary: HTTParties sober. Adds api_cache to httparty