Module: Pagerduty::Core
- Included in:
- Pagerduty, Incidents::Incident, Requests::Alerts
- Defined in:
- lib/pagerduty/core.rb
Instance Method Summary collapse
-
#curl(options) ⇒ Object
def curl.
-
#has_requirements?(keys, options) ⇒ Boolean
Check a Hash object for expected keys.
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() curl_request = { ssl: true, port: 443, headers: { "Content-Type" => "application/json", "Authorization" => "Token token=#{Pagerduty.class_variable_get(:@@token)}", }, } .merge! curl_request url = URI.parse([:uri]) if [:params] parameters = [: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 [: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 .has_key?(:data) request.set_form_data([:data]) end if .has_key?(:basic_auth) request.basic_auth [:basic_auth][:user], [:basic_auth][:password] end request.body = [:body] [:headers].each { |key,val| request.add_field(key,val) } response = if [:method] == 'POST' http.post(url.path,[:data].to_json,[:headers]) elsif [:method] == 'PUT' http.put(url.path,[:data].to_json,[:headers]) else http.request(request) end if [:raw_response] == true response elsif response.body JSON.parse(response.body) else { 'code' => response.code, 'message' => response. } 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
95 96 97 |
# File 'lib/pagerduty/core.rb', line 95 def has_requirements?(keys,) (keys - .keys).empty? end |