Class: Jerakia::Datasource::Http

Inherits:
Instance
  • Object
show all
Defined in:
lib/jerakia/datasource/http.rb

Instance Attribute Summary

Attributes inherited from Instance

#options, #request, #response

Instance Method Summary collapse

Methods inherited from Instance

#answer, feature, #features?, #initialize, option, set_options, validate_options

Constructor Details

This class inherits a constructor from Jerakia::Datasource::Instance

Instance Method Details

#lookupObject


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/jerakia/datasource/http.rb', line 30

def lookup

  lookup_supported_params = [
    :host,
    :port,
    :output,
    :failure,
    :ignore_404,
    :headers,
    :http_connect_timeout,
    :http_read_timeout,
    :use_ssl,
    :ssl_ca_cert,
    :ssl_cert,
    :ssl_key,
    :ssl_verify,
    :use_auth,
    :auth_user,
    :auth_pass
  ]
  lookup_params = options.select { |p| lookup_supported_params.include?(p) }
  http_lookup = LookupHttp.new(lookup_params)


  paths = options[:paths].flatten
  answer do |response|
    path = paths.shift
    break unless path
    Jerakia.log.debug("Attempting to load data from #{path}")

    data = http_lookup.get_parsed(path)
    Jerakia.log.debug("Datasource provided #{data} (#{data.class}) looking for key #{request.key}")

    if data.is_a?(Hash)
      if options[:lookup_key]
        response.submit data[request.key] if data.has_key?(request.key)
      else
        response.submit data
      end
    else
      unless options[:output] == 'plain' || options[:failure] == 'graceful'
        raise Jerakia::Error, "HTTP request did not return a hash for #{request.key}"
      end
      response.submit data
    end
  end
end