Class: Wagon::Connection

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

Constant Summary collapse

HOST =
'secure.lds.org'
LOGIN_PATH =
'/units/a/login/1,21568,779-1,00.html?URL='
CACHE_PATH =
File.join(File.expand_path('~'), '.wagon_cache')

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Connection

Returns a new instance of Connection.



16
17
18
19
20
21
22
# File 'lib/wagon/connection.rb', line 16

def initialize(username, password)
  response    = post(LOGIN_PATH, 'username' => username, 'password' => password)
  @cookies    = response['set-cookie']
  @home_path  = URI.parse(response['location']).path

  raise AuthenticationFailure.new("Invalid username and/or password") unless @cookies
end

Class Method Details

._load(string) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/wagon/connection.rb', line 66

def self._load(string)
  attributes = Marshal.restore(string)
  connection = Connection.allocate()
  connection.instance_variable_set(:@cookies, attributes.shift)
  connection.instance_variable_set(:@home_path, attributes.shift)
  connection
end

.perform_caching(true_or_false) ⇒ Object



52
53
54
# File 'lib/wagon/connection.rb', line 52

def self.perform_caching(true_or_false)
  @@perform_caching = true_or_false
end

.perform_caching?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/wagon/connection.rb', line 48

def self.perform_caching?
  @@perform_caching ||= true
end

Instance Method Details

#_dump(depth) ⇒ Object



62
63
64
# File 'lib/wagon/connection.rb', line 62

def _dump(depth)
  Marshal.dump([@cookies, @home_path])
end

#get(path) ⇒ Object



32
33
34
# File 'lib/wagon/connection.rb', line 32

def get(path)
  Connection.perform_caching? ? get_with_caching(path) : get_without_caching(path)
end

#get_with_caching(path) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/wagon/connection.rb', line 40

def get_with_caching(path)
  FileUtils::mkdir(CACHE_PATH) unless File.directory?(CACHE_PATH)
  cache_path = File.join(CACHE_PATH, Digest::SHA1.hexdigest(path) + ".cache")
  return open(cache_path).read if File.exists?(cache_path)
  open(cache_path, "w").write(data = get_without_caching(path))
  data
end

#get_without_caching(path) ⇒ Object



36
37
38
# File 'lib/wagon/connection.rb', line 36

def get_without_caching(path)
  _http.request(Net::HTTP::Get.new(path, {'Cookie' => @cookies || ''})).body
end

#home_pathObject



24
25
26
# File 'lib/wagon/connection.rb', line 24

def home_path
  @home_path
end

#post(path, data) ⇒ Object



56
57
58
59
60
# File 'lib/wagon/connection.rb', line 56

def post(path, data)
  request = Net::HTTP::Post.new(path, {'Cookie' => @cookies || ''})
  request.set_form_data(data)
  _http.request(request)
end

#wardObject



28
29
30
# File 'lib/wagon/connection.rb', line 28

def ward
  @ward ||= Ward.new(self, home_path)
end