Sha256: 873b8691e027d51932e7399b51694c5128e0d33cb9824281874a63f450d122c1

Contents?: true

Size: 1.37 KB

Versions: 14

Compression:

Stored size: 1.37 KB

Contents

module Calabash
  module HTTP

    # A representation of an HTTP request that can be passed passed to the HTTP
    # client as an argument for `get` or `post`.
    # @!visibility private
    class Request
      attr_reader :route, :params

      def initialize(route, params={})
        @route = route
        @params = params
      end

      # Create a new Request from `route` and `parameters`.
      #
      # @param [String] route The http route for the new request.
      # @param [Array, Hash] parameters An Array or Hash of parameters.
      # @return [Request] A new Request for `route` with `parameters`.
      # @raise [RequestError] Raises an error if the parameters cannot be
      #   converted to JSON
      def self.request(route, parameters)
        Request.new(route, Request.data(parameters))
      end

      private

      # Converts `parameters` to JSON.
      #
      # @param [Array, Hash] parameters An Array or Hash of parameters.
      # @return [String] A JSON formatted string that represents the parameters.
      # @raise [RequestError] Raises an error if the parameters cannot be
      #   converted to JSON
      def self.data(parameters)
        begin
          JSON.generate(parameters)
        rescue *[TypeError, JSON::GeneratorError] => e
          raise RequestError, "#{e}: could not generate JSON from '#{parameters}'"
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
calabash-2.0.0.pre11 lib/calabash/http/request.rb
calabash-2.0.0.pre10 lib/calabash/http/request.rb
calabash-2.0.0.pre9 lib/calabash/http/request.rb
calabash-2.0.0.prelegacy4 lib/calabash/http/request.rb
calabash-2.0.0.prelegacy3 lib/calabash/http/request.rb
calabash-2.0.0.prelegacy2 lib/calabash/http/request.rb
calabash-2.0.0.prelegacy lib/calabash/http/request.rb
calabash-2.0.0.pre6 lib/calabash/http/request.rb
calabash-2.0.0.pre5 lib/calabash/http/request.rb
calabash-2.0.0.pre4 lib/calabash/http/request.rb
calabash-2.0.0.pre3 lib/calabash/http/request.rb
calabash-2.0.0.pre2 lib/calabash/http/request.rb
calabash-2.0.0.pre1 lib/calabash/http/request.rb
calabash-1.9.9.pre3 lib/calabash/http/request.rb