Sha256: 45ee7d6430385b180a041c63eabae2b48bdcb14f212b3603d8e183711c9a66cb

Contents?: true

Size: 1.61 KB

Versions: 10

Compression:

Stored size: 1.61 KB

Contents

require 'json'
require 'httparty'

module AgileNotifier
  class ResponseError < StandardError
  end

  module ResponseHelper
    def get_value_of_key(key, *args)
      pm = PrivateMethods.new
      pm.get_value_of_key_from_json(key, pm.request_json_response(*args))
    end

    class PrivateMethods
      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

      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
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
agile_notifier-3.1.2 lib/agile_notifier/response_helper.rb
agile_notifier-3.0.2 lib/agile_notifier/response_helper.rb
agile_notifier-3.0.1 lib/agile_notifier/response_helper.rb
agile_notifier-3.0.0 lib/agile_notifier/response_helper.rb
agile_notifier-2.1.4 lib/agile_notifier/response_helper.rb
agile_notifier-2.1.3 lib/agile_notifier/response_helper.rb
agile_notifier-2.1.2 lib/agile_notifier/response_helper.rb
agile_notifier-2.1.1 lib/agile_notifier/response_helper.rb
agile_notifier-2.1 lib/agile_notifier/response_helper.rb
agile_notifier-2.0 lib/agile_notifier/response_helper.rb