Module: Wajub::HttpUtils

Defined in:
lib/wajub/http/http_utils.rb

Class Method Summary collapse

Class Method Details

.api_urlObject



16
17
18
# File 'lib/wajub/http/http_utils.rb', line 16

def api_url
  API_URL
end

.create_idempotency_key(prefix = 'wajub') ⇒ Object



20
21
22
# File 'lib/wajub/http/http_utils.rb', line 20

def create_idempotency_key(prefix = 'wajub')
  "#{prefix}-#{SecureRandom.uuid}"
end

.decode_json(data) ⇒ Object



43
44
45
46
47
# File 'lib/wajub/http/http_utils.rb', line 43

def decode_json(data)
  JSON.parse(data)
rescue JSON::ParserError
  {}
end

.normalize_api_key(raw) ⇒ Object



10
11
12
13
14
# File 'lib/wajub/http/http_utils.rb', line 10

def normalize_api_key(raw)
  key = raw.to_s.strip
  key = key.sub(/\Abearer\s+/i, '').strip if key.match?(/\Abearer\s+/i)
  key
end

.pick_list(body, *keys) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/wajub/http/http_utils.rb', line 31

def pick_list(body, *keys)
  keys.each do |key|
    if body.key?(key) && !body[key].nil?
      return { data: body[key], meta: body['meta'] }
    end
  end
  if body.key?('items') && !body['items'].nil?
    return { data: body['items'], meta: body['meta'] }
  end
  { data: body['data'] || [], meta: body['meta'] }
end

.pick_resource(body, *keys) ⇒ Object



24
25
26
27
28
29
# File 'lib/wajub/http/http_utils.rb', line 24

def pick_resource(body, *keys)
  keys.each do |key|
    return body[key] if body.key?(key) && !body[key].nil?
  end
  body
end