Class: Capybara::HTTPClientJson::Driver

Inherits:
Json::Driver::Base show all
Defined in:
lib/capybara/httpclient_json/driver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Driver

Returns a new instance of Driver.



22
23
24
25
26
# File 'lib/capybara/httpclient_json/driver.rb', line 22

def initialize(app, options = {})
  @app, @options = app, { :follow_redirect => true }.merge(options)
  @rack_server = Capybara::Server.new(@app)
  @rack_server.boot if Capybara.run_server
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



4
5
6
# File 'lib/capybara/httpclient_json/driver.rb', line 4

def app
  @app
end

#cookiesObject (readonly)

Returns the value of attribute cookies.



4
5
6
# File 'lib/capybara/httpclient_json/driver.rb', line 4

def cookies
  @cookies
end

#current_urlObject (readonly)

Returns the value of attribute current_url.



4
5
6
# File 'lib/capybara/httpclient_json/driver.rb', line 4

def current_url
  @current_url
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/capybara/httpclient_json/driver.rb', line 4

def options
  @options
end

#responseObject (readonly)

Returns the value of attribute response.



4
5
6
# File 'lib/capybara/httpclient_json/driver.rb', line 4

def response
  @response
end

Instance Method Details

#bodyObject



38
39
40
# File 'lib/capybara/httpclient_json/driver.rb', line 38

def body
  MultiJson.load(source) || {}
end

#clientObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/capybara/httpclient_json/driver.rb', line 6

def client
  unless @client
    @client = HTTPClient.new
    @client.follow_redirect_count  = 5 + 1 # allows 5 redirection
    @client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE

    # hack for redirect
    def @client.redirect_uri_callback_to_keep_new_uri(uri, res)
      new_uri = default_redirect_uri_callback(uri, res)
      @new_uri = new_uri
    end
    @client.redirect_uri_callback = @client.method(:redirect_uri_callback_to_keep_new_uri)
  end
  @client
end

#delete(url, params = {}, headers = {}) ⇒ Object



73
74
75
# File 'lib/capybara/httpclient_json/driver.rb', line 73

def delete(url, params = {}, headers = {})
  process :delete, url, params, headers
end

#get(url, params = {}, headers = {}) ⇒ Object Also known as: visit



50
51
52
# File 'lib/capybara/httpclient_json/driver.rb', line 50

def get(url, params = {}, headers = {})
  process :get, url, params, headers, options[:follow_redirect]
end

#jsonObject



42
43
44
# File 'lib/capybara/httpclient_json/driver.rb', line 42

def json
  MultiJson.load(source) || {}
end

#needs_server?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/capybara/httpclient_json/driver.rb', line 93

def needs_server?
  true
end

#patch(url, json, headers = {}) ⇒ Object



61
62
63
64
65
# File 'lib/capybara/httpclient_json/driver.rb', line 61

def patch(url, json, headers = {})
  json = MultiJson.dump(json) unless json.is_a?(String)
  headers['Content-Type'] = "application/json; charset=#{json.encoding.to_s.downcase}"
  process :patch, url, json, headers, options[:follow_redirect]
end

#post(url, json, headers = {}) ⇒ Object



55
56
57
58
59
# File 'lib/capybara/httpclient_json/driver.rb', line 55

def post(url, json, headers = {})
  json = MultiJson.dump(json) unless json.is_a?(String)
  headers['Content-Type'] = "application/json; charset=#{json.encoding.to_s.downcase}"
  process :post, url, json, headers, options[:follow_redirect]
end

#put(url, json, headers = {}) ⇒ Object



67
68
69
70
71
# File 'lib/capybara/httpclient_json/driver.rb', line 67

def put(url, json, headers = {})
  json = MultiJson.dump(json) unless json.is_a?(String)
  headers['Content-Type'] = "application/json; charset=#{json.encoding.to_s.downcase}"
  process :put, url, json, headers
end

#raw_jsonObject Also known as: source, html



32
33
34
# File 'lib/capybara/httpclient_json/driver.rb', line 32

def raw_json
  response.body
end

#reset!Object



97
98
99
# File 'lib/capybara/httpclient_json/driver.rb', line 97

def reset!
  @client = nil
end

#response_headersObject



46
47
48
# File 'lib/capybara/httpclient_json/driver.rb', line 46

def response_headers
  response.headers
end

#status_codeObject



28
29
30
# File 'lib/capybara/httpclient_json/driver.rb', line 28

def status_code
  response.code
end