Class: HTTTee::Client::Consumer

Inherits:
Rack::Client::Base
  • Object
show all
Defined in:
lib/htttee/client/consumer.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Consumer

Returns a new instance of Consumer.



4
5
6
7
8
9
# File 'lib/htttee/client/consumer.rb', line 4

def initialize(options = {})
  @base_uri = URI.parse(options.fetch(:endpoint, 'http://localhost:3000/'))
  inner_app = options.fetch(:app, Rack::Client::Handler::NetHTTP.new)

  super(inner_app)
end

Instance Method Details

#build_env(request_method, url, headers = {}, body = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/htttee/client/consumer.rb', line 29

def build_env(request_method, url, headers = {}, body = nil)
  uri = @base_uri.nil? ? URI.parse(url) : @base_uri + url

  env = super(request_method, uri.to_s, headers, body)

  env['HTTP_HOST']       ||= http_host_for(uri)
  env['HTTP_USER_AGENT'] ||= http_user_agent

  env
end

#down(uuid) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/htttee/client/consumer.rb', line 21

def down(uuid)
  get("/#{uuid}") do |status, headers, response_body|
    response_body.each do |chunk|
      yield chunk
    end
  end
end

#http_host_for(uri) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/htttee/client/consumer.rb', line 40

def http_host_for(uri)
  if uri.to_s.include?(":#{uri.port}")
    [uri.host, uri.port].join(':')
  else
    uri.host
  end
end

#http_user_agentObject



48
49
50
# File 'lib/htttee/client/consumer.rb', line 48

def http_user_agent
  "htttee (rack-client #{Rack::Client::VERSION} (app: #{@app.class}))"
end

#up(io, uuid, content_type = 'text/plain') ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/htttee/client/consumer.rb', line 11

def up(io, uuid, content_type = 'text/plain')
  headers = {
    'Content-Type'      => content_type,
    'Transfer-Encoding' => 'chunked',
    'Connection'        => 'Keep-Alive',
  }

  post("/#{uuid}", headers, io)
end