class BaseResource
Attributes
options[R]
prompter[R]
Public Class Methods
new(opts)
click to toggle source
# File lib/pvdgm-svc-client/base_resource.rb, line 11 def initialize(opts) @options = opts @prompter = HighLine.new @prompter.wrap_at = 120 @prompter.page_at = 22 end
Private Instance Methods
authentication_headers()
click to toggle source
# File lib/pvdgm-svc-client/base_resource.rb, line 117 def authentication_headers { 'Authorization' => "Token token=\"#{options[:api_token]}\"" } end
build_url(uri)
click to toggle source
# File lib/pvdgm-svc-client/base_resource.rb, line 113 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-svc-client/base_resource.rb, line 78 def delete(uri, is_json=true) invoke_rest(is_json) do RestClient.delete(build_url(uri), authentication_headers) end end
get(uri, is_json=true)
click to toggle source
# File lib/pvdgm-svc-client/base_resource.rb, line 71 def get(uri, is_json=true) invoke_rest(is_json) do RestClient.get(build_url(uri), authentication_headers) end end
invoke_rest(is_json=true) { || ... }
click to toggle source
# File lib/pvdgm-svc-client/base_resource.rb, line 21 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 if ex.response.nil? puts ex exit 1 end 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
is_valid_date?(date_string)
click to toggle source
# File lib/pvdgm-svc-client/base_resource.rb, line 106 def is_valid_date?(date_string) DateTime.parse date_string true rescue false end
is_valid_object?(object_type, object_id)
click to toggle source
# File lib/pvdgm-svc-client/base_resource.rb, line 101 def is_valid_object?(object_type, object_id) result = get("services/validation?object_type=#{object_type}&object_id=#{object_id}") result['exists'] end
post(uri, parameters, is_json=true)
click to toggle source
# File lib/pvdgm-svc-client/base_resource.rb, line 85 def post(uri, parameters, is_json=true) invoke_rest(is_json) do RestClient.post(build_url(uri), parameters, authentication_headers) end end
put(uri, parameters, is_json=true)
click to toggle source
# File lib/pvdgm-svc-client/base_resource.rb, line 93 def put(uri, parameters, is_json=true) invoke_rest(is_json) do RestClient.put(build_url(uri), parameters, authentication_headers) end end