Module: RspecApiDocumentation::DSL::Endpoint

Extended by:
ActiveSupport::Concern
Includes:
Rack::Test::Utils
Defined in:
lib/rspec_api_documentation/dsl/endpoint.rb

Overview

DSL methods available inside the RSpec example.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#do_request(extra_params = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rspec_api_documentation/dsl/endpoint.rb', line 32

def do_request(extra_params = {})
  @extra_params = extra_params

  params_or_body = nil
  path_or_query = path

  if http_method == :get && !query_string.blank?
    path_or_query += "?#{query_string}"
  else
    if respond_to?(:raw_post)
      params_or_body = raw_post
    else
      formatter = RspecApiDocumentation.configuration.request_body_formatter
      case formatter
      when :json
        params_or_body = params.empty? ? nil : params.to_json
      when :xml
        params_or_body = params.to_xml
      when Proc
        params_or_body = formatter.call(params)
      else
        params_or_body = params
      end
    end
  end

  rspec_api_documentation_client.send(http_method, path_or_query, params_or_body, headers)
end

#exampleObject



126
127
128
# File 'lib/rspec_api_documentation/dsl/endpoint.rb', line 126

def example
  RSpec.current_example
end

#explanation(text) ⇒ Object



122
123
124
# File 'lib/rspec_api_documentation/dsl/endpoint.rb', line 122

def explanation(text)
  example.[:explanation] = text
end

#header(name, value) ⇒ Object



73
74
75
76
# File 'lib/rspec_api_documentation/dsl/endpoint.rb', line 73

def header(name, value)
  example.[:headers] ||= {}
  example.[:headers][name] = value
end

#headersObject



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rspec_api_documentation/dsl/endpoint.rb', line 78

def headers
  return unless example.[:headers]
  example.[:headers].inject({}) do |hash, (header, value)|
    if value.is_a?(Symbol)
      hash[header] = send(value) if respond_to?(value)
    else
      hash[header] = value
    end
    hash
  end
end

#http_methodObject



90
91
92
# File 'lib/rspec_api_documentation/dsl/endpoint.rb', line 90

def http_method
  example.[:method]
end

#in_path?(param) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/rspec_api_documentation/dsl/endpoint.rb', line 102

def in_path?(param)
  path_params.include?(param)
end

#methodObject



94
95
96
# File 'lib/rspec_api_documentation/dsl/endpoint.rb', line 94

def method
  http_method
end

#paramsObject



65
66
67
68
69
70
71
# File 'lib/rspec_api_documentation/dsl/endpoint.rb', line 65

def params
  parameters = example..fetch(:parameters, {}).inject({}) do |hash, param|
    set_param(hash, param)
  end
  parameters.deep_merge!(extra_params)
  parameters
end

#pathObject



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/rspec_api_documentation/dsl/endpoint.rb', line 110

def path
  example.[:route].gsub(/:(\w+)/) do |match|
    if extra_params.keys.include?($1)
      delete_extra_param($1)
    elsif respond_to?($1)
      send($1)
    else
      match
    end
  end
end

#path_paramsObject



106
107
108
# File 'lib/rspec_api_documentation/dsl/endpoint.rb', line 106

def path_params
  example.[:route].scan(/:(\w+)/).flatten
end

#query_stringObject



61
62
63
# File 'lib/rspec_api_documentation/dsl/endpoint.rb', line 61

def query_string
  build_nested_query(params || {})
end

#statusObject



98
99
100
# File 'lib/rspec_api_documentation/dsl/endpoint.rb', line 98

def status
  rspec_api_documentation_client.status
end