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



73
74
75
# File 'lib/hcloud/typhoeus_ext.rb', line 73

def [](arg)
  parsed_json[arg]
end

#_error_class(code) ⇒ Object



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

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



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

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



47
48
49
# File 'lib/hcloud/typhoeus_ext.rb', line 47

def context
  @context ||= Context.new
end

#errorObject



93
94
95
# File 'lib/hcloud/typhoeus_ext.rb', line 93

def error
  parsed_json[:error].to_h
end

#error_codeObject



85
86
87
# File 'lib/hcloud/typhoeus_ext.rb', line 85

def error_code
  error[:code]
end

#error_messageObject



89
90
91
# File 'lib/hcloud/typhoeus_ext.rb', line 89

def error_message
  error[:message]
end

#paginationObject



62
63
64
# File 'lib/hcloud/typhoeus_ext.rb', line 62

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

#parsed_jsonObject



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

def parsed_json
  return {} if code == 204

  @parsed_json ||= begin
                     Oj.load(body, symbol_keys: true).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
                   end
rescue StandardError
  raise Error::UnexpectedError, "unable to load body: #{body}"
end

#resourceObject



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

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



66
67
68
69
70
71
# File 'lib/hcloud/typhoeus_ext.rb', line 66

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