Class: WebStub::Stub

Inherits:
Object
  • Object
show all
Defined in:
lib/webstub/stub.rb

Constant Summary collapse

METHODS =
["GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS", "PATCH"].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, url) ⇒ Stub

Returns a new instance of Stub

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/webstub/stub.rb', line 5

def initialize(method, url)
  @request_method = canonicalize_method(method)
  raise ArgumentError, "invalid method name" unless METHODS.include? @request_method

  @requests = 0

  @request_url = canonicalize_url(url)
  @request_headers = nil
  @request_body = nil
  @callback = nil

  @response_body = ""
  @response_delay = 0.0
  @response_error = nil
  @response_headers = {}
  @response_status_code = 200
end

Instance Attribute Details

#callbackObject

Returns the value of attribute callback



56
57
58
# File 'lib/webstub/stub.rb', line 56

def callback
  @callback
end

#requestsObject

Returns the value of attribute requests



55
56
57
# File 'lib/webstub/stub.rb', line 55

def requests
  @requests
end

#response_bodyObject (readonly)

Returns the value of attribute response_body



66
67
68
# File 'lib/webstub/stub.rb', line 66

def response_body
  @response_body
end

#response_delayObject (readonly)

Returns the value of attribute response_delay



67
68
69
# File 'lib/webstub/stub.rb', line 67

def response_delay
  @response_delay
end

#response_errorObject (readonly)

Returns the value of attribute response_error



68
69
70
# File 'lib/webstub/stub.rb', line 68

def response_error
  @response_error
end

#response_headersObject (readonly)

Returns the value of attribute response_headers



69
70
71
# File 'lib/webstub/stub.rb', line 69

def response_headers
  @response_headers
end

#response_status_codeObject (readonly)

Returns the value of attribute response_status_code



70
71
72
# File 'lib/webstub/stub.rb', line 70

def response_status_code
  @response_status_code
end

Instance Method Details

#do_callback(headers, body) ⇒ Object



115
116
117
118
119
# File 'lib/webstub/stub.rb', line 115

def do_callback(headers, body)
  if @callback
    @callback.call(headers, body)
  end
end

#error?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/webstub/stub.rb', line 23

def error?
  ! @response_error.nil?
end

#matches?(method, url, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/webstub/stub.rb', line 27

def matches?(method, url, options={})
  if @request_url != canonicalize_url(url)
    return false
  end

  if @request_method != canonicalize_method(method)
    return false
  end

  if @request_headers
    headers = options[:headers] || {}

    @request_headers.each do |key, value|
      if headers[key] != value
        return false
      end
    end
  end

  if @request_body
    if @request_body != options[:body]
      return false
    end
  end

  true
end

#redirects?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/webstub/stub.rb', line 58

def redirects?
  @response_status_code.between?(300, 399) && @response_headers["Location"] != nil
end

#requested?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/webstub/stub.rb', line 62

def requested?
  @requests > 0
end

#to_fail(options) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/webstub/stub.rb', line 72

def to_fail(options)
  if error = options.delete(:error)
    @response_error = error
  elsif code = options.delete(:code)
    @response_error = NSError.errorWithDomain(NSURLErrorDomain, code: code, userInfo: nil)
  else
    raise ArgumentError, "to_fail requires either the code or error option"
  end

  self
end

#to_redirect(options) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/webstub/stub.rb', line 125

def to_redirect(options)
  unless url = options.delete(:url)
    raise ArgumentError, "to_redirect requires the :url option"
  end

  options[:headers] ||= {}
  options[:headers]["Location"] = url
  options[:status_code] = 301

  to_return(options)
end

#to_return(options) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/webstub/stub.rb', line 84

def to_return(options)
  if status_code = options[:status_code]
    @response_status_code = status_code
  end

  if headers = options[:headers]
    @response_headers.merge!(headers)
  end

  if json = options[:json]
    @response_body = json
    @response_headers["Content-Type"] = "application/json"

    if @response_body.is_a?(Hash) || @response_body.is_a?(Array)
      @response_body = JSON.generate(@response_body)
    end
  else
    @response_body = options[:body] || ""

    if content_type = options[:content_type]
      @response_headers["Content-Type"] = content_type
    end
  end

  if delay = options[:delay]
    @response_delay = delay
  end

  self
end

#with(options) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/webstub/stub.rb', line 137

def with(options)
  if body = options[:body]
    @request_body = body

    if @request_body.is_a?(Hash)
      @request_body = @request_body.inject({}) { |h, (k,v)| h[k.to_s] = v; h }
    end
  end

  if headers = options[:headers]
    @request_headers = headers
  end

  self
end

#with_callback(&callback) ⇒ Object



121
122
123
# File 'lib/webstub/stub.rb', line 121

def with_callback(&callback)
  @callback = callback
end