Class: Doh::HttpHelper

Inherits:
Object show all
Defined in:
lib/doh/util/http_helper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ HttpHelper

Returns a new instance of HttpHelper.



10
11
12
13
14
# File 'lib/doh/util/http_helper.rb', line 10

def initialize(url)
  @url = url
  @headers = {}
  init_sender
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



8
9
10
# File 'lib/doh/util/http_helper.rb', line 8

def headers
  @headers
end

Class Method Details

.load(url, use_ssl = true, encode_url = false) ⇒ Object

shortcut method



71
72
73
74
75
76
# File 'lib/doh/util/http_helper.rb', line 71

def self.load(url, use_ssl = true, encode_url = false)
  url = URI.escape(url) if encode_url
  helper = HttpHelper.new(url)
  helper.nossl if !use_ssl
  helper.get
end

Instance Method Details

#get(get_body_only = true, allow_redirect = false, num_redirects = 0) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/doh/util/http_helper.rb', line 28

def get(get_body_only = true, allow_redirect = false, num_redirects = 0)
  getstr = @parsed_url.path
  getstr += '?' + @parsed_url.query unless @parsed_url.query.to_s.empty?
  request = Net::HTTP::Get.new(getstr)
  request.initialize_http_header(@headers)
  response = @sender.start { |http| http.request(request) }
  if response.is_a?(Net::HTTPRedirection) && allow_redirect
    raise "too many redirects" if num_redirects > 20
    @url = response['location']
    init_sender
    return get(get_body_only, true, num_redirects + 1)
  end
  return response.body.to_s if get_body_only
  response
end

#hdr(name, value) ⇒ Object



65
66
67
68
# File 'lib/doh/util/http_helper.rb', line 65

def hdr(name, value)
  @headers[name] = value
  self
end

#nosslObject



54
55
56
57
58
# File 'lib/doh/util/http_helper.rb', line 54

def nossl
  @sender.use_ssl = false
  @nossl = true
  self
end

#post(data, get_body_only = true) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/doh/util/http_helper.rb', line 16

def post(data, get_body_only = true)
  dohlog.debug("posting to url #@url data: #{data.inspect}")
  response = if data.is_a?(Hash)
    post_hash(data)
  else
    post_string(data)
  end
  check_for_errors(response)
  return response.body.to_s if get_body_only
  response
end

#soap_action(action) ⇒ Object



49
50
51
52
# File 'lib/doh/util/http_helper.rb', line 49

def soap_action(action)
  @headers['SOAPAction'] = action
  self
end

#timeout(seconds) ⇒ Object



60
61
62
63
# File 'lib/doh/util/http_helper.rb', line 60

def timeout(seconds)
  @sender.read_timeout = seconds
  self
end

#xmlObject



44
45
46
47
# File 'lib/doh/util/http_helper.rb', line 44

def xml
  @headers['Content-Type'] = 'text/xml'
  self
end