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



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


50
51
52
53
54
55
56
# File 'lib/api_spec/parameters.rb', line 50

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

  { "Cookie" => string }
end

#headersObject



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

def headers
  @header.merge(cookie_header)
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