Sha256: c9e9c586511d7285b00cd6d48d0b27144179797f6ed5740bf129e10d0a1718e1

Contents?: true

Size: 1.25 KB

Versions: 17

Compression:

Stored size: 1.25 KB

Contents

begin
  require "active_support"
rescue LoadError
end

module Frenchy
  class Request
    attr_accessor :service, :method, :path, :params, :extras

    # Create a new request with given parameters
    def initialize(service, method, path, params={}, extras={})
      path = path.dup
      path.scan(/(:[a-z0-9_+]+)/).flatten.uniq.each do |pat|
        k = pat.sub(":", "")
        begin
          v = params.fetch(pat.sub(":", "")).to_s
        rescue
          raise Frenchy::Error, "The required parameter '#{k}' was not specified."
        end

        params.delete(k)
        path.sub!(pat, v)
      end

      @service = service
      @method = method
      @path = path
      @params = params
      @extras = extras
    end

    # Issue the request and return the value
    def value
      Frenchy.find_service(@service).send(@method, @path, @params)
    end

    # Requests are instrumented if ActiveSupport is available.
    if defined?(ActiveSupport::Notifications)
      alias_method :value_without_instrumentation, :value

      def value
        ActiveSupport::Notifications.instrument("request.frenchy", {service: @service, method: @method, path: @path, params: @params}.merge(@extras)) do
          value_without_instrumentation
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
frenchy-0.6.2 lib/frenchy/request.rb
frenchy-0.6.1 lib/frenchy/request.rb
frenchy-0.6.0 lib/frenchy/request.rb
frenchy-0.5.2 lib/frenchy/request.rb
frenchy-0.5.1 lib/frenchy/request.rb
frenchy-0.5.0 lib/frenchy/request.rb
frenchy-0.4.0 lib/frenchy/request.rb
frenchy-0.3.0 lib/frenchy/request.rb
frenchy-0.2.9 lib/frenchy/request.rb
frenchy-0.2.8 lib/frenchy/request.rb
frenchy-0.2.7 lib/frenchy/request.rb
frenchy-0.2.6 lib/frenchy/request.rb
frenchy-0.2.5 lib/frenchy/request.rb
frenchy-0.2.4 lib/frenchy/request.rb
frenchy-0.2.3 lib/frenchy/request.rb
frenchy-0.2.2 lib/frenchy/request.rb
frenchy-0.2.1 lib/frenchy/request.rb