Class: ExpoNotifier::Request::Base Abstract

Inherits:
Object
  • Object
show all
Extended by:
T::Generic
Defined in:
lib/expo_notifier/request/base.rb

Overview

This class is abstract.

An abstract class for building and sending requests.

Constant Summary collapse

RequestBody =
type_member(:out) { { upper: Mapper::Base } }
ResponseBody =
type_member(:out) { { upper: Mapper::Base } }
Generic =
T.type_alias { Base[Mapper::Base, Mapper::Base] }
BASE_URL =

---------- Default config ---------- ##########

'https://exp.host'
WRITE_TIMEOUT =
5
OPEN_TIMEOUT =
5
READ_TIMEOUT =
30
GZIP_MIN_SIZE =
1024
BASE_HEADERS =
{
  'Content-Type'    => 'application/json',
  'Accept'          => 'application/json',
  'Accept-Encoding' => 'gzip, deflate',
}.freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, access_token: nil, faraday_adapter: Faraday.default_adapter, base_url: BASE_URL, write_timeout: WRITE_TIMEOUT, open_timeout: OPEN_TIMEOUT, read_timeout: READ_TIMEOUT, gzip: true, gzip_min_size: GZIP_MIN_SIZE, additional_headers: nil) ⇒ Base

: ( | RequestBody, | ?access_token: String?, | ?faraday_adapter: Symbol, | ?base_url: String, | ?write_timeout: Integer, | ?open_timeout: Integer, | ?read_timeout: Integer, | ?gzip: bool, | ?gzip_min_size: Integer, | ?additional_headers: Hash[String, String]?, | ) -> void



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/expo_notifier/request/base.rb', line 66

def initialize(
  body,
  access_token:       nil,
  faraday_adapter:    Faraday.default_adapter,
  base_url:           BASE_URL,
  write_timeout:      WRITE_TIMEOUT,
  open_timeout:       OPEN_TIMEOUT,
  read_timeout:       READ_TIMEOUT,
  gzip:               true,
  gzip_min_size:      GZIP_MIN_SIZE,
  additional_headers: nil
)
  @body               = body
  @access_token       = access_token
  @faraday_adapter    = faraday_adapter
  @url                = URI.join(base_url, self.class.path).to_s #: String
  @write_timeout      = write_timeout
  @open_timeout       = open_timeout
  @read_timeout       = read_timeout
  @gzip               = gzip
  @gzip_min_size      = gzip_min_size
  @additional_headers = additional_headers
end

Class Attribute Details

.pathObject

: String



39
40
41
# File 'lib/expo_notifier/request/base.rb', line 39

def path
  @path
end

.response_classObject

: singleton(Mapper::Base)



42
43
44
# File 'lib/expo_notifier/request/base.rb', line 42

def response_class
  @response_class
end

Instance Attribute Details

#bodyObject (readonly)

: RequestBody



49
50
51
# File 'lib/expo_notifier/request/base.rb', line 49

def body
  @body
end

#responseObject (readonly)



52
53
54
# File 'lib/expo_notifier/request/base.rb', line 52

def response
  @response
end

#urlObject (readonly)

: String



46
47
48
# File 'lib/expo_notifier/request/base.rb', line 46

def url
  @url
end

Instance Method Details

#body_to_sendObject

String in the JSON format, compressed with gzip if needed : -> String



122
123
124
125
126
127
128
# File 'lib/expo_notifier/request/base.rb', line 122

def body_to_send
  return raw_body unless compress_body?

  io = StringIO.new
  Zlib::GzipWriter.wrap(io) { |gz| gz.write(raw_body) }
  io.string
end

#compress_body?Boolean Also known as: body_compressed?

: -> bool

Returns:

  • (Boolean)


131
132
133
134
135
# File 'lib/expo_notifier/request/base.rb', line 131

def compress_body?
  return false unless @gzip

  raw_body.bytesize >= @gzip_min_size
end

#executeObject

: -> Response



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/expo_notifier/request/base.rb', line 91

def execute
  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)

  faraday_response = begin
    faraday_connection.post do |req|
      req.body = body_to_send
    end
  rescue Faraday::Error => e
    e
  end

  duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time
  @response = Response[ResponseBody].new(self.class.response_class, duration.to_f, faraday_response)
end

#headersObject

: -> T::Hash[String, String]



107
108
109
110
111
112
# File 'lib/expo_notifier/request/base.rb', line 107

def headers
  h = BASE_HEADERS
  h = h.merge({ 'Content-Encoding' => 'gzip' }) if compress_body?
  h = h.merge(@additional_headers) if @additional_headers
  h
end

#nameObject

: -> String?



140
# File 'lib/expo_notifier/request/base.rb', line 140

def name = self.class.name&.split('::')&.last

#raw_bodyObject

String in the JSON format : -> String



116
117
118
# File 'lib/expo_notifier/request/base.rb', line 116

def raw_body
  @raw_body ||= body.to_json
end