DanaDanger-ruby_tubesday 0.1 → 0.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 (2) hide show
  1. data/lib/ruby_tubesday.rb +21 -7
  2. metadata +2 -2
data/lib/ruby_tubesday.rb CHANGED
@@ -36,6 +36,8 @@ class RubyTubesday
36
36
  # cache:: An instance of an ActiveSupport::Cache subclass to use
37
37
  # as a content cache. You can set this to false to disable
38
38
  # caching. Default is a new MemoryStore.
39
+ # force_cache:: An amount of time to cache requests, regardless of the
40
+ # request's cache control policy. Default is nil.
39
41
  # params:: Parameters to include in every request. For example, if
40
42
  # the service you're accessing requires an API key, you can
41
43
  # set it here and it will be included in every request made
@@ -53,18 +55,21 @@ class RubyTubesday
53
55
  # request.
54
56
  # password:: Username to send using basic authentication with every
55
57
  # request.
58
+ # headers:: Hash of HTTP headers to set for every request.
56
59
  #
57
60
  # All of these options can be overriden on a per-request basis.
58
61
  def initialize(options = {})
59
62
  @default_options = {
60
63
  :raw => false,
61
64
  :cache => ActiveSupport::Cache::MemoryStore.new,
65
+ :force_cache => nil,
62
66
  :params => {},
63
67
  :max_redirects => 5,
64
68
  :ca_file => (File.dirname(__FILE__) + '/../ca-bundle.crt'),
65
69
  :verify_ssl => true,
66
70
  :username => nil,
67
- :password => nil
71
+ :password => nil,
72
+ :headers => nil
68
73
  }
69
74
  @default_options = normalize_options(options)
70
75
  end
@@ -129,17 +134,20 @@ protected
129
134
  normalized_options = {
130
135
  :raw => options.delete(:raw),
131
136
  :cache => options.delete(:cache),
137
+ :force_cache => options.delete(:force_cache),
132
138
  :params => options.delete(:params) || @default_options[:params],
133
139
  :max_redirects => options.delete(:max_redirects) || @default_options[:max_redirects],
134
140
  :ca_file => options.delete(:ca_file) || @default_options[:ca_file],
135
141
  :verify_ssl => options.delete(:verify_ssl),
136
142
  :username => options.delete(:username) || @default_options[:username],
137
- :password => options.delete(:password) || @default_options[:password]
143
+ :password => options.delete(:password) || @default_options[:password],
144
+ :headers => options.delete(:headers) || @default_options[:headers]
138
145
  }
139
146
 
140
- normalized_options[:raw] = @default_options[:raw] if normalized_options[:raw].nil?
141
- normalized_options[:cache] = @default_options[:cache] if normalized_options[:cache].nil?
142
- normalized_options[:verify_ssl] = @default_options[:verify_ssl] if normalized_options[:verify_ssl].nil?
147
+ normalized_options[:raw] = @default_options[:raw] if normalized_options[:raw].nil?
148
+ normalized_options[:cache] = @default_options[:cache] if normalized_options[:cache].nil?
149
+ normalized_options[:force_cache] = @default_options[:force_cache] if normalized_options[:force_cache].nil?
150
+ normalized_options[:verify_ssl] = @default_options[:verify_ssl] if normalized_options[:verify_ssl].nil?
143
151
 
144
152
  unless options.empty?
145
153
  raise ArgumentError, "unrecognized keys: `#{options.keys.join('\', `')}'"
@@ -157,7 +165,7 @@ protected
157
165
  response = Marshal.load(options[:cache].read(url.to_s))
158
166
  response_age = Time.now - Time.parse(response['Last-Modified'] || response['Date'])
159
167
  cache_policy = CachePolicy.new(response['Cache-Control'], cache_policy_options)
160
- if cache_policy.fetch_action(response_age)
168
+ if (options[:force_cache] && (options[:force_cache] < response_age)) || cache_policy.fetch_action(response_age)
161
169
  response = nil
162
170
  end
163
171
  end
@@ -166,6 +174,12 @@ protected
166
174
  if response.nil?
167
175
  redirects_left = options[:max_redirects]
168
176
 
177
+ # Configure headers.
178
+ headers = options[:headers] || {}
179
+ headers.each do |key, value|
180
+ request[key] = value
181
+ end
182
+
169
183
  while !response.is_a?(Net::HTTPSuccess)
170
184
  client = Net::HTTP.new(url.host, url.port)
171
185
 
@@ -196,7 +210,7 @@ protected
196
210
  # Write the response to the cache if we're allowed to.
197
211
  if request.is_a?(Net::HTTP::Get) && options[:cache]
198
212
  cache_policy = CachePolicy.new(response['Cache-Control'], cache_policy_options)
199
- if cache_policy.may_cache?
213
+ if cache_policy.may_cache? || options[:force_cache]
200
214
  options[:cache].write(url.to_s, Marshal.dump(response))
201
215
  end
202
216
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: DanaDanger-ruby_tubesday
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.1"
4
+ version: "0.2"
5
5
  platform: ruby
6
6
  authors:
7
7
  - DanaDanger
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-08-13 00:00:00 -07:00
12
+ date: 2008-10-08 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency