httparty_sober 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Michael Bumann
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,18 @@
1
+ = httparty_sober
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but
13
+ bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2009 Michael Bumann. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,57 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "httparty_sober"
8
+ gem.summary = %Q{HTTParties sober. Adds api_cache to httparty}
9
+ gem.description = %Q{Cache responses in HTTParty using APICache}
10
+ gem.email = "michael@railslove.com"
11
+ gem.homepage = "http://github.com/bumi/httparty_sober"
12
+ gem.authors = ["Michael Bumann"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ gem.add_development_dependency "mocha"
15
+ gem.add_dependency "httparty"
16
+ gem.add_dependency "api_cache", ">=0.2.0"
17
+ gem.files = FileList['lib/**/*.rb', '[A-Z]*', 'test/**/*'].to_a
18
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
19
+ end
20
+ Jeweler::GemcutterTasks.new
21
+ rescue LoadError
22
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
23
+ end
24
+
25
+ require 'rake/testtask'
26
+ Rake::TestTask.new(:test) do |test|
27
+ test.libs << 'lib' << 'test'
28
+ test.pattern = 'test/**/test_*.rb'
29
+ test.verbose = true
30
+ end
31
+
32
+ begin
33
+ require 'rcov/rcovtask'
34
+ Rcov::RcovTask.new do |test|
35
+ test.libs << 'test'
36
+ test.pattern = 'test/**/test_*.rb'
37
+ test.verbose = true
38
+ end
39
+ rescue LoadError
40
+ task :rcov do
41
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
42
+ end
43
+ end
44
+
45
+ task :test => :check_dependencies
46
+
47
+ task :default => :test
48
+
49
+ require 'rake/rdoctask'
50
+ Rake::RDocTask.new do |rdoc|
51
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
52
+
53
+ rdoc.rdoc_dir = 'rdoc'
54
+ rdoc.title = "httparty_sober #{version}"
55
+ rdoc.rdoc_files.include('README*')
56
+ rdoc.rdoc_files.include('lib/**/*.rb')
57
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,34 @@
1
+ module HTTParty
2
+ module Sober
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ @@cache_store = nil
9
+ @@cache_options = {}
10
+
11
+ def cache(cache_options={})
12
+ @@cache_store = cache_options.delete(:store)
13
+ APICache.store = @@cache_store if @@cache_store
14
+ @@cache_options = cache_options
15
+ end
16
+
17
+ def cache!(cache_options={})
18
+ cache(cache_options)
19
+ self.instance_eval do
20
+ alias :get_without_caching :get
21
+ alias :get :get_with_caching
22
+ end
23
+ end
24
+
25
+ def get_with_caching(path, options={})
26
+ cache_options = (@@cache_options || {}).merge(options[:cache] || {})
27
+ APICache.get(path,cache_options) do
28
+ perform_request(Net::HTTP::Get, path, options)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+
@@ -0,0 +1,3 @@
1
+ require "httparty"
2
+ require "api_cache"
3
+ require "httparty/sober"
data/test/helper.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+ require 'httparty'
6
+ require 'rubygems'
7
+ require 'api_cache'
8
+
9
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
10
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
11
+ require 'httparty_sober'
12
+
13
+ class Test::Unit::TestCase
14
+ end
15
+
16
+ class CacheParty
17
+ include HTTParty
18
+ include HTTParty::Sober
19
+ end
@@ -0,0 +1,40 @@
1
+ require 'helper'
2
+
3
+ class TestHttpartySober < Test::Unit::TestCase
4
+
5
+ should "define caching class methods when included" do
6
+ assert CacheParty.respond_to?("get_with_caching")
7
+ assert CacheParty.respond_to?("cache")
8
+ assert CacheParty.respond_to?("cache!")
9
+ end
10
+
11
+ should "set APICache.store" do
12
+ APICache.expects(:store=).with("cache_store")
13
+ CacheParty.cache :store => "cache_store"
14
+ end
15
+
16
+ should "save and use default caching options" do
17
+ options = {:cache =>123, :valid => 123}
18
+ CacheParty.cache options
19
+ APICache.expects(:get).with("url", options)
20
+ CacheParty.get_with_caching "url"
21
+ end
22
+
23
+ context "force caching" do
24
+ should "alias get to get_with_caching" do
25
+ klass = class OverwriteGet; include HTTParty; include HTTParty::Sober; end
26
+ klass.cache!
27
+ APICache.expects(:get).with("url", {}).returns("response")
28
+ assert_equal "response", klass.get("url")
29
+ end
30
+
31
+ should "alias original get class method to get_without_caching" do
32
+ klass = class OriginalGet; include HTTParty; include HTTParty::Sober; end
33
+ APICache.expects(:get).never
34
+ klass.expects(:perform_request).with(Net::HTTP::Get, "url", {}).returns("response")
35
+ klass.cache!
36
+ assert_equal "response", klass.get_without_caching("url")
37
+ end
38
+ end
39
+
40
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: httparty_sober
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Bumann
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-16 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: mocha
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: httparty
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: api_cache
47
+ type: :runtime
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 0.2.0
54
+ version:
55
+ description: Cache responses in HTTParty using APICache
56
+ email: michael@railslove.com
57
+ executables: []
58
+
59
+ extensions: []
60
+
61
+ extra_rdoc_files:
62
+ - LICENSE
63
+ - README.rdoc
64
+ files:
65
+ - LICENSE
66
+ - README.rdoc
67
+ - Rakefile
68
+ - VERSION
69
+ - lib/httparty/sober.rb
70
+ - lib/httparty_sober.rb
71
+ - test/helper.rb
72
+ - test/test_httparty_sober.rb
73
+ has_rdoc: true
74
+ homepage: http://github.com/bumi/httparty_sober
75
+ licenses: []
76
+
77
+ post_install_message:
78
+ rdoc_options:
79
+ - --charset=UTF-8
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: "0"
87
+ version:
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: "0"
93
+ version:
94
+ requirements: []
95
+
96
+ rubyforge_project:
97
+ rubygems_version: 1.3.5
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: HTTParties sober. Adds api_cache to httparty
101
+ test_files:
102
+ - test/helper.rb
103
+ - test/test_httparty_sober.rb