Class: GoFigure::StubHttpFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/go_figure/test/stub_http_fetcher.rb

Defined Under Namespace

Modules: Response Classes: ErrorResponse, HashStruct, StringResponse

Instance Method Summary collapse

Instance Method Details

#get(url) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/go_figure/test/stub_http_fetcher.rb', line 46

def get(url)
  if body = interweb[:get][url]
    body.invoked!
    return body.execute
  else
    raise "404 - Could not find #{url}. Available URLs are #{@interweb[:get].keys.inspect}"
  end
end

#interwebObject



85
86
87
# File 'lib/go_figure/test/stub_http_fetcher.rb', line 85

def interweb
  @interweb ||= {:get => {}, :post => {}}
end

#invoked?(url, type = :get, params = {}) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
# File 'lib/go_figure/test/stub_http_fetcher.rb', line 67

def invoked?(url, type = :get, params = {})
  if body = interweb[type][url]
    response = body.execute
    if response.headers == params

      body.invoked?
    end
  end
end

#post(url, params = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/go_figure/test/stub_http_fetcher.rb', line 55

def post(url, params = {})
  if body = interweb[:post][url]
    response = body.execute
    if response.headers == params
      body.invoked!
      return response
    end
  end

  raise "404 - Could not find a url listening to #{url.inspect} that responds to post params #{params.inspect}. Available URLs are #{@interweb[:post].keys.inspect}"
end

#register_content(content, url, type = :get, headers = {}) ⇒ Object



77
78
79
# File 'lib/go_figure/test/stub_http_fetcher.rb', line 77

def register_content(content, url, type = :get, headers = {})
  interweb[type][url] = StringResponse.new(content, 200, headers)
end

#register_error(url, type = :get, error) ⇒ Object



81
82
83
# File 'lib/go_figure/test/stub_http_fetcher.rb', line 81

def register_error(url, type = :get, error)
  interweb[type][url] = ErrorResponse.new(error)
end