Class: ImageVenue::Connection

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, cookie = nil) ⇒ Connection

Returns a new instance of Connection.



59
60
61
62
63
64
65
# File 'lib/image_venue/connection.rb', line 59

def initialize(username, password, cookie=nil)
  self.action = '1'
  self.username = username
  self.password = password
  self.cookie = cookie
  return nil
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



53
54
55
# File 'lib/image_venue/connection.rb', line 53

def action
  @action
end

Returns the value of attribute cookie.



54
55
56
# File 'lib/image_venue/connection.rb', line 54

def cookie
  @cookie
end

#debugObject

Returns the value of attribute debug.



55
56
57
# File 'lib/image_venue/connection.rb', line 55

def debug
  @debug
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#usernameObject

Returns the value of attribute username.



57
58
59
# File 'lib/image_venue/connection.rb', line 57

def username
  @username
end

Class Method Details



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

def cookie_directory_key
  @cookie_directory_key ||= 'cur_upload_dir'
end


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

def cookie_root_key
  @cookie_root_key ||= 'root'
end


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

def cookie_timestamp_key
  @cookie_timestamp_key ||= 'tsctr'
end


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

def cookie_user_key
  @cookie_user_key ||= 'user'
end


40
41
42
# File 'lib/image_venue/connection.rb', line 40

def cookie_view_directory
  @cookie_view_directory ||= 'view_dir'
end

.login_action_keyObject



16
17
18
# File 'lib/image_venue/connection.rb', line 16

def 
  @login_action_key ||= 'action'
end

.login_params(connection) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/image_venue/connection.rb', line 44

def (connection)
  {
    self. => connection.action,
    self. => connection.username,
    self. => connection.password
  }
end

.login_password_keyObject



12
13
14
# File 'lib/image_venue/connection.rb', line 12

def 
  @login_password_key ||= 'password'
end

.login_uriObject



4
5
6
# File 'lib/image_venue/connection.rb', line 4

def 
  @login_uri ||= URI.parse(ImageVenue.base_url + '/process_logon.php')
end

.login_username_keyObject



8
9
10
# File 'lib/image_venue/connection.rb', line 8

def 
  @login_username_key ||= 'user'
end

.logout_uriObject



20
21
22
# File 'lib/image_venue/connection.rb', line 20

def logout_uri
  @logout_uri ||= URI.parse(ImageVenue.base_url + '/logout.php')
end

Instance Method Details

#clear_cacheObject



182
183
184
185
# File 'lib/image_venue/connection.rb', line 182

def clear_cache
  @directories = nil
  self.puts_debug "Connection's cache cleared."
end


164
165
166
167
168
169
170
171
# File 'lib/image_venue/connection.rb', line 164

def cookie_as_string
  unless self.cookie.nil?
    cookie_str = self.cookie.map {|key, value| [key, value].join('=')}.join('; ')
    return cookie_str
  else
    return nil
  end
end

#debug?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/image_venue/connection.rb', line 67

def debug?
  (ImageVenue.debug or self.debug)
end

#directories(reload = false) ⇒ Object



173
174
175
176
177
178
179
180
# File 'lib/image_venue/connection.rb', line 173

def directories(reload=false)
  if reload or @directories.nil?
    @directories = ImageVenue::Directory.list(self)
  else
    self.puts_debug "Directories from cache!"
  end
  return @directories
end

#logged_in?Boolean

Returns:

  • (Boolean)


102
103
104
105
106
107
108
# File 'lib/image_venue/connection.rb', line 102

def logged_in?
  unless self.cookie.nil?
    return true
  else
    return false
  end
end

#loginObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/image_venue/connection.rb', line 80

def 
  self.puts_debug "Trying to login ..."
  response = Net::HTTP.post_form(self.class., self.class.(self))
  unless response.header['Set-Cookie'].nil? or response.header['Set-Cookie'].empty?
    self.cookie = {}
    self.cookie[self.class.cookie_user_key] = $1 if response.header['Set-Cookie'] =~ /#{Regexp.escape(self.class.cookie_user_key)}=(.+?)[,;]/
    self.cookie[self.class.cookie_root_key] = $1 if response.header['Set-Cookie'] =~ /#{Regexp.escape(self.class.cookie_root_key)}=(\d+)/
    response_timestamp = Net::HTTP.get_response(ImageVenue.base_uri)
    unless response_timestamp.header['Set-Cookie'].nil? or response_timestamp.header['Set-Cookie'].empty?
      self.cookie[self.class.cookie_timestamp_key] = $1 if response_timestamp.header['Set-Cookie'] =~ /#{Regexp.escape(self.class.cookie_timestamp_key)}=(.+?)[,;]/
      self.puts_debug "Login successfull!"
      return true
    else
      self.puts_debug "Login failed!"
      return false
    end
  else
    self.cookie = nil
    return false
  end
end

#logoutObject



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/image_venue/connection.rb', line 110

def logout
  self.puts_debug "Trying to logout ..."
  response = Net::HTTP.get_response(self.class.logout_uri)
  if response.is_a?(Net::HTTPSuccess) or response.is_a?(Net::HTTPFound)
    self.cookie = nil
    self.puts_debug "Logout successfull! Resetting cookie."
    return true
  else
    self.cookie = nil
    self.puts_debug "Logout failed! But resetting cookie anyway."
    return false
  end
end

#puts_debug(*args) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/image_venue/connection.rb', line 71

def puts_debug(*args)
  if self.debug?
    puts(*args)
    return true
  else
    return false
  end
end

#selected_directoryObject



124
125
126
127
128
129
130
# File 'lib/image_venue/connection.rb', line 124

def selected_directory
  if self.cookie.is_a?(Hash)
    return self.cookie[self.class.cookie_directory_key]
  else
    return nil
  end
end

#selected_directory=(directory) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/image_venue/connection.rb', line 132

def selected_directory=(directory)
  if self.cookie.is_a?(Hash)
    if directory.nil?
      return self.cookie.delete(self.class.cookie_directory_key)
    else
      return self.cookie[self.class.cookie_directory_key] = directory
    end
  else
    return nil
  end
end

#view_directory(directory) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/image_venue/connection.rb', line 144

def view_directory
  if self.cookie.is_a?(Hash)
    return self.cookie[self.class.cookie_view_directory]
  else
    return nil
  end
end