Module: Pagerduty::Core

Included in:
Pagerduty, Incidents::Incident, Requests::Alerts
Defined in:
lib/pagerduty/core.rb

Instance Method Summary collapse

Instance Method Details

#curl(options) ⇒ Object

def curl

Purpose: Performs a CURL request

Params: options<~Hash> - The options Hash to send

uri<~String> - The URI to curl
ssl<~Boolean><Optional> - Whether or not to use SSL
port<~Integer><Optional> - The port number to connect to
params<~Hash><Optional> - The params to send in the curl request
headers<~Hash><Optional> - The headers to send in the curl request
method<~String> - The HTTP method to perform
basic_auth<~Hash><Optional>
  user<~String> - Basic auth user
  password<~String> - Basic auth password

Returns: <String>



22
23
24
25
26
27
28
29
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
77
78
79
80
81
82
83
# File 'lib/pagerduty/core.rb', line 22

def curl(options)

  curl_request = {
    ssl: true,
    port: 443,
    headers: {
      "Content-Type" => "application/json",
      "Authorization" => "Token token=#{Pagerduty.class_variable_get(:@@token)}",
    },
  }

  options.merge! curl_request

  url = URI.parse(options[:uri])

  if options[:params]
    parameters = options[:params].map { |k,v| v.to_query(k) }.join("&")
    url += "?#{parameters}"
  end

  http = Net::HTTP.new(url.host, 443)
  http.use_ssl = true

  request = case options[:method]
            when 'DELETE'
              Net::HTTP::Delete.new(url)
            when 'GET'
              Net::HTTP::Get.new(url)
            when 'POST'
              Net::HTTP::Post.new(url)
            when 'PUT'
              Net::HTTP::Put.new(url)
            end

  if options.has_key?(:data)
    request.set_form_data(options[:data])
  end

  if options.has_key?(:basic_auth)
    request.basic_auth options[:basic_auth][:user], options[:basic_auth][:password]
  end

  request.body = options[:body]

  options[:headers].each { |key,val| request.add_field(key,val) }

  response = if options[:method] == 'POST'
               http.post(url.path,options[:data].to_json,options[:headers])
             elsif options[:method] == 'PUT'
               http.put(url.path,options[:data].to_json,options[:headers])
             else
               http.request(request)
             end

  if options[:raw_response] == true
    response
  elsif response.body
    JSON.parse(response.body)
  else
    { 'code' => response.code, 'message' => response.message }
  end
end

#has_requirements?(keys, options) ⇒ Boolean

Check a Hash object for expected keys

Parameters

  • ‘keys’<~Array><~Object> - An array of objects expected to be found as keys in the supplied Hash

  • ‘options’<~Hash> - The Hash to perform the check on

Returns

  • Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/pagerduty/core.rb', line 95

def has_requirements?(keys,options)
  (keys - options.keys).empty?
end