Sha256: 53d3939e236d1cd2fb6a1e2c217965b55fce44666bf9c229fd16d02d321e2734

Contents?: true

Size: 1.45 KB

Versions: 55

Compression:

Stored size: 1.45 KB

Contents

module Appfuel
  class Request
    attr_reader :action_route, :feature, :action, :inputs, :namespace

    def initialize(action_route, inputs = {})
      unless inputs.respond_to?(:to_h)
        fail "inputs must respond to :to_h"
      end
      @inputs = inputs.to_h
      @action_route, @feature, @action = parse_route(action_route)
      @namespace = "features.#{feature}.actions.#{action}"
    end

    private

    # The service route is a forward slash separated string consisting of two
    # parts. The first part is the feature that holds the action and the
    # second is the action itself.
    #
    # @example 'offers/create'
    #   feature is Offers
    #   action is Create
    #
    # @param route [String]
    # @return [Array]
    def parse_route(route)
      feature_name, action_name = route.to_s.split('/')

      feature_name = handle_parsed_string(feature_name)
      action_name  = handle_parsed_string(action_name)

      handle_empty_feature(feature_name)
      handle_empty_action(action_name)


      [route, feature_name, action_name]
    end

    def handle_parsed_string(value)
      value.to_s.strip
    end

    def handle_empty_feature(feature_name)
      return unless feature_name.empty?
      fail "feature is missing, action route must be like <feature/action>"
    end

    def handle_empty_action(action_name)
      return unless action_name.empty?
      fail "action is missing, action route must be like <feature/action>"
    end
  end
end

Version data entries

55 entries across 55 versions & 1 rubygems

Version Path
appfuel-0.7.0 lib/appfuel/request.rb
appfuel-0.6.16 lib/appfuel/request.rb
appfuel-0.6.15 lib/appfuel/request.rb
appfuel-0.6.14 lib/appfuel/request.rb
appfuel-0.6.13 lib/appfuel/request.rb
appfuel-0.6.12 lib/appfuel/request.rb
appfuel-0.6.11 lib/appfuel/request.rb
appfuel-0.6.10 lib/appfuel/request.rb
appfuel-0.6.9 lib/appfuel/request.rb
appfuel-0.6.8 lib/appfuel/request.rb
appfuel-0.6.7 lib/appfuel/request.rb
appfuel-0.6.6 lib/appfuel/request.rb
appfuel-0.6.5 lib/appfuel/request.rb
appfuel-0.6.4 lib/appfuel/request.rb
appfuel-0.6.3 lib/appfuel/request.rb
appfuel-0.6.1 lib/appfuel/request.rb
appfuel-0.5.16 lib/appfuel/request.rb
appfuel-0.5.15 lib/appfuel/request.rb
appfuel-0.5.14 lib/appfuel/request.rb
appfuel-0.5.13 lib/appfuel/request.rb