activeresource-conditional-get 0.0.1

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.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm ruby-1.9.2-p290@activeresource-conditional-get
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ gem "activeresource", ">= 2.3.10"
4
+
5
+ # Add dependencies to develop your gem here.
6
+ # Include everything needed to run rake, tests, features, etc.
7
+ group :development do
8
+ gem "rdoc", "~> 3.12"
9
+ gem "bundler"
10
+ gem "jeweler", "~> 1.8.3"
11
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Sergey Rozum
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.
@@ -0,0 +1,47 @@
1
+ = activeresource-conditional-get
2
+
3
+ HTTP conditional get support for ActiveResource.
4
+
5
+ == Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'activeresource-conditional-get'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install activeresource-conditional-get
18
+
19
+ == Usage
20
+
21
+ Works out of box after adding:
22
+
23
+ ActiveResource::Base.cache = Rails.cache
24
+
25
+ Or
26
+
27
+ ActiveResource::Base.cache = ActiveSupport::Cache.lookup_store(:mem_cache_store)
28
+
29
+
30
+ == References
31
+
32
+ * [activeresource](https://github.com/rails/activeresource)
33
+ * [HTTP Conditional Get](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html)
34
+
35
+ == Contributing to activeresource-conditional-get
36
+
37
+ * Fork it
38
+ * Create your feature branch (`git checkout -b my-new-feature`)
39
+ * Commit your changes (`git commit -am 'Added some feature'`)
40
+ * Push to the branch (`git push origin my-new-feature`)
41
+ * Create new Pull Request
42
+
43
+ == Copyright
44
+
45
+ Copyright (c) 2012 Sergey Rozum. See LICENSE.txt for
46
+ further details.
47
+
@@ -0,0 +1,46 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "activeresource-conditional-get"
18
+ gem.homepage = "http://github.com/srozum/activeresource-conditional-get"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{HTTP conditional get support for ActiveResource}
21
+ gem.description = %Q{HTTP conditional get support for ActiveResource}
22
+ gem.email = "sergey.rozum@gmail.com"
23
+ gem.authors = ["Sergey Rozum"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ task :default => :test
37
+
38
+ require 'rdoc/task'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "activeresource-conditional-get #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,62 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "activeresource-conditional-get"
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Sergey Rozum"]
12
+ s.date = "2012-06-06"
13
+ s.description = "HTTP conditional get support for ActiveResource"
14
+ s.email = "sergey.rozum@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rvmrc",
22
+ "Gemfile",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "activeresource-conditional-get.gemspec",
28
+ "lib/active_resource/conditional_get.rb",
29
+ "lib/active_resource/conditional_get/base.rb",
30
+ "lib/active_resource/conditional_get/connection.rb",
31
+ "lib/activeresource_conditional_get.rb",
32
+ "test/helper.rb",
33
+ "test/test_activeresource-conditional-get.rb"
34
+ ]
35
+ s.homepage = "http://github.com/srozum/activeresource-conditional-get"
36
+ s.licenses = ["MIT"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = "1.8.21"
39
+ s.summary = "HTTP conditional get support for ActiveResource"
40
+
41
+ if s.respond_to? :specification_version then
42
+ s.specification_version = 3
43
+
44
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
45
+ s.add_runtime_dependency(%q<activeresource>, [">= 2.3.10"])
46
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
47
+ s.add_development_dependency(%q<bundler>, [">= 0"])
48
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
49
+ else
50
+ s.add_dependency(%q<activeresource>, [">= 2.3.10"])
51
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
52
+ s.add_dependency(%q<bundler>, [">= 0"])
53
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<activeresource>, [">= 2.3.10"])
57
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
58
+ s.add_dependency(%q<bundler>, [">= 0"])
59
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
60
+ end
61
+ end
62
+
@@ -0,0 +1,2 @@
1
+ require 'active_resource/conditional_get/base'
2
+ require 'active_resource/conditional_get/connection'
@@ -0,0 +1,32 @@
1
+ require 'active_resource/base'
2
+
3
+ module ActiveResource
4
+ class Base
5
+
6
+ cattr_accessor :cache
7
+
8
+ class << self
9
+
10
+ def connection_with_cache(refresh = false)
11
+ if defined?(@connection) || superclass == Object
12
+ @connection = Connection.new(site, format) if refresh || @connection.nil?
13
+ @connection.proxy = proxy if proxy
14
+ @connection.user = user if user
15
+ @connection.password = password if password
16
+ @connection.auth_type = auth_type if auth_type
17
+ @connection.timeout = timeout if timeout
18
+ @connection.ssl_options = ssl_options if ssl_options
19
+ @connection.cache = cache if cache
20
+ @connection
21
+ else
22
+ superclass.connection
23
+ end
24
+ end
25
+
26
+ alias_method :connection_without_cache, :connection
27
+ alias_method :connection, :connection_with_cache
28
+
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,42 @@
1
+ require 'active_resource/connection'
2
+
3
+ module ActiveResource
4
+ class Connection
5
+ extend ActiveSupport::Concern
6
+
7
+ attr_accessor :cache
8
+
9
+ def get_with_conditional_get(path, headers = {})
10
+ return get_without_conditional_get(path, headers) unless cache
11
+ begin
12
+ cached_response = cache.read(cache_key(path))
13
+ if cached_response
14
+ headers.update('If-None-Match' => cached_response['eTag']) if cached_response['eTag']
15
+ headers.update('If-Modified-Since' => cached_response['Cache-Control']) if cached_response['Cache-Control']
16
+ end
17
+
18
+ response = get_without_conditional_get(path, headers)
19
+
20
+ return cached_response if (response.try(:code).to_i == 304) && cached_response
21
+
22
+ if (response['Cache-Control'] =~ /public/) && (response['eTag'] || response['Last-Modified'])
23
+ if response['Cache-Control'] =~ /max-age=(\d+)/ && $1.to_i > 0
24
+ cache.write(cache_key(path), response, :expires_in => $1.to_i)
25
+ end
26
+ end
27
+ return response
28
+ rescue ActiveResource::TimeoutError => e
29
+ cached_response || raise(e)
30
+ end
31
+ end
32
+
33
+ alias_method_chain :get, :conditional_get
34
+
35
+ private
36
+
37
+ def cache_key(path)
38
+ 'conditional_get:' + Digest::MD5.hexdigest(path)
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,5 @@
1
+ unless defined?(ActiveResource)
2
+ require 'active_resource'
3
+ end
4
+
5
+ require 'active_resource/conditional_get'
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+
12
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ require 'activeresource-conditional-get'
15
+
16
+ class Test::Unit::TestCase
17
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestActiveresourceConditionalGet < Test::Unit::TestCase
4
+ def test_something_for_real
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activeresource-conditional-get
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sergey Rozum
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activeresource
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.3.10
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 2.3.10
30
+ - !ruby/object:Gem::Dependency
31
+ name: rdoc
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '3.12'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '3.12'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: jeweler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.8.3
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.8.3
78
+ description: HTTP conditional get support for ActiveResource
79
+ email: sergey.rozum@gmail.com
80
+ executables: []
81
+ extensions: []
82
+ extra_rdoc_files:
83
+ - LICENSE.txt
84
+ - README.rdoc
85
+ files:
86
+ - .document
87
+ - .rvmrc
88
+ - Gemfile
89
+ - LICENSE.txt
90
+ - README.rdoc
91
+ - Rakefile
92
+ - VERSION
93
+ - activeresource-conditional-get.gemspec
94
+ - lib/active_resource/conditional_get.rb
95
+ - lib/active_resource/conditional_get/base.rb
96
+ - lib/active_resource/conditional_get/connection.rb
97
+ - lib/activeresource_conditional_get.rb
98
+ - test/helper.rb
99
+ - test/test_activeresource-conditional-get.rb
100
+ homepage: http://github.com/srozum/activeresource-conditional-get
101
+ licenses:
102
+ - MIT
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ segments:
114
+ - 0
115
+ hash: -2584738020901776792
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ! '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 1.8.21
125
+ signing_key:
126
+ specification_version: 3
127
+ summary: HTTP conditional get support for ActiveResource
128
+ test_files: []