Class: AgileNotifier::ResponseHelper::PrivateMethods

Inherits:
Object
  • Object
show all
Defined in:
lib/agile_notifier/response_helper.rb

Instance Method Summary collapse

Instance Method Details

#get_value_of_key_from_json(key, collection) ⇒ Object

only returns the first match if duplicated keys exist



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/agile_notifier/response_helper.rb', line 30

def get_value_of_key_from_json(key, collection) # only returns the first match if duplicated keys exist
  if collection.respond_to?(:each_pair) # behaves like Hash
    return collection[key] if collection.has_key?(key)
    collection.keys.each do |k|
      value = send(__method__, key, collection[k]) # recursive call
      return value if !value.nil?
    end
  elsif collection.respond_to?(:each_index) # behaves like Array
    collection.each do |x|
      value = send(__method__, key, x) # recursive call
      return value if !value.nil?
    end
  end
  return nil
end

#request_json_response(*args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/agile_notifier/response_helper.rb', line 15

def request_json_response(*args)
  method = ((args[1].respond_to?(:delete) ? args[1].delete(:method) : false) || :get).to_sym
  supported_methods = [:get, :post, :put, :delete, :head, :options]
  unless supported_methods.include?(method)
    raise(RuntimeError, "Unsupported HTTP Method: #{method.to_s}", caller)
  else
    response = HTTParty.send(method, *args)
    if response.code.to_s.match(/^2\d{2}$/)
      return JSON.parse(response.body)
    else
      raise(ResponseError, "HTTP Status Code: #{response.code} - #{response.parsed_response}", caller)
    end
  end
end