imagevenue 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,110 +2,7 @@
2
2
 
3
3
  $LOAD_PATH.push(File.expand_path(File.dirname(__FILE__) + '/../lib'))
4
4
 
5
- require 'getoptlong'
6
- #require 'rubygems'
7
5
  require 'image_venue'
6
+ require 'image_venue/cli'
8
7
 
9
-
10
- options = GetoptLong.new(
11
- ['-c', '--cookie', GetoptLong::OPTIONAL_ARGUMENT],
12
- ['-p', '--password', GetoptLong::REQUIRED_ARGUMENT],
13
- ['-u', '--username', GetoptLong::REQUIRED_ARGUMENT]
14
- )
15
- options_hash = {}
16
- options.each do |option, argument|
17
- options_hash[option] = argument
18
- end
19
- command = ARGV.shift
20
- cookie = nil
21
-
22
- if command == 'list'
23
- ImageVenue.authenticate(options_hash['-u'], options_hash['-p'], cookie)
24
- if ImageVenue.authenticate.login
25
- ImageVenue.directory.list.each do |directory|
26
- puts directory
27
- end
28
- exit(0)
29
- else
30
- exit(-10)
31
- end
32
-
33
- elsif command == 'files'
34
- directory = ARGV.shift
35
- unless directory.nil? or directory.empty?
36
- ImageVenue.authenticate(options_hash['-u'], options_hash['-p'], cookie)
37
- if ImageVenue.authenticate.login
38
- ImageVenue.directory.files(directory).each do |file|
39
- puts file.basename
40
- end
41
- exit(0)
42
- else
43
- exit(-10)
44
- end
45
- else
46
- exit(-20)
47
- end
48
-
49
- elsif command == 'create'
50
- directory = ARGV.shift
51
- unless directory.nil? or directory.empty?
52
- ImageVenue.authenticate(options_hash['-u'], options_hash['-p'], cookie)
53
- if ImageVenue.authenticate.login
54
- if ImageVenue.directory.create(directory)
55
- exit(0)
56
- else
57
- exit(-30)
58
- end
59
- else
60
- exit(-10)
61
- end
62
- else
63
- exit(-20)
64
- end
65
-
66
- elsif command == 'upload'
67
- directory = ARGV.shift
68
- files = ARGV.clone
69
- unless directory.nil? or directory.empty? or files.nil? or files.empty?
70
- ImageVenue.authenticate(options_hash['-u'], options_hash['-p'], cookie)
71
- if ImageVenue.authenticate.login
72
- if ImageVenue.directory.select_or_create(directory)
73
- upload = ImageVenue::Upload.new
74
- files.each do |file|
75
- upload_file = ImageVenue::UploadFile.from_local_path(file)
76
- upload.add(upload_file)
77
- end
78
- upload.debug = true
79
- upload.process
80
- exit(0)
81
- else
82
- exit(-30)
83
- end
84
- else
85
- exit(-10)
86
- end
87
- else
88
- exit(-20)
89
- end
90
-
91
- elsif command == 'delete'
92
- directory = ARGV.shift
93
- unless directory.nil? or directory.empty?
94
- ImageVenue.authenticate(options_hash['-u'], options_hash['-p'], cookie)
95
- if ImageVenue.authenticate.login
96
- if ImageVenue.directory.delete(directory)
97
- exit(0)
98
- else
99
- exit(-30)
100
- end
101
- else
102
- exit(-10)
103
- end
104
- else
105
- exit(-20)
106
- end
107
-
108
- else
109
- puts "USAGE: #{$0} -u <USERNAME> -p <PASSWORD> <COMMAND> [<ARGUMENT>]"
110
- exit(-1)
111
- end
8
+ ImageVenue::Cli.main
@@ -7,47 +7,51 @@ require 'uri' unless defined?(URI)
7
7
  require 'rubygems' unless defined?(Gem)
8
8
  require 'hpricot' unless defined?(Hpricot)
9
9
 
10
+ Kernel_File = File
10
11
  module Kernel
11
- class File < File
12
+ class File < Kernel_File
12
13
  end
13
14
  end
14
15
 
15
16
  module ImageVenue
16
- def self.base_url
17
- @base_url ||= 'http://users.imagevenue.com'
18
- end
19
-
20
- def self.base_uri
21
- @base_uri ||= URI.parse(self.base_url)
22
- end
23
-
24
- def self.version
25
- @version ||= [0, 0, 2]
26
- end
27
-
28
- def self.debug
29
- @debug
30
- end
31
-
32
- def self.debug=(debug)
33
- @debug = debug
34
- end
35
-
36
- def self.authenticate(username=nil, password=nil, cookie=nil)
37
- @authenticate ||= ImageVenue::Authenticate.new(username, password, cookie)
38
- end
39
-
40
- def self.directory
41
- @directory ||= ImageVenue::Directory.new
42
- end
43
-
44
- def self.upload
45
- @upload ||= ImageVenue::Upload.new
17
+ class << self
18
+ attr_accessor :debug
19
+
20
+ def debug?
21
+ (self.debug) ? true : false
22
+ end
23
+
24
+ def puts_debug(*args)
25
+ if self.debug?
26
+ puts(*args)
27
+ return true
28
+ else
29
+ return false
30
+ end
31
+ end
32
+
33
+ def base_url
34
+ @base_url ||= 'http://users.imagevenue.com'
35
+ end
36
+
37
+ def base_uri
38
+ @base_uri ||= URI.parse(self.base_url)
39
+ end
40
+
41
+ def version
42
+ @version ||= [0, 1, 0]
43
+ end
44
+
45
+ def login(username, password, &block)
46
+ connection = ImageVenue::Connection.new(username, password)
47
+ if connection.login
48
+ block.call(connection) if block_given?
49
+ end
50
+ return connection
51
+ end
46
52
  end
47
53
  end
48
54
 
49
- require 'image_venue/authenticate'
55
+ require 'image_venue/connection'
50
56
  require 'image_venue/directory'
51
- require 'image_venue/file'
52
- require 'image_venue/upload'
53
- require 'image_venue/upload_file'
57
+ require 'image_venue/file'
@@ -0,0 +1,125 @@
1
+ require 'getoptlong'
2
+
3
+ module ImageVenue
4
+ class Cli
5
+ class << self
6
+ def main
7
+ cli = self.new
8
+ cli.main
9
+ end
10
+ end
11
+
12
+ attr_accessor :options
13
+ attr_accessor :options_hash
14
+ attr_accessor :command
15
+ attr_accessor :arguments
16
+
17
+ def initialize
18
+ self.options = GetoptLong.new(
19
+ ['-d', '--debug', GetoptLong::NO_ARGUMENT],
20
+ ['-p', '--password', GetoptLong::REQUIRED_ARGUMENT],
21
+ ['-u', '--username', GetoptLong::REQUIRED_ARGUMENT]
22
+ )
23
+ self.options_hash = {}
24
+ self.options.each do |option, argument|
25
+ self.options_hash[option] = argument
26
+ end
27
+ self.command = ARGV.shift
28
+ self.arguments = ARGV.clone
29
+ return nil
30
+ end
31
+
32
+ def main
33
+ ImageVenue.debug = self.options_hash.has_key?('-d')
34
+
35
+ if self.command.nil? or self.command.empty?
36
+ puts "USAGE: #{$0} -u <USERNAME> -p <PASSWORD> <COMMAND> [<ARGUMENT>]"
37
+ return false
38
+ else
39
+ command_block = nil
40
+ self.command = self.command.to_sym
41
+
42
+ if self.command == :list
43
+ command_block = lambda do |connection|
44
+ connection.directories.each do |directory|
45
+ puts directory.name
46
+ end
47
+ end
48
+
49
+ elsif self.command == :files
50
+ directory_name = self.arguments.shift
51
+ unless directory_name.nil? or directory_name.empty?
52
+ command_block = lambda do |connection|
53
+ directory = connection.directories.find {|dir| dir.name == directory_name}
54
+ if directory
55
+ directory.files.each do |file|
56
+ puts file.real_name
57
+ end
58
+ else
59
+ puts "NOT_FOUND_ERROR"
60
+ end
61
+ end
62
+ end
63
+
64
+ elsif self.command == :create
65
+ directory_name = self.arguments.shift
66
+ unless directory_name.nil? or directory_name.empty?
67
+ command_block = lambda do |connection|
68
+ directory = ImageVenue::Directory.new(connection, directory_name)
69
+ if directory.save
70
+ puts "OK"
71
+ else
72
+ puts "ERROR"
73
+ end
74
+ end
75
+ end
76
+
77
+ elsif self.command == :upload
78
+ directory_name = self.arguments.shift
79
+ filepathes = self.arguments
80
+ unless directory_name.nil? or directory_name.empty? or filepathes.nil? or filepathes.empty?
81
+ command_block = lambda do |connection|
82
+ directory = connection.directories.find {|dir| dir.name == directory_name}
83
+ if directory
84
+ directory.select
85
+ filepathes.each do |filepath|
86
+ file = ImageVenue::File.new(directory, filepath)
87
+ if file.save
88
+ puts "OK #{filepath}"
89
+ else
90
+ puts "ERROR #{filepath}"
91
+ end
92
+ end
93
+ else
94
+ end
95
+ end
96
+ end
97
+
98
+ elsif self.command == :delete
99
+ directory_name = self.arguments.shift
100
+ unless directory_name.nil? or directory_name.empty?
101
+ command_block = lambda do |connection|
102
+ directory = connection.directories.find {|dir| dir.name == directory_name}
103
+ if directory
104
+ if directory.destroy
105
+ puts "OK"
106
+ else
107
+ puts "ERROR"
108
+ end
109
+ else
110
+ puts "NOT_FOUND_ERROR"
111
+ end
112
+ end
113
+ end
114
+ end
115
+
116
+ if command_block
117
+ ImageVenue.login(options_hash['-u'], options_hash['-p']) do |connection|
118
+ command_block.call(connection)
119
+ connection.logout
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
@@ -1,5 +1,5 @@
1
1
  module ImageVenue
2
- class Authenticate
2
+ class Connection
3
3
  class << self
4
4
  def login_uri
5
5
  @login_uri ||= URI.parse(ImageVenue.base_url + '/process_logon.php')
@@ -32,13 +32,29 @@ module ImageVenue
32
32
  def cookie_timestamp_key
33
33
  @cookie_timestamp_key ||= 'tsctr'
34
34
  end
35
+
36
+ def cookie_directory_key
37
+ @cookie_directory_key ||= 'cur_upload_dir'
38
+ end
39
+
40
+ def cookie_view_directory
41
+ @cookie_view_directory ||= 'view_dir'
42
+ end
43
+
44
+ def login_params(connection)
45
+ {
46
+ self.login_action_key => connection.action,
47
+ self.login_username_key => connection.username,
48
+ self.login_password_key => connection.password
49
+ }
50
+ end
35
51
  end
36
52
 
37
53
  attr_accessor :action
38
- attr_accessor :username
39
- attr_accessor :password
40
54
  attr_accessor :cookie
41
55
  attr_accessor :debug
56
+ attr_accessor :password
57
+ attr_accessor :username
42
58
 
43
59
  def initialize(username, password, cookie=nil)
44
60
  self.action = '1'
@@ -49,7 +65,7 @@ module ImageVenue
49
65
  end
50
66
 
51
67
  def debug?
52
- (ImageVenue.debug or self.debug) ? true : false
68
+ (ImageVenue.debug or self.debug)
53
69
  end
54
70
 
55
71
  def puts_debug(*args)
@@ -62,8 +78,8 @@ module ImageVenue
62
78
  end
63
79
 
64
80
  def login
65
- puts_debug "Trying to login ..."
66
- response = Net::HTTP.post_form(self.class.login_uri, self.login_params)
81
+ self.puts_debug "Trying to login ..."
82
+ response = Net::HTTP.post_form(self.class.login_uri, self.class.login_params(self))
67
83
  unless response.header['Set-Cookie'].nil? or response.header['Set-Cookie'].empty?
68
84
  self.cookie = {}
69
85
  self.cookie[self.class.cookie_user_key] = $1 if response.header['Set-Cookie'] =~ /#{Regexp.escape(self.class.cookie_user_key)}=(.+?)[,;]/
@@ -71,10 +87,10 @@ module ImageVenue
71
87
  response_timestamp = Net::HTTP.get_response(ImageVenue.base_uri)
72
88
  unless response_timestamp.header['Set-Cookie'].nil? or response_timestamp.header['Set-Cookie'].empty?
73
89
  self.cookie[self.class.cookie_timestamp_key] = $1 if response_timestamp.header['Set-Cookie'] =~ /#{Regexp.escape(self.class.cookie_timestamp_key)}=(.+?)[,;]/
74
- puts_debug "Login successfull!"
90
+ self.puts_debug "Login successfull!"
75
91
  return true
76
92
  else
77
- puts_debug "Login failed!"
93
+ self.puts_debug "Login failed!"
78
94
  return false
79
95
  end
80
96
  else
@@ -83,14 +99,6 @@ module ImageVenue
83
99
  end
84
100
  end
85
101
 
86
- def login_params
87
- {
88
- self.class.login_action_key => self.action,
89
- self.class.login_username_key => self.username,
90
- self.class.login_password_key => self.password
91
- }
92
- end
93
-
94
102
  def logged_in?
95
103
  unless self.cookie.nil?
96
104
  return true
@@ -100,19 +108,59 @@ module ImageVenue
100
108
  end
101
109
 
102
110
  def logout
103
- puts_debug "Trying to logout ..."
111
+ self.puts_debug "Trying to logout ..."
104
112
  response = Net::HTTP.get_response(self.class.logout_uri)
105
113
  if response.is_a?(Net::HTTPSuccess) or response.is_a?(Net::HTTPFound)
106
114
  self.cookie = nil
107
- puts_debug "Logout successfull! Resetting cookie."
115
+ self.puts_debug "Logout successfull! Resetting cookie."
108
116
  return true
109
117
  else
110
118
  self.cookie = nil
111
- puts_debug "Logout failed! But resetting cookie anyway."
119
+ self.puts_debug "Logout failed! But resetting cookie anyway."
112
120
  return false
113
121
  end
114
122
  end
115
123
 
124
+ def selected_directory
125
+ if self.cookie.is_a?(Hash)
126
+ return self.cookie[self.class.cookie_directory_key]
127
+ else
128
+ return nil
129
+ end
130
+ end
131
+
132
+ def selected_directory=(directory)
133
+ if self.cookie.is_a?(Hash)
134
+ if directory.nil?
135
+ return self.cookie.delete(self.class.cookie_directory_key)
136
+ else
137
+ return self.cookie[self.class.cookie_directory_key] = directory
138
+ end
139
+ else
140
+ return nil
141
+ end
142
+ end
143
+
144
+ def view_directory
145
+ if self.cookie.is_a?(Hash)
146
+ return self.cookie[self.class.cookie_view_directory]
147
+ else
148
+ return nil
149
+ end
150
+ end
151
+
152
+ def view_directory(directory)
153
+ if self.cookie.is_a?(Hash)
154
+ if directory.nil?
155
+ return self.cookie.delete(self.class.cookie_view_directory)
156
+ else
157
+ return self.cookie[self.class.cookie_view_directory] = directory
158
+ end
159
+ else
160
+ return nil
161
+ end
162
+ end
163
+
116
164
  def cookie_as_string
117
165
  unless self.cookie.nil?
118
166
  cookie_str = self.cookie.map {|key, value| [key, value].join('=')}.join('; ')
@@ -121,5 +169,19 @@ module ImageVenue
121
169
  return nil
122
170
  end
123
171
  end
172
+
173
+ def directories(reload=false)
174
+ if reload or @directories.nil?
175
+ @directories = ImageVenue::Directory.list(self)
176
+ else
177
+ self.puts_debug "Directories from cache!"
178
+ end
179
+ return @directories
180
+ end
181
+
182
+ def clear_cache
183
+ @directories = nil
184
+ self.puts_debug "Connection's cache cleared."
185
+ end
124
186
  end
125
187
  end