Class: Amfetamine::NeinNeinNein

Inherits:
Object
  • Object
show all
Defined in:
lib/amfetamine/helpers/test_helpers.rb

Overview

Annoying stub for http responses

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/amfetamine/helpers/test_helpers.rb', line 50

def method_missing(method, *args, &block)
  if [:get,:post,:delete,:put].include?(method)
    # TODO: Dump and rewrite

    # If this is the dsl calling
    if args.first.is_a?(Hash) || args.empty?
      opts = args.first || {}
      path = opts[:path] || 'default'
      code = opts[:code] || 200
      query = opts[:query]
    else # Else this is a request
      path = args[0] || 'default'
      query = args[1][:query]
    end

    paths_with_values = instance_variable_get("@#{method.to_s}") || {}

    path.gsub!(/\/$/,'') #remove trailing slash
    old_path = path
    path += query.to_s.strip

    if block_given?
      paths_with_values[path]= FakeResponse.new(method, code, block)
      instance_variable_set("@#{method.to_s}", paths_with_values)
    end

    response = paths_with_values ? (paths_with_values[path] || paths_with_values[old_path] || paths_with_values['default']) : nil

    return response if response

    raise Amfetamine::ExternalConnectionsNotAllowed, "Tried to do #{method} with #{args}\n Allowed paths: \n #{paths_with_values.keys.join("\n")}"
  else
    super
  end
end

Instance Method Details

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
# File 'lib/amfetamine/helpers/test_helpers.rb', line 42

def respond_to?(method)
  if [:get,:post,:delete, :put].include?(method)
    true
  else
    super
  end
end