lib/rhack/frame.rb in rhack-1.2.8 vs lib/rhack/frame.rb in rhack-1.2.9
- old
+ new
@@ -29,11 +29,11 @@
#args.insert -2, {} unless args[-2].is Hash
#opts = args[-2]
#if scouts = (opts[:scouts] || opts[:threads])
# args[-1] = scouts
#end
- opts = args.find_by_class Hash
+ opts = args.find_by_class(Hash) || {}
scouts_count = opts[:scouts] || opts[:threads] || 10
@opts = {:eval => Johnson::Enabled, :redir => true, :cp => true, :result => Page}.merge!(opts)
if args[0].is String
url = args[0].dup
'http://' >> url if url !~ /^\w+:\/\//
@@ -121,11 +121,12 @@
def interpret_request(*args)
body, mp, url, opts = args.dup.get_opts [nil, false, nil], @opts
L.log [body, mp, url, opts]
zip = opts.delete :zip
verb = opts.delete :verb
- post = put = verb == :put
+ put = verb == :put
+ post = put || verb == :post
many = order = orders = false
if put
# If request is PUT then first argument is always body
if mp.is String
@@ -140,13 +141,17 @@
# if second arg is String then it's URL
url, mp, post = mp.dup, false, true
# L.debug "URL #{url.inspect} has been passed as second argument instead of third"
# But if we have only one argument actually passed
# except for options hash then believe it's GET
- elsif body.is String or body.kinda [String]
- L.debug "first parameter (#{body.inspect}) was implicitly taken as url#{' '+body.class if body.kinda Array}, but last paramter is of type #{url.class}, too" if url
- url = body.dup
+ elsif body.is String or body.kinda [String] # mp is boolean
+ if post
+ url = url.dup if url
+ else
+ L.debug "first parameter (#{body.inspect}) was implicitly taken as url#{' '+body.class if body.kinda Array}, but last paramter is of type #{url.class}, too" if url
+ url = body.dup
+ end
elsif !body
url = nil
else
url = url.dup if url
mp, post = !!mp, true
@@ -179,12 +184,12 @@
unless body.is String
raise TypeError, "body of PUT request must be a string, params was
(#{args.inspect[1..-2]})"
end
else
- unless body.is Hash
- raise TypeError, "body of POST request must be a hash, params was
+ unless body.is Hash or body.is String
+ raise TypeError, "body of POST request must be a hash or a string params was
(#{args.inspect[1..-2]})"
end
end
url = validate url
@@ -214,14 +219,18 @@
opts[:load_scripts] = self if opts[:load_scripts]
opts[:stream] = true if opts[:raw]
(opts[:headers] ||= {})['X-Requested-With'] = 'XMLHttpRequest' if opts[:xhr]
if opts[:content_type]
- if mime_type = Mime::Type.lookup_by_extension(opts[:content_type])
- (opts[:headers] ||= {})['Content-Type'] = mime_type
+ if opts[:content_type].is Symbol
+ if mime_type = Mime::Types.of(opts[:content_type])[0]
+ (opts[:headers] ||= {})['Content-Type'] = mime_type.content_type
+ else
+ raise ArgumentError, "failed to detect Mime::Type by extension: #{opts[:content_type]}
+ (#{args.inspect[1..-2]})"
+ end
else
- raise ArgumentError, "failed to detect Mime::Type by extension: #{opts[:content_type]}
- (#{args.inspect[1..-2]})"
+ (opts[:headers] ||= {})['Content-Type'] = opts[:content_type]
end
end
[many, order, orders, opts]
end
\ No newline at end of file