Class: TarkinClient
- Inherits:
-
Object
- Object
- TarkinClient
- Defined in:
- lib/tarkin.rb
Constant Summary collapse
- SETTINGS_FILES =
["#{Dir.home}/.tarkin", ".tarkin", "/etc/tarkin"]
- API =
"_api/v1"
Instance Attribute Summary collapse
-
#api_client ⇒ Object
readonly
Returns the value of attribute api_client.
-
#settings ⇒ Object
Returns the value of attribute settings.
Instance Method Summary collapse
-
#authorized? ⇒ Boolean
Returns true, if there is a connectivity to the server and you are authorized.
-
#find(term) ⇒ Object
Search for a username or directory Input: a String to search, may contain wildcard (*) Output: array of hashes: [ { :label, :redirect_to }].
-
#initialize(**options) ⇒ TarkinClient
constructor
Constructor.
- #inspect ⇒ Object
-
#ls(path = '/') ⇒ Object
Returns the content of given directory Output is a hash containing: * directories => [ :id, :created_at, :updated_at, :description ] * items => [ :id, :created_at, :updated_at, :description ].
-
#password(path_or_id) ⇒ Object
Returns Hash containing :id, :username and :password Gets Item.id or full path to the Item as a parameter.
-
#token ⇒ Object
Returns string with valid token > tc.token #> “zvaY5…sds=”.
Constructor Details
#initialize(**options) ⇒ TarkinClient
Constructor. Needs to know the Tarkin Server parameters.
- They can be passed with three differen ways
-
the options hash. Notice that in this case the token will not be stored in the ~/.tarkin file >> tc = TarkinClient.new email: ‘[email protected]’, password: ‘password0’, tarkin_url: ‘tarkin.tg.pl’ # TarkinClient <server: tarkin.tg.pl, authorized: true> >> tc = TarkinClient.new tarkin_url: ‘tarkin.tg.pl’, token: ‘Ahoishwqbn32ldhw.…..’ # TarkinClient <server: tarkin.tg.pl, authorized: true>
-
by reading the stored parameters in settings file (like ~/.tarkin)
>> tc = TarkinClient.new # TarkinClient <server: localhost:3000, authorized: true>
-
by asking user via command line (when file not found) >> tc = TarkinClient.new # Your Tarkin server URL: |tarkin.tg.pl| localhost:3000 # Your Tarkin account email: |[email protected]| [email protected] # Password for [email protected]: ******** # TarkinClient <server: localhost:3000, authorized: true>
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/tarkin.rb', line 34 def initialize(**) @authorized = false @settings = {} if [:email] && [:password] && [:tarkin_url] @settings = .select { |k,v| [:email, :tarkin_url].include? k } @settings[:token] = get_token(@settings[:email], [:password]) elsif [:token] && [:tarkin_url] @settings[:tarkin_url] = [:tarkin_url] @settings[:token] = [:token] else get_settings save_settings end @api_client = ARest.new(api_url, token: @settings[:token]) end |
Instance Attribute Details
#api_client ⇒ Object (readonly)
Returns the value of attribute api_client.
13 14 15 |
# File 'lib/tarkin.rb', line 13 def api_client @api_client end |
#settings ⇒ Object
Returns the value of attribute settings.
12 13 14 |
# File 'lib/tarkin.rb', line 12 def settings @settings end |
Instance Method Details
#authorized? ⇒ Boolean
Returns true, if there is a connectivity to the server and you are authorized
137 138 139 |
# File 'lib/tarkin.rb', line 137 def @authorized || check_connectivity end |
#find(term) ⇒ Object
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"}]
119 120 121 122 123 124 125 126 |
# 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.}" end end |
#inspect ⇒ Object
141 142 143 |
# File 'lib/tarkin.rb', line 141 def inspect "TarkinClient <server: #{@settings[:tarkin_url]}, authorized: #{}>" end |
#ls(path = '/') ⇒ Object
Returns the content of given directory Output is a hash containing:
-
directories => [ :id, :created_at, :updated_at, :description ]
-
items => [ :id, :created_at, :updated_at, :description ]
> tc.ls ‘/db/prod/oracle’ #>
[{:name=>"C84PROD", :id=>14, :created_at=>"2015-06-07T10:36:56.463Z", :updated_at=>"2015-06-07T10:36:56.463Z", :description=>"Production"], :items=> [ :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"]}
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# 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.}" end end end |
#password(path_or_id) ⇒ Object
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"
# }
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# 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.}" end end |
#token ⇒ Object
Returns string with valid token
> tc.token
#> "zvaY5...sds="
131 132 133 |
# File 'lib/tarkin.rb', line 131 def token @settings[:token] end |