Class: GCR::Request

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Request

Returns a new instance of Request.



20
21
22
23
24
# File 'lib/gcr/request.rb', line 20

def initialize(opts)
  @route      = opts["route"]
  @class_name = opts["class_name"]
  @body       = opts["body"]
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



18
19
20
# File 'lib/gcr/request.rb', line 18

def body
  @body
end

#class_nameObject (readonly)

Returns the value of attribute class_name.



18
19
20
# File 'lib/gcr/request.rb', line 18

def class_name
  @class_name
end

#routeObject (readonly)

Returns the value of attribute route.



18
19
20
# File 'lib/gcr/request.rb', line 18

def route
  @route
end

Class Method Details

.from_hash(hash_req) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/gcr/request.rb', line 10

def self.from_hash(hash_req)
  new(
    "route"      => hash_req["route"],
    "class_name" => hash_req["class_name"],
    "body"       => hash_req["body"],
  )
end

.from_proto(route, proto_req, *_) ⇒ Object



2
3
4
5
6
7
8
# File 'lib/gcr/request.rb', line 2

def self.from_proto(route, proto_req, *_)
  new(
    "route"      => route,
    "class_name" => proto_req.class.name,
    "body"       => proto_req.to_json,
  )
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  return false unless route == other.route
  return false unless class_name == other.class_name

  parsed_body.keys.all? do |k|
    next true if GCR.ignored_fields.include?(k)
    parsed_body[k] == other.parsed_body[k]
  end
end

#parsed_bodyObject



26
27
28
# File 'lib/gcr/request.rb', line 26

def parsed_body
  @parsed_body ||= JSON.parse(body)
end

#to_json(*_) ⇒ Object



30
31
32
# File 'lib/gcr/request.rb', line 30

def to_json(*_)
  JSON.dump("route" => route, "class_name" => class_name, "body" => body)
end

#to_protoObject



34
35
36
# File 'lib/gcr/request.rb', line 34

def to_proto
  [route, Object.const_get(class_name).decode_json(body)]
end