Class: Avm::EacGitlabBase0::Api::BaseEntity

Inherits:
EacRest::Entity
  • Object
show all
Defined in:
lib/avm/eac_gitlab_base0/api/base_entity.rb

Direct Known Subclasses

File, Member, Node, Root

Instance Method Summary collapse

Instance Method Details

#delete(url_suffix) ⇒ Object



13
14
15
# File 'lib/avm/eac_gitlab_base0/api/base_entity.rb', line 13

def delete(url_suffix)
  api.request(url_suffix).verb(:delete).response.body_data
end

#dump_debug(basename, data) ⇒ Object



50
51
52
53
54
# File 'lib/avm/eac_gitlab_base0/api/base_entity.rb', line 50

def dump_debug(basename, data)
  file = ::Pathname.new('/tmp').join('gitlab_temp', basename + '.yaml')
  file.parent.mkpath
  ::EacRubyUtils::Yaml.dump_file(file, data)
end

#dump_response(response) ⇒ Object



43
44
45
46
47
48
# File 'lib/avm/eac_gitlab_base0/api/base_entity.rb', line 43

def dump_response(response)
  basename = response.url.to_s.variableize[0..99]
  { data: response.body_data, headers: response.headers, links: response.links }
    .each { |part, value| dump_debug("#{basename}_#{part}", value) }
  response
end

#encode_id(id) ⇒ Object



17
18
19
20
21
# File 'lib/avm/eac_gitlab_base0/api/base_entity.rb', line 17

def encode_id(id)
  return id if id.is_a?(::Integer)

  ::CGI.escape(id.to_s)
end

#fetch_entities(suffix, klass) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/avm/eac_gitlab_base0/api/base_entity.rb', line 30

def fetch_entities(suffix, klass)
  r = []
  request = api.request_json(suffix)
  while request
    response = request.response
    r += validate_response_data(response).map { |rr| klass.new(api, rr) }
    request = response.link('next').if_present do |v|
      api.request_json(v)
    end
  end
  r
end

#fetch_entity(suffix, klass, not_found_message = nil) ⇒ Object



23
24
25
26
27
28
# File 'lib/avm/eac_gitlab_base0/api/base_entity.rb', line 23

def fetch_entity(suffix, klass, not_found_message = nil)
  validate_response_data(
    dump_response(api.request_json(suffix).response),
    not_found_message
  ).if_present { |v| klass.new(api, v) }
end

#validate_response_data(response, not_found_message = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/avm/eac_gitlab_base0/api/base_entity.rb', line 56

def validate_response_data(response, not_found_message = nil)
  if response.body_data.is_a?(::Hash)
    response.body_data['error'].if_present do |v|
      raise "URL: #{response.url}, Data: #{v}"
    end

    response.body_data['message'].if_present do |v|
      return nil if v == not_found_message
    end
  end

  response.body_data
end