Skip to content

Commit

Permalink
Merge 4fccebf into 4aed534
Browse files Browse the repository at this point in the history
  • Loading branch information
keeguon committed Jun 2, 2016
2 parents 4aed534 + 4fccebf commit f1bdc25
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
1 change: 1 addition & 0 deletions httpi.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Gem::Specification.new do |s|

s.add_dependency 'rack'
s.add_dependency 'socksify'
s.add_dependency 'multipart-post'

s.add_development_dependency 'rubyntlm', '~> 0.3.2'
s.add_development_dependency 'rake', '~> 10.0'
Expand Down
34 changes: 26 additions & 8 deletions lib/httpi/adapter/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module Adapter
# http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/
class NetHTTP < Base

register :net_http, :deps => %w(net/https)
register :net_http, :deps => %w(net/http net/http/post/multipart)
def initialize(request)
check_net_ntlm_version!
@request = request
Expand All @@ -28,12 +28,24 @@ def initialize(request)
# Executes arbitrary HTTP requests.
# @see HTTPI.request
def request(method)
# Determine if Net::HTTP supports the method using reflection
unless Net::HTTP.const_defined?(:"#{method.to_s.capitalize}") &&
Net::HTTP.const_get(:"#{method.to_s.capitalize}").class == Class
# Special case for multipart post
if method.to_s == 'multipart'
klass = 'Post::Multipart'

raise NotSupportedError, "Net::HTTP does not support "\
"#{method.to_s.upcase}"
unless Net::HTTP.const_defined?(:"#{klass}") &&
Net::HTTP.const_get(:"#{klass}").class == Class

raise NotSupportedError, "Net::HTTP does not support "\
"#{klass}"
end
else
# Determine if Net::HTTP supports the method using reflection
unless Net::HTTP.const_defined?(:"#{method.to_s.capitalize}") &&
Net::HTTP.const_get(:"#{method.to_s.capitalize}").class == Class

raise NotSupportedError, "Net::HTTP does not support "\
"#{method.to_s.upcase}"
end
end
do_request(method) do |http, http_request|
http_request.body = @request.body
Expand Down Expand Up @@ -183,9 +195,15 @@ def ssl_cert_store(ssl)
end

def request_client(type)
request_class = Net::HTTP.const_get(:"#{type.to_s.capitalize}")
request_class = nil
if type.to_s == 'multipart'
request_class = Net::HTTP::Post::Multipart
request_client = request_class.new @request.url.request_uri, @request.attachments, @request.headers
else
request_class = Net::HTTP.const_get(:"#{type.to_s.capitalize}")
request_client = request_class.new @request.url.request_uri, @request.headers
end

request_client = request_class.new @request.url.request_uri, @request.headers
request_client.basic_auth(*@request.auth.credentials) if @request.auth.basic?

if @request.auth.digest?
Expand Down
7 changes: 7 additions & 0 deletions lib/httpi/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ def on_body(&block)
@on_body
end

attr_writer :attachments

# Returns attachments if any - defaults to nil
def attachments
@attachments ||= nil
end

# Returns the <tt>HTTPI::Authentication</tt> object.
def auth
@auth ||= Auth::Config.new
Expand Down

0 comments on commit f1bdc25

Please sign in to comment.