Class: Namira::Async::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/namira/async/serializer.rb

Class Method Summary collapse

Class Method Details

.serialize_request(request) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/namira/async/serializer.rb', line 7

def serialize_request(request)
  JSON.dump(
    u: request.uri,
    m: request.http_method,
    h: request.headers.to_h,
    b: request.body,
    c: request.config.to_h
  )
end

.serialize_response(response) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/namira/async/serializer.rb', line 30

def serialize_response(response)
  backing = response.instance_variable_get('@backing')
  JSON.dump(
    b: backing.to_a,
    m: response.method,
    r: response.redirect_count,
    u: response.url.to_s,
    v: backing.instance_variable_get('@version')
  )
end

.unserialize_request(payload) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/namira/async/serializer.rb', line 17

def unserialize_request(payload)
  data = JSON.parse(payload)
  Namira::Request.new(
    uri: data.fetch('u'),
    http_method: data.fetch('m').to_sym,
    headers: data.fetch('h'),
    body: data.fetch('b'),
    config: data.fetch('c')
  )
rescue KeyError => error
  raise Namira::Errors::AsyncError, error.message
end

.unserialize_response(payload) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/namira/async/serializer.rb', line 41

def unserialize_response(payload)
  data = JSON.parse(payload)
  backing = HTTP::Response.new(
    status: data.fetch('b')[0],
    headers: data.fetch('b')[1],
    body: data.fetch('b')[2],
    version: data.fetch('v')
  )
  Namira::Response.new(
    data.fetch('m').to_sym,
    Addressable::URI.parse(data.fetch('u')),
    data.fetch('r').to_i,
    backing
  )
rescue KeyError => error
  raise Namira::Errors::AsyncError, error.message
end