Class: ImageVenue::Connection
- Inherits:
-
Object
- Object
- ImageVenue::Connection
- Defined in:
- lib/image_venue/connection.rb
Instance Attribute Summary collapse
-
#action ⇒ Object
Returns the value of attribute action.
-
#cookie ⇒ Object
Returns the value of attribute cookie.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#password ⇒ Object
Returns the value of attribute password.
-
#username ⇒ Object
Returns the value of attribute username.
Class Method Summary collapse
- .cookie_directory_key ⇒ Object
- .cookie_root_key ⇒ Object
- .cookie_timestamp_key ⇒ Object
- .cookie_user_key ⇒ Object
- .cookie_view_directory ⇒ Object
- .login_action_key ⇒ Object
- .login_params(connection) ⇒ Object
- .login_password_key ⇒ Object
- .login_uri ⇒ Object
- .login_username_key ⇒ Object
- .logout_uri ⇒ Object
Instance Method Summary collapse
- #clear_cache ⇒ Object
- #cookie_as_string ⇒ Object
- #debug? ⇒ Boolean
- #directories(reload = false) ⇒ Object
-
#initialize(username, password, cookie = nil) ⇒ Connection
constructor
A new instance of Connection.
- #logged_in? ⇒ Boolean
- #login ⇒ Object
- #logout ⇒ Object
- #puts_debug(*args) ⇒ Object
- #selected_directory ⇒ Object
- #selected_directory=(directory) ⇒ Object
- #view_directory(directory) ⇒ Object
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, =nil) self.action = '1' self.username = username self.password = password self. = return nil end |
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action.
53 54 55 |
# File 'lib/image_venue/connection.rb', line 53 def action @action end |
#cookie ⇒ Object
Returns the value of attribute cookie.
54 55 56 |
# File 'lib/image_venue/connection.rb', line 54 def end |
#debug ⇒ Object
Returns the value of attribute debug.
55 56 57 |
# File 'lib/image_venue/connection.rb', line 55 def debug @debug end |
#password ⇒ Object
Returns the value of attribute password.
56 57 58 |
# File 'lib/image_venue/connection.rb', line 56 def password @password end |
#username ⇒ Object
Returns the value of attribute username.
57 58 59 |
# File 'lib/image_venue/connection.rb', line 57 def username @username end |
Class Method Details
.cookie_directory_key ⇒ Object
36 37 38 |
# File 'lib/image_venue/connection.rb', line 36 def ||= 'cur_upload_dir' end |
.cookie_root_key ⇒ Object
28 29 30 |
# File 'lib/image_venue/connection.rb', line 28 def ||= 'root' end |
.cookie_timestamp_key ⇒ Object
32 33 34 |
# File 'lib/image_venue/connection.rb', line 32 def ||= 'tsctr' end |
.cookie_user_key ⇒ Object
24 25 26 |
# File 'lib/image_venue/connection.rb', line 24 def ||= 'user' end |
.cookie_view_directory ⇒ Object
40 41 42 |
# File 'lib/image_venue/connection.rb', line 40 def ||= 'view_dir' end |
.login_action_key ⇒ Object
16 17 18 |
# File 'lib/image_venue/connection.rb', line 16 def login_action_key @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 login_params(connection) { self.login_action_key => connection.action, self.login_username_key => connection.username, self.login_password_key => connection.password } end |
.login_password_key ⇒ Object
12 13 14 |
# File 'lib/image_venue/connection.rb', line 12 def login_password_key @login_password_key ||= 'password' end |
.login_uri ⇒ Object
4 5 6 |
# File 'lib/image_venue/connection.rb', line 4 def login_uri @login_uri ||= URI.parse(ImageVenue.base_url + '/process_logon.php') end |
.login_username_key ⇒ Object
8 9 10 |
# File 'lib/image_venue/connection.rb', line 8 def login_username_key @login_username_key ||= 'user' end |
.logout_uri ⇒ Object
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_cache ⇒ Object
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 |
#cookie_as_string ⇒ Object
164 165 166 167 168 169 170 171 |
# File 'lib/image_venue/connection.rb', line 164 def unless self..nil? = self..map {|key, value| [key, value].join('=')}.join('; ') return else return nil end end |
#debug? ⇒ 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
102 103 104 105 106 107 108 |
# File 'lib/image_venue/connection.rb', line 102 def logged_in? unless self..nil? return true else return false end end |
#login ⇒ Object
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 login self.puts_debug "Trying to login ..." response = Net::HTTP.post_form(self.class.login_uri, self.class.login_params(self)) unless response.header['Set-Cookie'].nil? or response.header['Set-Cookie'].empty? self. = {} self.[self.class.] = $1 if response.header['Set-Cookie'] =~ /#{Regexp.escape(self.class.cookie_user_key)}=(.+?)[,;]/ self.[self.class.] = $1 if response.header['Set-Cookie'] =~ /#{Regexp.escape(self.class.cookie_root_key)}=(\d+)/ = Net::HTTP.get_response(ImageVenue.base_uri) unless .header['Set-Cookie'].nil? or .header['Set-Cookie'].empty? self.[self.class.] = $1 if .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. = nil return false end end |
#logout ⇒ Object
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. = nil self.puts_debug "Logout successfull! Resetting cookie." return true else self. = 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_directory ⇒ Object
124 125 126 127 128 129 130 |
# File 'lib/image_venue/connection.rb', line 124 def selected_directory if self..is_a?(Hash) return self.[self.class.] 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..is_a?(Hash) if directory.nil? return self..delete(self.class.) else return self.[self.class.] = 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..is_a?(Hash) return self.[self.class.] else return nil end end |