module ProfitBricks
Top-level module
ProfitBricks
SDK Ruby module
Constants
- VERSION
Public Class Methods
configure() { |Config| ... }
click to toggle source
# File lib/profitbricks/profitbricks.rb, line 3 def self.configure(&block) # Configuration variable defaults ProfitBricks::Config.timeout = 60 ProfitBricks::Config.interval = 5 ProfitBricks::Config.depth = 1 ProfitBricks::Config.global_classes = true ProfitBricks::Config.debug = false ProfitBricks::Config.protocol = 'https' ProfitBricks::Config.port = '443' ProfitBricks::Config.path_prefix = 'cloudapi/v4' yield ProfitBricks::Config if ProfitBricks::Config.host url = construct_url else url = ProfitBricks::Config.url || 'https://api.profitbricks.com/cloudapi/v4' end params = { user: ProfitBricks::Config.username, password: ProfitBricks::Config.password, debug: ProfitBricks::Config.debug, omit_default_port: true, connect_timeout: 360 } @client = Excon.new(url, params) @client ProfitBricks.client = @client # Flatten module namespace. if ProfitBricks::Config.global_classes ProfitBricks.constants.select { |c| Class === ProfitBricks.const_get(c) }.each do |klass| next if klass == :Config unless Kernel.const_defined?(klass) Kernel.const_set(klass, ProfitBricks.const_get(klass)) end end end end
wait_for(timeout = ProfitBricks::Config.timeout, interval = ProfitBricks::Config.interval, &_block)
click to toggle source
# File lib/profitbricks/wait_for.rb, line 3 def self.wait_for(timeout = ProfitBricks::Config.timeout, interval = ProfitBricks::Config.interval, &_block) duration = 0 start = Time.now retries = 0 until yield || duration > timeout sleep(interval.respond_to?(:call) ? interval.call(retries += 1).to_f : interval.to_f) duration = Time.now - start end if duration > timeout fail StandardError, "The specified wait_for timeout (#{timeout} seconds) was exceeded." else { duration: duration } end end
Private Class Methods
add_headers(params)
click to toggle source
# File lib/profitbricks/profitbricks.rb, line 93 def self.add_headers(params) params[:headers] ||= {} params[:headers].merge!(ProfitBricks::Config.headers) if ProfitBricks::Config.headers if params[:headers]['User-Agent'] params[:headers]['User-Agent'] = "profitbricks-ruby-sdk/#{ProfitBricks::VERSION} " + params[:headers]['User-Agent'] else params[:headers]['User-Agent'] = "profitbricks-ruby-sdk/#{ProfitBricks::VERSION}" end unless params[:headers].key?('Content-Type') params[:headers]['Content-Type'] = content_type(params[:method]) end params end
add_request_id(response)
click to toggle source
# File lib/profitbricks/profitbricks.rb, line 74 def self.add_request_id(response) location ||= response.headers['Location'] request_id ||= location.match(/requests\/([-a-f0-9]+)/i)[1] unless location.nil? body = parse_json(response.body) if body.nil? body = { requestId: request_id } else body['requestId'] = request_id end body end
append_query(params)
click to toggle source
# File lib/profitbricks/profitbricks.rb, line 122 def self.append_query(params) params[:query] ||= {} params[:query].merge!(depth: ProfitBricks::Config.depth) if !params[:query][:depth] end
client()
click to toggle source
# File lib/profitbricks/profitbricks.rb, line 70 def self.client @client end
client=(client)
click to toggle source
# File lib/profitbricks/profitbricks.rb, line 66 def self.client=(client) @client = client end
construct_url()
click to toggle source
# File lib/profitbricks/profitbricks.rb, line 127 def self.construct_url "#{ProfitBricks::Config.protocol}://" \ "#{ProfitBricks::Config.host}:" \ "#{ProfitBricks::Config.port}" \ "#{ProfitBricks::Config.path_prefix}" end
content_type(method)
click to toggle source
# File lib/profitbricks/profitbricks.rb, line 109 def self.content_type(method) 'application/json' end
get_class(name, options = {})
click to toggle source
# File lib/profitbricks/profitbricks.rb, line 134 def self.get_class(name, options = {}) klass = name.camelcase klass = options[:class_name].to_s.camelcase if options[:class_name] if ProfitBricks.const_defined?(klass) klass = ProfitBricks.const_get(klass) else begin require "profitbricks/#{klass.downcase}" klass = ProfitBricks.const_get(klass) rescue LoadError raise LoadError.new("Invalid association, could not locate the class '#{klass}'") end end klass end
parse_json(body)
click to toggle source
# File lib/profitbricks/profitbricks.rb, line 87 def self.parse_json(body) JSON.parse(body) unless body.nil? || body.empty? rescue JSON::ParserError => error raise error end
prepend_path_prefix(params)
click to toggle source
# File lib/profitbricks/profitbricks.rb, line 113 def self.prepend_path_prefix(params) return params unless ProfitBricks::Config.path_prefix path_prefix = ProfitBricks::Config.path_prefix.sub(/\/$/, '') params[:path] = params[:path].sub(/^\//, '') params[:path] = "#{path_prefix}/#{params[:path]}" params end
request(params)
click to toggle source
# File lib/profitbricks/profitbricks.rb, line 49 def self.request(params) begin params = add_headers(params) ProfitBricks.client.params[:query] = append_query(params) response = ProfitBricks.client.request(prepend_path_prefix(params)) rescue Excon::Errors::Unauthorized => error raise error, parse_json(error.response.body)['messages'] rescue Excon::Errors::HTTPStatusError => error raise error, parse_json(error.response.body)['messages'] rescue Excon::Errors::InternalServerError => error raise error, parse_json(error.response.body)['messages'] rescue Excon::Error::Forbidden => error raise error, parse_json(error.response.body)['messages'] end add_request_id(response) end