Class: Jetra::ThriftAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/jetra/adapter/thrift.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, &custom_block) ⇒ ThriftAdapter

Returns a new instance of ThriftAdapter.



15
16
17
18
19
20
# File 'lib/jetra/adapter/thrift.rb', line 15

def initialize(app, &custom_block)
  @app = app

  @custom_block = custom_block

end

Instance Method Details

#call(request) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jetra/adapter/thrift.rb', line 22

def call(request)

  if @custom_block
    @custom_block.call(request)
  end

  route = request.route || ""
  
  params = parse_params(request.params)

  sym_route = route.to_sym

  res = @app.call(sym_route, params)

  response = Thrift::Response.new
  response.status = res.status
  response.body = res.body.to_json
  response
end

#indifferent_hashObject

Creates a Hash with indifferent access.



63
64
65
# File 'lib/jetra/adapter/thrift.rb', line 63

def indifferent_hash
  Hash.new {|hash,key| hash[key.to_s] if Symbol === key }
end

#indifferent_params(object) ⇒ Object

Enable string or symbol key access to the nested params hash.



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/jetra/adapter/thrift.rb', line 49

def indifferent_params(object)
  case object
  when Hash
    new_hash = indifferent_hash
    object.each { |key, value| new_hash[key] = indifferent_params(value) }
    new_hash
  when Array
    object.map { |item| indifferent_params(item) }
  else
    object
  end
end

#parse_params(params) ⇒ Object



42
43
44
45
46
# File 'lib/jetra/adapter/thrift.rb', line 42

def parse_params(params)
  indifferent_params(JSON.load(params).to_h)
rescue => boom
  {}
end