Class: Avm::Gitlab::RestApi::BaseEntity

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

Direct Known Subclasses

File, Member, Node, Root

Instance Method Summary collapse

Instance Method Details

#dump_debug(basename, data) ⇒ Object



46
47
48
49
50
# File 'lib/avm/gitlab/rest_api/base_entity.rb', line 46

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



39
40
41
42
43
44
# File 'lib/avm/gitlab/rest_api/base_entity.rb', line 39

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



13
14
15
16
17
# File 'lib/avm/gitlab/rest_api/base_entity.rb', line 13

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

  ::CGI.escape(id.to_s)
end

#fetch_entities(suffix, klass) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/avm/gitlab/rest_api/base_entity.rb', line 26

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



19
20
21
22
23
24
# File 'lib/avm/gitlab/rest_api/base_entity.rb', line 19

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



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/avm/gitlab/rest_api/base_entity.rb', line 52

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 ::RuntimeError, "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