Module: Hcloud::TyphoeusExt

Defined in:
lib/hcloud/typhoeus_ext.rb

Defined Under Namespace

Classes: Context

Constant Summary collapse

ATTRIBUTES =
%i[
  block autoload_action resource_path
  resource_class expected_code
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.collect_attributes(kwargs) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/hcloud/typhoeus_ext.rb', line 16

def self.collect_attributes(kwargs)
  hash = {}
  ATTRIBUTES.each do |key|
    hash[key] = kwargs.delete(key) if kwargs.key?(key)
  end
  hash
end

Instance Method Details

#[](arg) ⇒ Object



71
72
73
# File 'lib/hcloud/typhoeus_ext.rb', line 71

def [](arg)
  parsed_json[arg]
end

#_error_class(code) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/hcloud/typhoeus_ext.rb', line 95

def _error_class(code)
  case code
  when 'invalid_input' then Error::InvalidInput
  when 'forbidden' then Error::Forbidden
  when 'locked' then Error::Locked
  when 'not_found' then Error::NotFound
  when 'rate_limit_exceeded' then Error::RateLimitExceeded
  when 'resource_unavailable' then Error::ResourceUnavailable
  when 'service_error' then Error::ServiceError
  when 'uniqueness_error' then Error::UniquenessError
  else
    Error::ServerError
  end
end

#attributes=(kwargs) ⇒ Object



24
25
26
27
28
# File 'lib/hcloud/typhoeus_ext.rb', line 24

def attributes=(kwargs)
  kwargs.each do |key, value|
    public_send("#{key}=", value)
  end
end

#check_for_error(e_code: nil, e_message: nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/hcloud/typhoeus_ext.rb', line 49

def check_for_error(e_code: nil, e_message: nil)
  case code
  when 401 then raise(Error::Unauthorized)
  when 0 then raise(Error::ServerError, "Connection error: #{return_code}")
  when 400...600
    raise _error_class(e_code || error_code), e_message || error_message
  end

  raise Error::UnexpectedError, body if expected_code && expected_code != code
end

#contextObject



45
46
47
# File 'lib/hcloud/typhoeus_ext.rb', line 45

def context
  @context ||= Context.new
end

#errorObject



91
92
93
# File 'lib/hcloud/typhoeus_ext.rb', line 91

def error
  parsed_json[:error].to_h
end

#error_codeObject



83
84
85
# File 'lib/hcloud/typhoeus_ext.rb', line 83

def error_code
  error[:code]
end

#error_messageObject



87
88
89
# File 'lib/hcloud/typhoeus_ext.rb', line 87

def error_message
  error[:message]
end

#paginationObject



60
61
62
# File 'lib/hcloud/typhoeus_ext.rb', line 60

def pagination
  @pagination ||= Pagination.new(nil, parsed_json[:meta].to_h[:pagination])
end

#parsed_jsonObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hcloud/typhoeus_ext.rb', line 30

def parsed_json
  return {} if code == 204

  @parsed_json ||= Oj.load(body, symbol_keys: true, mode: :compat).tap do |json|
    next unless request.hydra

    check_for_error(
      e_code: json.to_h.dig(:error, :code),
      e_message: json.to_h.dig(:error, :message)
    )
  end
rescue StandardError
  raise Error::UnexpectedError, "unable to load body: #{body}"
end

#resourceObject



75
76
77
78
79
80
81
# File 'lib/hcloud/typhoeus_ext.rb', line 75

def resource
  action = parsed_json[:action] if autoload_action
  return [Action.new(self, action), block.call(self)].flatten(1) if block && action
  return block.call(self) if block

  @resource_class.from_response(self, autoload_action: autoload_action)
end

#resource_attributesObject



64
65
66
67
68
69
# File 'lib/hcloud/typhoeus_ext.rb', line 64

def resource_attributes
  _resource = [@resource_path].flatten.compact.map(&:to_s).map(&:to_sym)
  return parsed_json if _resource.empty?

  parsed_json.dig(*_resource)
end