Sha256: ea73538de03b8c96d23b863584f025a6bdfd2a42b1bda89f2e31e44427f9a494

Contents?: true

Size: 1.49 KB

Versions: 4

Compression:

Stored size: 1.49 KB

Contents

module Zoomus
  class Utils

    class << self

      def argument_error(name)
        name ? ArgumentError.new("You must provide #{name}") : ArgumentError.new
      end

      def raise_if_error!(response)
        if response["error"]
          raise Error.new(response["error"]["message"])
        else
          response
        end
      end

      def parse_response(http_response)
        response = http_response.parsed_response
        # Mocked response returns a string
        response.kind_of?(Hash) ? response : JSON.parse(response)
      end

      def require_params(params, options)
        params = [params] unless params.is_a? Array
        params.each do |param|
          unless options[param]
            raise argument_error(param.to_s)
            break
          end
        end
      end

      # Dinamically defines bang methods for Actions modules
      def define_bang_methods(klass)
        klass.instance_methods.each do |m|
          klass.send(:define_method, "#{m}!") do |*args|
            Utils.raise_if_error! send(m, *args)
          end
        end
      end

      def extract_options!(array)
        array.last.is_a?(::Hash) ? array.pop : {}
      end

      def process_datetime_params!(params, options)
        params = [params] unless params.is_a? Array
        params.each do |param|
          if options[param] && options[param].kind_of?(Time)
            options[param] = options[param].strftime("%FT%TZ")
          end
        end
        options
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
zoomus-0.3.1 lib/zoomus/utils.rb
zoomus-0.3.0 lib/zoomus/utils.rb
zoomus-0.2.1 lib/zoomus/utils.rb
zoomus-0.2.0 lib/zoomus/utils.rb