Class: HttpMonkey::Client::Environment

Inherits:
Hash
  • Object
show all
Defined in:
lib/http_monkey/client/environment.rb

Overview

Rack environment on steroids! -_- This Hash with super powers is passed to Middlewares.

“Snaky” middlewares always remember! “With great power comes great responsibility”

Instance Method Summary collapse

Constructor Details

#initialize(obj = nil, &block) ⇒ Environment

Returns a new instance of Environment.



9
10
11
12
13
# File 'lib/http_monkey/client/environment.rb', line 9

def initialize(obj = nil, &block)
  super
  self.merge!(obj) unless obj.nil?  # better idea? pull please.
  self.default = nil
end

Instance Method Details

#add_http_header(headers = {}) ⇒ Object

Sets HTTP header in rack standard way of life.

Example

env = Client::Environment.new
env.add_http_header("Content-Type" => "text/html")
env.inspect  # => {"HTTP_CONTENT_TYPE" => "text/html"}

Returns nothing important.



41
42
43
44
45
# File 'lib/http_monkey/client/environment.rb', line 41

def add_http_header(headers = {})
  headers.each do |key, value|
    self["HTTP_#{key.to_s.upcase.gsub("-", "_")}"] = value
  end
end

#http_headersObject

Extracts HTTP_ headers from rack environment.

Example

env = Client::Environment.new({"HTTP_X_CUSTOM" => "custom"})
env.http_headers  # => {"X-Custom" => "custom"}

Returns Hash with normalized http headers.



23
24
25
26
27
28
29
30
# File 'lib/http_monkey/client/environment.rb', line 23

def http_headers
  req_headers = self.reject {|k,v| !k.start_with? "HTTP_" }
  normalized = req_headers.map do |key, value|
    new_key = key.sub("HTTP_",'').split('_').map(&:capitalize).join('-')
    [new_key, value]
  end
  Hash[normalized]
end

#monkey_clientObject

Returns HttpMonkey::Client instance or nil.



55
56
57
58
59
# File 'lib/http_monkey/client/environment.rb', line 55

def monkey_client
  if (data = self['http_monkey.request'])
    data[2]
  end
end

#monkey_requestObject

Returns HTTPI::Request instance or nil.



48
49
50
51
52
# File 'lib/http_monkey/client/environment.rb', line 48

def monkey_request
  if (data = self['http_monkey.request'])
    data[1]
  end
end

#request_methodObject

Returns normalized request method.

Example

env = Client::Environment.new('REQUEST_METHOD' => 'GET')
env.request_method  # => :get


91
92
93
94
# File 'lib/http_monkey/client/environment.rb', line 91

def request_method
  method = self['REQUEST_METHOD'].to_s
  (method.empty? ? nil : method.downcase.to_sym)
end

#storageObject

Returns instance from HttpMonkey::Configuration#storage. Simple Hash instance by default.



98
99
100
# File 'lib/http_monkey/client/environment.rb', line 98

def storage
  self['http_monkey.storage']
end

#uriObject

Returns uri from Rack environment. Throws ArgumentError for invalid uri.



75
76
77
78
79
80
81
82
# File 'lib/http_monkey/client/environment.rb', line 75

def uri
  uri = %Q{#{self['rack.url_scheme']}://#{self['SERVER_NAME']}:#{self['SERVER_PORT']}#{self['REQUEST_URI']}}
  begin
    URI.parse(uri)
  rescue StandardError => e
    raise ArgumentError, "Invalid #{uri}", e.backtrace
  end
end

#uri=(uri) ⇒ Object

Sets uri as Rack wants.



62
63
64
65
66
67
68
69
70
71
# File 'lib/http_monkey/client/environment.rb', line 62

def uri=(uri)
  self['SERVER_NAME'] = uri.host
  self['SERVER_PORT'] = uri.port.to_s
  self['QUERY_STRING'] = (uri.query || "")
  self['PATH_INFO'] = (!uri.path || uri.path.empty?) ? "/" : uri.path
  self['rack.url_scheme'] = uri.scheme
  self['HTTPS'] = (uri.scheme == "https" ? "on" : "off")
  self['REQUEST_URI'] = uri.request_uri
  self['HTTP_HOST'] = uri.host
end