class BaseResource

Attributes

options[R]
prompter[R]

Public Class Methods

new(opts) click to toggle source
# File lib/pvdgm-bs-client/base_resource.rb, line 11
def initialize(opts)
  @options = opts
  @prompter = HighLine.new
end

Private Instance Methods

authentication_headers() click to toggle source
# File lib/pvdgm-bs-client/base_resource.rb, line 98
def authentication_headers
  {
    'Authorization' => "Token token=\"#{options[:api_token]}\""
  }
end
build_url(uri) click to toggle source
# File lib/pvdgm-bs-client/base_resource.rb, line 94
def build_url(uri)
  "#{options[:use_ssl] ? 'https' : 'http'}://#{options[:server]}/#{uri}"
end
delete(uri, is_json=true) click to toggle source
# File lib/pvdgm-bs-client/base_resource.rb, line 71
def delete(uri, is_json=true)
  invoke_rest(is_json) do
    result = RestClient.delete(build_url(uri),
                               authentication_headers)
  end
end
get(uri, is_json=true) click to toggle source
# File lib/pvdgm-bs-client/base_resource.rb, line 64
def get(uri, is_json=true)
  invoke_rest(is_json) do
    result = RestClient.get(build_url(uri),
                            authentication_headers)
  end
end
invoke_rest(is_json=true) { || ... } click to toggle source
# File lib/pvdgm-bs-client/base_resource.rb, line 18
def invoke_rest(is_json=true)
  begin
    response = yield
    begin
      is_json ? JSON.parse(response) : response
    rescue Exception => ex
      puts "Error parsing response: #{ex.message[0..100]}"
      if options[:trace]
        puts "Full Error message: #{ex.message}"
        puts "\t#{ex.backtrace.join("\n\t")}"
      end
      exit 1
    end
  rescue RestClient::Exception => ex
    puts "Response: #{ex.response.code} - #{ex.response.description}"
    if [ 403, 404 ].include?(ex.response.code)
      if is_json
        begin
          hash = JSON.parse(ex.response.http_body)
          if hash.has_key?('error')
            puts(hash['error'])
          elsif hash.has_key?('validation_error')
            puts "Validation errors:"
            hash['validation_error'].each_pair do | key, value |
              puts "  #{key}"
              puts "    #{value.join("\n    ")}"
            end
          else
            puts(hash.inspect)
          end
        rescue Exception => json_ex
          puts "Unable to parse body for error: #{ex.response.http_body[0..50]}"
        end
      end
    end
    exit 1
  rescue SystemExit
    raise
  rescue Exception => ex
    # General unknown exception
    puts "Error invoking RestClient: #{ex.message}"
    puts "\t#{ex.backtrace.join("\n\t")}" if options[:trace]
    exit 1
  end
end
post(uri, parameters, is_json=true) click to toggle source
# File lib/pvdgm-bs-client/base_resource.rb, line 78
def post(uri, parameters, is_json=true)
  invoke_rest(is_json) do
    result = RestClient.post(build_url(uri),
                             parameters,
                             authentication_headers)
  end
end
put(uri, parameters, is_json=true) click to toggle source
# File lib/pvdgm-bs-client/base_resource.rb, line 86
def put(uri, parameters, is_json=true)
  invoke_rest(is_json) do
    result = RestClient.put(build_url(uri),
                            parameters,
                            authentication_headers)
  end
end