Module: Analysand::Http

Extended by:
Forwardable
Includes:
Rack::Utils
Included in:
Database, Instance
Defined in:
lib/analysand/http.rb

Overview

Private: HTTP client methods for Database and Instance.

Users of this module MUST set @http and @uri in their initializer. @http SHOULD be a Net::HTTP::Persistent instance, and @uri SHOULD be a URI instance.

Constant Summary collapse

SSL_METHODS =
%w(
  certificate ca_file cert_store private_key
  reuse_ssl_sessions ssl_version verify_callback verify_mode
).map { |m| [m, "#{m}="] }.flatten

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#httpObject (readonly)

Returns the value of attribute http.



17
18
19
# File 'lib/analysand/http.rb', line 17

def http
  @http
end

#uriObject (readonly)

Returns the value of attribute uri.



18
19
20
# File 'lib/analysand/http.rb', line 18

def uri
  @uri
end

Instance Method Details

#_req(klass, doc_id, credentials, query, headers, body, block) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/analysand/http.rb', line 60

def _req(klass, doc_id, credentials, query, headers, body, block)
  uri = self.uri.dup
  uri.path += doc_id
  uri.query = build_query(query) unless query.empty?

  req = klass.new(uri.request_uri)

  headers.each { |k, v| req.add_field(k, v) }
  req.body = body if body && req.request_body_permitted?
  set_credentials(req, credentials)

  http.request(uri, req, &block)
end

#closeObject



44
45
46
# File 'lib/analysand/http.rb', line 44

def close
  http.shutdown
end

#init_http_client(uri) ⇒ Object

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/analysand/http.rb', line 27

def init_http_client(uri)
  unless uri.respond_to?(:path) && uri.respond_to?(:absolute?)
    uri = URI(uri)
  end

  raise InvalidURIError, 'You must supply an absolute URI' unless uri.absolute?

  @http = Net::HTTP::Persistent.new('analysand')
  @uri = uri

  # Document IDs and other database bits are appended to the URI path,
  # so we need to make sure that it ends in a /.
  unless uri.path.end_with?('/')
    uri.path += '/'
  end
end

#set_credentials(req, creds) ⇒ Object

Sets credentials on a request object.

If creds is a hash containing :username and :password keys, HTTP basic authorization is used. If creds is a string, the string is added as a cookie.



80
81
82
83
84
85
86
87
88
# File 'lib/analysand/http.rb', line 80

def set_credentials(req, creds)
  return unless creds

  if String === creds
    req.add_field('Cookie', creds)
  elsif creds[:username] && creds[:password]
    req.basic_auth(creds[:username], creds[:password])
  end
end