Class: ApiSpec::Parameters

Inherits:
Object
  • Object
show all
Defined in:
lib/api_spec/parameters.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, path, table = nil) ⇒ Parameters

Returns a new instance of Parameters.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/api_spec/parameters.rb', line 5

def initialize(method, path, table = nil)
  @method = method
  @path = path

  @path_params = {}
  @query = {}
  @form = {}
  @json = {}
  @cookie = {}
  @header = {}

  load_table(table) if table
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



32
33
34
# File 'lib/api_spec/parameters.rb', line 32

def method_missing(name)
  State.get(name)
end

Instance Attribute Details

#queryObject (readonly)

Returns the value of attribute query.



3
4
5
# File 'lib/api_spec/parameters.rb', line 3

def query
  @query
end

Instance Method Details

#bodyObject



36
37
38
39
40
41
42
43
44
# File 'lib/api_spec/parameters.rb', line 36

def body
  if @json.any?
    @json.to_json
  elsif @form.any?
    @form
  else
    ""
  end
end

#content_type_headersObject



60
61
62
63
64
65
66
67
# File 'lib/api_spec/parameters.rb', line 60

def content_type_headers
  if @json.any?
    # {"Content-Type" => "application/json"}
    {content_type: :json, "CONTENT_TYPE" => "application/json"}
  else
    {}
  end
end


50
51
52
# File 'lib/api_spec/parameters.rb', line 50

def cookie_header
  { "Cookie" => cookie_string, "HTTP_COOKIE" => cookie_string }
end


54
55
56
57
58
# File 'lib/api_spec/parameters.rb', line 54

def cookie_string
  @cookie
    .map { |key, value| "#{key}=#{value}" }
    .join("; ")
end

#headersObject



46
47
48
# File 'lib/api_spec/parameters.rb', line 46

def headers
  @header.merge(cookie_header).merge(content_type_headers)
end

#urlObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/api_spec/parameters.rb', line 19

def url
  interpolated_path = @path.scan(/\{(.*?)\}/).reduce(@path) do |path, match|
    key = match.first
    value = @path_params[key]

    raise StandardError.new("Missing #{key}") unless value

    path.gsub /\{#{key}\}/, URI.encode(value)
  end

  "http://localhost:#{$port}#{interpolated_path}"
end