class TarkinClient

Constants

API
SETTINGS_FILES

Attributes

api_client[R]
settings[RW]

Public Class Methods

new(**options) click to toggle source

Constructor. Needs to know the Tarkin Server parameters.

They can be passed with three differen ways
# File lib/tarkin.rb, line 34
def initialize(**options)
  @authorized = false  
  @settings = {}  
  if options[:email] && options[:password] && options[:tarkin_url]
    @settings = options.select { |k,v| [:email, :tarkin_url].include? k }
    @settings[:token] = get_token(@settings[:email], options[:password])
  elsif options[:token] && options[:tarkin_url]
    @settings[:tarkin_url] = options[:tarkin_url]
    @settings[:token] = options[:token]
  else
    get_settings
    save_settings
  end
  @api_client = ARest.new(api_url, token: @settings[:token])
end

Public Instance Methods

authorized?() click to toggle source

Returns true, if there is a connectivity to the server and you are authorized

# File lib/tarkin.rb, line 137
def authorized?
  @authorized || check_connectivity
end
find(term) click to toggle source

Search for a username or directory Input: a String to search, may contain wildcard (*) Output: array of hashes: [ { :label, :redirect_to }]

> tc.find 'sys'
#> [{:label=>"/db/prod/oracle/C84PROD/sys", :redirect_to=>"/db/prod/oracle/C84PROD#4"},
    {:label=>"/db/prod/oracle/C84PROD/sysadm", :redirect_to=>"/db/prod/oracle/C84PROD#5"}]
# File lib/tarkin.rb, line 119
def find(term)
  response = @api_client.get("_find.json", form_data: {term: term})
  if response.ok?
    response.deserialize
  else
    raise TarkinClientException, "Can't search for '#{term}', server returns #{response.code}: #{response.message}"
  end
end
inspect() click to toggle source
# File lib/tarkin.rb, line 141
def inspect
  "TarkinClient <server: #{@settings[:tarkin_url]}, authorized: #{authorized?}>"
end
ls(path = '/') click to toggle source

Returns the content of given directory Output is a hash containing:

  • directories => [ {:name, :id, :created_at, :updated_at, :description} ]

  • items => [ {:username, :id, :created_at, :updated_at, :description} ]

    > tc.ls '/db/prod/oracle' #> {:directories=>

    [{:name=>"C84PROD", :id=>14, :created_at=>"2015-06-07T10:36:56.463Z", :updated_at=>"2015-06-07T10:36:56.463Z", :description=>"Production"}],
    :items=>
     [{:id=>6,
      :username=>"scott",
      :created_at=>"2015-06-07T10:38:27.981Z",
      :updated_at=>"2015-06-07T10:38:27.981Z",
      :description=>"The same user in all production databases"}]}
# File lib/tarkin.rb, line 97
def ls(path = '/')
  u = path.strip.chomp.sub(/(\/)+$/,'') # remove trailing slashes
  u = if u == '' then '_dir.json' else "_dir/#{u}.json" end
  response = @api_client.get(u)
  if response.ok?
    response.deserialize
  else
    if response.code == "404"
      raise TarkinClientException, "No such file or directory"
    else
      raise TarkinClientException, "Can't list directory, server returns #{response.code}: #{response.message}"
    end
  end
end
password(path_or_id) click to toggle source

Returns Hash containing :id, :username and :password Gets Item.id or full path to the Item as a parameter

>> tc.password(107)
# {
#       "id" => 110,
# "username" => "sysdba",
# "password" => "secret_top"
# }

>> tc.password('/db/oracle/C84PCPY/sysdba')
# {
#       "id" => 110,
# "username" => "sysdba",
# "password" => "secret_top"
# }
# File lib/tarkin.rb, line 66
def password(path_or_id)
  case path_or_id
  when String
    # full path given
    u = "#{path_or_id}.json"
  when Fixnum
    # Item ID given
    u = "_password/#{path_or_id}.json"
  end
  response = @api_client.get(u)
  if response.ok?
    response.deserialize
  else
    raise TarkinClientException, "Can't get password, server returns #{response.code}: #{response.message}"
  end
end
token() click to toggle source

Returns string with valid token

> tc.token
#> "zvaY5...sds="
# File lib/tarkin.rb, line 131
def token
  @settings[:token]
end

Private Instance Methods

api_url() click to toggle source
# File lib/tarkin.rb, line 146
def api_url
  "#{@settings[:tarkin_url]}/#{API}"
end
check_connectivity() click to toggle source
# File lib/tarkin.rb, line 196
def check_connectivity
  # @authorized = api_uri['_ping'].get.ok? unless @authorized
  # @authorized = "#{api_url}/_ping".to_uri.get("", "Authorization" => "Token token=#{@settings[:token]}").ok? unless @authorized
  @api_client.get("_ping").ok? unless @authorized
end
get_settings() click to toggle source
# File lib/tarkin.rb, line 150
def get_settings
  settings_file = SETTINGS_FILES.find {|file| File.exists? file}
  if settings_file
    @settings = YAML::load_file settings_file
  else
    new_settings
  end
end
get_token(email, password) click to toggle source
# File lib/tarkin.rb, line 179
def get_token(email, password)
  begin
    client = ARest.new(api_url, username: email, password: password)
    response = client.get('_authorize.json')
  rescue SocketError
    say "<%= color('Cannot connect to server.', BOLD) %> Please retry."
    return nil
  end
  unless response.ok?
    say "<%= color('#{response.message}', BOLD) %>. Please retry."
    nil
  else
    @authorized = true
    response.deserialize[:token]
  end
end
new_settings() click to toggle source
# File lib/tarkin.rb, line 159
def new_settings
  @settings = Hash.new
  until @settings[:token]
    @settings[:tarkin_url] = ask("Your <%= color('Tarkin', BOLD) %> server URL: ") do |q|
      q.default = @settings[:tarkin_url] || "http://tarkin.tg.pl" 
      q.validate = /^(http|https):\/\/.*/ix
    end
    @settings[:email] = ask("Your <%= color('Tarkin', BOLD) %> account email: ") do |q| 
      q.default = @settings[:email] || "user@example.com"
      q.validate = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
    end
    password = ask("Password for #{@settings[:email]}: ") { |q| q.echo = "*" }
    @settings[:token] = get_token(@settings[:email], password)
  end 
end
save_settings() click to toggle source
# File lib/tarkin.rb, line 175
def save_settings
  File.open(SETTINGS_FILES.first, 'w') { |f| f.puts @settings.to_yaml }
end