class NiceJsonApi::Response::SingleRequest
Do a single html request
Public Class Methods
new(uri, method: :get, body: nil, auth: nil)
click to toggle source
# File lib/nice_json_api.rb, line 71 def initialize(uri, method: :get, body: nil, auth: nil) @uri = URI(uri) @method = method @body = body @auth = Hash(auth) end
Public Instance Methods
response()
click to toggle source
# File lib/nice_json_api.rb, line 78 def response Net::HTTP.start(@uri.hostname, @uri.port, use_ssl: ssl?) do |http| http.request(req) end end
Private Instance Methods
auth_header()
click to toggle source
# File lib/nice_json_api.rb, line 86 def auth_header if @auth.key?(:user) "Basic #{Base64.strict_encode64("#{@auth[:user]}:#{@auth[:password]}")}" elsif @auth.key?(:bearer) "Bearer #{@auth[:bearer]}" end end
headers()
click to toggle source
# File lib/nice_json_api.rb, line 94 def headers { 'Accept' => 'application/json', 'Authorization' => auth_header, 'Content-Type' => @body ? 'application/json' : nil, 'User-Agent' => "NiceJsonApi Ruby #{VERSION}" }.reject { |_, v| v.nil? } end
klass()
click to toggle source
# File lib/nice_json_api.rb, line 103 def klass NiceJsonApi::Internal::Inflector.constantize("Net::HTTP::#{@method.to_s.capitalize}") end
manual_header()
click to toggle source
# File lib/nice_json_api.rb, line 107 def manual_header return {} unless @auth.key?(:header) { @auth[:header][:name] => @auth[:header][:value] } end
req()
click to toggle source
# File lib/nice_json_api.rb, line 112 def req @req ||= begin req = klass.new(@uri) req.body = @body.to_json if @body headers.merge(manual_header).each { |header, value| req[header] = value } req end end
ssl?()
click to toggle source
# File lib/nice_json_api.rb, line 121 def ssl? @uri.scheme == 'https' end