Class: Wise::Client
- Inherits:
-
Object
- Object
- Wise::Client
- Defined in:
- lib/wise/client.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#rest ⇒ Object
readonly
Returns the value of attribute rest.
Instance Method Summary collapse
- #dataset_id(dataset) ⇒ Object
- #get_credentials(options) ⇒ Object
-
#initialize(path, options) ⇒ Client
constructor
A new instance of Client.
- #model_id(obj) ⇒ Object
- #obj_id(resource, obj) ⇒ Object
- #resource(res) ⇒ Object
Constructor Details
#initialize(path, options) ⇒ Client
Returns a new instance of Client.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/wise/client.rb', line 36 def initialize path, address = [:address] || "https://wise.io" owner, project = path.split("/")[1..2] user, pass = get_credentials @rest = RestClient::Resource.new "#{address}/api", user, pass begin raw = @rest['project/'].get(:params => {:name => project, :owner__username => owner, :limit => 1}) obj = JSON.load(raw)['objects'][0] @owner = obj['owner'] @project = obj['id'] rescue RestClient::Unauthorized raise AuthenticationError.new "Invalid credentials given." end end |
Instance Attribute Details
#rest ⇒ Object (readonly)
Returns the value of attribute rest.
34 35 36 |
# File 'lib/wise/client.rb', line 34 def rest @rest end |
Instance Method Details
#dataset_id(dataset) ⇒ Object
95 96 97 |
# File 'lib/wise/client.rb', line 95 def dataset_id dataset obj_id('dataset', dataset) end |
#get_credentials(options) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/wise/client.rb', line 60 def get_credentials user, pass = nil if ![:user].nil? && ![:password].nil? user, pass = [[:user], [:password]] elsif File.exists?(prf = File.join(Etc.getpwuid.dir, ".wise", "profile")) File.open(prf, "r") do |f| user = f.readline.chomp pass = f.readline.chomp end else raise AuthenticationError.new "Credentials not given and profile not found." end return [user, pass] end |
#model_id(obj) ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/wise/client.rb', line 86 def model_id obj case obj when Integer obj when String obj_id('model', obj) end end |
#obj_id(resource, obj) ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/wise/client.rb', line 77 def obj_id resource, obj raw = @rest["#{resource}/"].get(:params => {:name => obj, :ordering => '-created_at', :project => @project, :limit => 1}) resp = JSON.load(raw)['objects'][0] raise ResourceError.new "Invalid #{resource} '#{obj}'." if resp.nil? resp['id'] end |
#resource(res) ⇒ Object
56 57 58 |
# File 'lib/wise/client.rb', line 56 def resource res @rest[res] end |