Class: NOMS::Command::UserAgent::Requester::Typhoeus

Inherits:
Base
  • Object
show all
Defined in:
lib/noms/command/useragent/requester/typhoeus.rb

Instance Attribute Summary

Attributes inherited from Base

#logger

Instance Method Summary collapse

Methods inherited from Base

#default_logger

Constructor Details

#initialize(opt = {}) ⇒ Typhoeus

Returns a new instance of Typhoeus.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/noms/command/useragent/requester/typhoeus.rb', line 30

def initialize(opt={})
    @log = opt[:logger] || default_logger
    @log.debug "Creating #{self.class} with options: #{opt.inspect}"
    @agent_name = "noms/#{NOMS::Command::VERSION} typhoeus/#{::Typhoeus::VERSION}"
    @auth = { }
    @client_opts = {
        :ssl_verifypeer => false
    }
    cookies = opt.has_key?(:cookies) ? opt[:cookies] : true

    if cookies
        cookie_jar = File.join(NOMS::Command.home, 'cookies.txt')
        @client_opts = @client_opts.merge({
                                              :cookiefile => cookie_jar,
                                              :cookiejar => cookie_jar
                                          })
    end
end

Instance Method Details

#get_auth(url) ⇒ Object



49
50
51
52
53
# File 'lib/noms/command/useragent/requester/typhoeus.rb', line 49

def get_auth(url)
    url = URI.parse(url) unless url.respond_to? :scheme
    domain = url.scheme + '://' + url.host + ':' + url.port.to_s + '/'
    @auth[domain] || { }
end

#request(opt = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/noms/command/useragent/requester/typhoeus.rb', line 55

def request(opt={})
    url = opt[:url]
    spec = @client_opts.merge({
                                  :method => opt[:method] || opt[:get],
                                  :headers => {
                                      'User-Agent' => @agent_name
                                  }.merge(opt[:headers]),
                                  :body => opt[:body]
                              }).merge(get_auth(url))
    request = ::Typhoeus::Request.new(opt[:url], spec)
    response = request.run
    NOMS::Command::UserAgent::Response::Typhoeus.new(response)
end

#set_auth(domain, username, password) ⇒ Object



69
70
71
# File 'lib/noms/command/useragent/requester/typhoeus.rb', line 69

def set_auth(domain, username, password)
    @auth[domain] = { :userpwd => [username, password].join(':') }
end