Class: Hotwire::Request

Inherits:
Base
  • Object
show all
Defined in:
lib/hotwire/request.rb

Instance Attribute Summary

Attributes inherited from Base

#errors

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#add_error, #valid?

Class Method Details

.from_params(params) ⇒ Object

Factory method to create a Request instance from the request parameters. tqx:out is used to determine the specific class instance returned by this method, depending on the requested output format.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/hotwire/request.rb', line 7

def self.from_params(params)
  # Exract Wire params from the request.
  wire_params = {}
  tqx = params[:tqx]
  return false if not tqx or tqx.blank?
  
  wire_params[:tqx] = true
  tqx.split(';').each do |kv|
    key, value = kv.split(':')
    wire_params[key.to_sym] = value
  end

  # Create the appropriate Wire instance from the gviz-specific parameters
  wire_params[:out] ||= "json"    
  self.new(wire_params)
end

Instance Method Details

#[](k) ⇒ Object

Access a Wire parameter. k must be symbols, like :out, :reqId.



51
52
53
# File 'lib/hotwire/request.rb', line 51

def [](k)
  @params[k]
end

#[]=(k, v) ⇒ Object

Sets a Wire parameter. k must be symbols, like :out, :reqId.



56
57
58
# File 'lib/hotwire/request.rb', line 56

def []=(k, v)
  @params[k] = v
end

#build_responseObject

Builds a new response object from the request



32
33
34
# File 'lib/hotwire/request.rb', line 32

def build_response
  Hotwire::Response.from_request(self)
end

#validateObject

Validates this instance by checking that the configuration parameters conform to the official specs.



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/hotwire/request.rb', line 38

def validate
  @errors.clear
  if @params[:tqx]
    add_error(:invalid_request, 
              "Missing required parameter reqId") unless @params[:reqId]
  
    if @params[:version] && !Hotwire.supported_api_versions.include?(@params[:version])
      add_error(:invalid_request, "Unsupported version #{@params[:version]}")
    end
  end
end