class Fog::Hetznercloud::Client

Public Class Methods

new(endpoint, token, connection_options) click to toggle source
# File lib/fog/hetznercloud/client.rb, line 4
def initialize(endpoint, token, connection_options)
  @endpoint           = endpoint
  @token              = token
  @connection_options = connection_options
end

Public Instance Methods

request(params) click to toggle source
# File lib/fog/hetznercloud/client.rb, line 10
def request(params)
  params[:headers] ||= {}
  params[:headers]['Content-Type'] ||= 'application/json'
  params[:headers]['Authorization'] ||= "Bearer #{@token}"

  params[:body] = encode_body(params)

  response = connection.request(params)

  response.body = decode_body(response)

  response
end

Private Instance Methods

connection() click to toggle source
# File lib/fog/hetznercloud/client.rb, line 26
def connection
  @connection ||= Fog::Core::Connection.new(@endpoint, false, @connection_options)
end
decode_body(response) click to toggle source
# File lib/fog/hetznercloud/client.rb, line 43
def decode_body(response)
  body         = response.body
  content_type = response.headers['Content-Type']

  if !body.nil? && !body.empty? && content_type =~ %r{application/json.*}i
    Fog::JSON.decode(body)
  else
    body
  end
end
encode_body(params) click to toggle source
# File lib/fog/hetznercloud/client.rb, line 30
def encode_body(params)
  body         = params[:body]
  content_type = params[:headers]['Content-Type']

  if body.nil? || body.is_a?(String)
    body
  elsif content_type =~ %r{application/json.*}i
    Fog::JSON.encode(body)
  else
    body.to_s
  end
end