Class: Umwelt::Abstract::Request

Inherits:
Interactor show all
Defined in:
lib/umwelt/abstract/request.rb

Direct Known Subclasses

Episode::Get, History::Get, Project::Get

Constant Summary collapse

BASE_URL =
'http://umwelt.dev/api'
HEADERS =

BASE_URL = ‘localhost:2300/api

{
  'Content-Type': 'application/json',
  'User-Agent': "Umwelt client #{Umwelt::VERSION}",
  'Accept': 'application/json'
}.freeze

Instance Method Summary collapse

Methods inherited from Interactor

#prove

Instance Method Details

#check(request) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/umwelt/abstract/request.rb', line 24

def check(request)
  if request.parsed_response.key?('errors')
    error! request.parsed_response['errors']
  else
    request
  end
end

#get(host: BASE_URL, path: '', params: {}) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/umwelt/abstract/request.rb', line 16

def get(host: BASE_URL, path: '', params: {})
  check HTTParty.get(
    [host, path].join('/'), options(params)
  )
rescue StandardError => e
  error! [self.class.name, e.message, path]
end

#options(params) ⇒ Object



47
48
49
# File 'lib/umwelt/abstract/request.rb', line 47

def options(params)
  { headers: HEADERS, format: :json }.merge(params)
end

#parse(request) ⇒ Object



32
33
34
35
36
# File 'lib/umwelt/abstract/request.rb', line 32

def parse(request)
  request
    .parsed_response
    .transform_keys(&:to_sym)
end

#struct(data, mapper) ⇒ Object



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

def struct(data, mapper)
  result = mapper.call(data)
  if result.success?
    result.struct
  else
    error! result.errors
  end
end