Class: Wise::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/wise/client.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options
  address = options[:address] || "https://wise.io"
  owner, project = path.split("/")[1..2]
  
  user, pass = get_credentials options
  
  @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

#restObject (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 options
  user, pass = nil

  if !options[:user].nil? && !options[:password].nil?
    user, pass = [options[:user], options[: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