Module: RSpec::Endpoint

Defined in:
lib/rspec/endpoint.rb,
lib/rspec/endpoint/version.rb

Constant Summary collapse

VERSION =
"0.1.2"

Instance Method Summary collapse

Instance Method Details

#endpoint(description, metadata = {}, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rspec/endpoint.rb', line 6

def endpoint(description,  = {}, &block)
  method, path = description.split(" ")
  method = method.downcase

  methods = %w(get post put delete patch head options)
  fail "Invalid method #{method}. Valid options: #{methods}" unless methods.include? method.downcase

  segments = path.split("/")

  path_params = segments.select { |segment| segment.start_with?(":") }.map { |segment| segment[1..-1] }

  describe(description, ) do
    let(:path) { segments.map { |seg| seg.start_with?(":") ? send(seg[1..-1]) : seg }.join("/") }
    let(:params) { path_params.reduce({}) { |params, param| params.merge(param.to_sym => send(param)) } }

    path_params.each { |param| let(param.to_sym) { send(param.to_sym) } }

    subject { send(method.to_sym, send(:path), params) }

    class_eval(&block)
  end
end