Class: Jetra::GrpcAdapter

Inherits:
Jetra::Grpc::Interface::Service show all
Defined in:
lib/jetra/adapter/grpc.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, &custom_block) ⇒ GrpcAdapter

Returns a new instance of GrpcAdapter.



13
14
15
16
17
18
# File 'lib/jetra/adapter/grpc.rb', line 13

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

    @custom_block = custom_block
end

Instance Method Details

#call(request, _unused_call) ⇒ Object



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

def call(request, _unused_call)

    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 = Jetra::Grpc::JetraResponse.new
    response.status = res.status
    response.body = res.body.to_json
    response
end

#indifferent_hashObject

Creates a Hash with indifferent access.



61
62
63
# File 'lib/jetra/adapter/grpc.rb', line 61

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.



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/jetra/adapter/grpc.rb', line 47

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



40
41
42
43
44
# File 'lib/jetra/adapter/grpc.rb', line 40

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