Parent

Rufus::Jig::HttpCore

The base for the Rufus::Jig::Http class.

Attributes

last_response[R]

mostly for debugging purposes

cache[R]

the path => [ etag, decoded_body] client cache

options[R]

the options for the http client

host[R]

host and port, vanilla

port[R]

host and port, vanilla

error_class[RW]

The class of the error that should be raised when a request is not 2xx.

Public Class Methods

new(host, port, opts) click to toggle source
     # File lib/rufus/jig/http.rb, line 134
134:     def initialize (host, port, opts)
135: 
136:       @host = host
137:       @port = port
138: 
139:       @options = opts.dup
140: 
141:       @cache = LruHash.new((opts[:cache_size] || 35).to_i)
142: 
143:       if pf = @options[:prefix]
144:         @options[:prefix] = "/#{pf}" if (not pf.match(/^\//))
145:       end
146: 
147:       @error_class = opts[:error_class] || HttpError
148:     end

Public Instance Methods

close() click to toggle source
     # File lib/rufus/jig/http.rb, line 150
150:     def close
151: 
152:       # default implementation does nothing
153:     end
delete(path, opts={}) click to toggle source
     # File lib/rufus/jig/http.rb, line 170
170:     def delete (path, opts={})
171: 
172:       request(:delete, path, nil, opts)
173:     end
get(path, opts={}) click to toggle source
     # File lib/rufus/jig/http.rb, line 155
155:     def get (path, opts={})
156: 
157:       request(:get, path, nil, opts)
158:     end
post(path, data, opts={}) click to toggle source
     # File lib/rufus/jig/http.rb, line 160
160:     def post (path, data, opts={})
161: 
162:       request(:post, path, data, opts)
163:     end
put(path, data, opts={}) click to toggle source
     # File lib/rufus/jig/http.rb, line 165
165:     def put (path, data, opts={})
166: 
167:       request(:put, path, data, opts)
168:     end

Protected Instance Methods

add_params(path, opts) click to toggle source
     # File lib/rufus/jig/http.rb, line 278
278:     def add_params (path, opts)
279: 
280:       if params = opts[:params]
281: 
282:         return path if params.empty?
283: 
284:         params = params.inject([]) { |a, (k, v)|
285:           a << "#{k}=#{v}"; a
286:         }.join("&")
287: 
288:         return path.index('?') ? "#{path}&#{params}" : "#{path}?#{params}"
289:       end
290: 
291:       path
292:     end
add_prefix(path, opts) click to toggle source
     # File lib/rufus/jig/http.rb, line 263
263:     def add_prefix (path, opts)
264: 
265:       host = Rufus::Jig.parse_host(path)
266: 
267:       return path if host
268: 
269:       elts = [ path ]
270: 
271:       if path.match(/^[^\/]/) && prefix = @options[:prefix]
272:         elts.unshift(prefix)
273:       end
274: 
275:       Path.join(*elts)
276:     end
decode_body(response, opts) click to toggle source
     # File lib/rufus/jig/http.rb, line 324
324:     def decode_body (response, opts)
325: 
326:       b = response.body
327:       ct = response.headers['Content-Type']
328: 
329:       if opts[:force_json] || (ct && ct.match(/^application\/json/))
330:         Rufus::Json.decode(b)
331:       else
332:         b
333:       end
334:     end
do_cache(method, path, response, body, opts) click to toggle source
     # File lib/rufus/jig/http.rb, line 315
315:     def do_cache (method, path, response, body, opts)
316: 
317:       if (method == :delete) || (opts[:cache] == false)
318:         @cache.delete(path)
319:       elsif (method == :get) && (et = response.headers['Etag'])
320:         @cache[path] = [ et, Rufus::Jig.marshal_copy(body) ]
321:       end
322:     end
from_cache(path, opts) click to toggle source
     # File lib/rufus/jig/http.rb, line 177
177:     def from_cache (path, opts)
178: 
179:       if et = opts[:etag]
180: 
181:         cached = @cache[path]
182: 
183:         if cached && cached.first != et
184:           #
185:           # cached version is perhaps stale
186:           #
187:           cached = nil
188:           opts.delete(:etag)
189:         end
190: 
191:         cached
192: 
193:       else
194: 
195:         nil
196:       end
197:     end
raw_expected?(opts) click to toggle source
     # File lib/rufus/jig/http.rb, line 235
235:     def raw_expected? (opts)
236: 
237:       raw = opts[:raw]
238: 
239:       raw == false ? false : raw || @options[:raw]
240:     end
rehash_options(opts) click to toggle source

Should work with GET and POST/PUT options

     # File lib/rufus/jig/http.rb, line 244
244:     def rehash_options (opts)
245: 
246:       opts['Accept'] ||= (opts.delete(:accept) || 'application/json')
247:       opts['Accept'] = 'application/json' if opts['Accept'] == :json
248: 
249:       if ct = opts.delete(:content_type)
250:         opts['Content-Type'] = ct
251:       end
252:       if opts['Content-Type'] == :json
253:         opts['Content-Type'] = 'application/json'
254:       end
255: 
256:       if et = opts.delete(:etag)
257:         opts['If-None-Match'] = et
258:       end
259: 
260:       opts
261:     end
repack_data(data, opts) click to toggle source
     # File lib/rufus/jig/http.rb, line 294
294:     def repack_data (data, opts)
295: 
296:       return nil unless data
297: 
298: 
299:       data = if data.is_a?(String)
300:         data
301:       elsif (opts['Content-Type'] || '').match(/^application\/json/)
302:         Rufus::Json.encode(data)
303:       else
304:         data.to_s
305:       end
306: 
307:       #opts['Content-Length'] =
308:       #  (data.respond_to?(:bytesize) ? data.bytesize : data.size).to_s
309:         #
310:         # Patron doesn't play well with custom lengths : "56, 56" issue
311: 
312:       data
313:     end
request(method, path, data, opts={}) click to toggle source
     # File lib/rufus/jig/http.rb, line 199
199:     def request (method, path, data, opts={})
200: 
201:       raw = raw_expected?(opts)
202: 
203:       path = add_prefix(path, opts)
204:       path = add_params(path, opts)
205: 
206:       path = '/' if path == ''
207: 
208:       cached = from_cache(path, opts)
209:       opts.delete(:etag) if not cached
210: 
211:       opts = rehash_options(opts)
212:       data = repack_data(data, opts)
213: 
214:       r = do_request(method, path, data, opts)
215: 
216:       @last_response = r
217: 
218:       unless raw
219: 
220:         return Rufus::Jig.marshal_copy(cached.last) if r.status == 304
221:         return nil if method == :get && r.status == 404
222:         return true if [ 404, 409 ].include?(r.status)
223: 
224:         raise @error_class.new(r.status, r.body)            if r.status >= 400 && r.status < 600
225:       end
226: 
227:       b = decode_body(r, opts)
228: 
229:       do_cache(method, path, r, b, opts)
230: 
231:       raw ? r : b
232:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.