Class: Site

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Site

Returns a new instance of Site.



5
6
7
8
9
10
11
12
13
# File 'lib/site.rb', line 5

def initialize(options)
  @root = options[:root] if options[:root]
  @subdomain = options[:subdomain] if options[:subdomain]
  @username = options[:username] if options[:username]
  @password = options[:password] if options[:password]
  @auth_key = options[:auth_key] if options[:auth_key]
  @session_id = Digest::MD5.hexdigest(rand.to_s)
  @data_path = options[:data_path] || "#{@root}/.vae/data/"
end

Instance Attribute Details

#auth_keyObject (readonly)

Returns the value of attribute auth_key.



2
3
4
# File 'lib/site.rb', line 2

def auth_key
  @auth_key
end

#dataObject (readonly)

Returns the value of attribute data.



2
3
4
# File 'lib/site.rb', line 2

def data
  @data
end

#data_pathObject (readonly)

Returns the value of attribute data_path.



2
3
4
# File 'lib/site.rb', line 2

def data_path
  @data_path
end

#passwordObject (readonly)

Returns the value of attribute password.



2
3
4
# File 'lib/site.rb', line 2

def password
  @password
end

#rootObject (readonly)

Returns the value of attribute root.



2
3
4
# File 'lib/site.rb', line 2

def root
  @root
end

#secret_keyObject

Returns the value of attribute secret_key.



3
4
5
# File 'lib/site.rb', line 3

def secret_key
  @secret_key
end

#session_idObject (readonly)

Returns the value of attribute session_id.



2
3
4
# File 'lib/site.rb', line 2

def session_id
  @session_id
end

#subdomainObject (readonly)

Returns the value of attribute subdomain.



2
3
4
# File 'lib/site.rb', line 2

def subdomain
  @subdomain
end

#usernameObject (readonly)

Returns the value of attribute username.



2
3
4
# File 'lib/site.rb', line 2

def username
  @username
end

Instance Method Details

#domainObject



15
16
17
# File 'lib/site.rb', line 15

def domain
  subdomain =~ /\./ ? subdomain : "#{subdomain}.vaesite.com"
end

#fetch_from_server(req) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/site.rb', line 19

def fetch_from_server(req)
  http = Net::HTTP.new(domain, 443)
  http.use_ssl = true
  http.start { |http|
    http.read_timeout = 120
    http.request(req)
  }
end

#login_to_serverObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/site.rb', line 28

def 
  req = Net::HTTP::Post.new("/")
  body = "__vae_local=#{session_id}&__local_version=#{VER}"
  if auth_key
    req.body = "#{body}&__local_auth_key=#{CGI.escape(auth_key)}"
  else
    req.body = "#{body}&__local_username2=#{CGI.escape(username)}&__local_password=#{CGI.escape(password)}"
  end
  res = fetch_from_server(req)
  data = JSON.parse(res.body)
  if data == nil or data == []
    VaeLocal.write_auth_key(subdomain, nil)
    raise VaeError, "Invalid password or insufficient permissions."
  elsif data['alert']
    puts data['alert']
  elsif data['valid'] != "valid"
    VaeLocal.write_auth_key(subdomain, nil)
    raise VaeError, "Invalid password or insufficient permissions."
  end
  if data['auth_key'] and !auth_key
    VaeLocal.write_auth_key(subdomain, data['auth_key'])
    @auth_key = data['auth_key']
  end
  @data = data
rescue JSON::ParserError
  raise VaeError, "An unknown error occurred signing into Vae Platform.  Please email support for help."
end

#subdomain_baseObject



56
57
58
# File 'lib/site.rb', line 56

def subdomain_base
  subdomain.split(".").first
end

#vaeplatform_urlObject



60
61
62
# File 'lib/site.rb', line 60

def vaeplatform_url
  VaeLocal.vaeplatform_url(subdomain)
end