brick_ftp 0.8.4 → 1.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +0 -5
  3. data/.rubocop.yml +2 -1
  4. data/.yardopts +1 -0
  5. data/CHANGELOG.md +16 -12
  6. data/Gemfile +2 -0
  7. data/README.md +24 -43
  8. data/Rakefile +2 -0
  9. data/bin/console +1 -0
  10. data/brick_ftp.gemspec +10 -9
  11. data/exe/brick_ftp +1 -0
  12. data/lib/brick_ftp.rb +5 -78
  13. data/lib/brick_ftp/client.rb +35 -411
  14. data/lib/brick_ftp/core_ext.rb +8 -0
  15. data/lib/brick_ftp/core_ext/hash.rb +13 -0
  16. data/lib/brick_ftp/core_ext/hash/compact.rb +13 -0
  17. data/lib/brick_ftp/core_ext/struct.rb +13 -0
  18. data/lib/brick_ftp/core_ext/struct/new.rb +16 -0
  19. data/lib/brick_ftp/restful_api.rb +82 -0
  20. data/lib/brick_ftp/restful_api/add_group_member.rb +44 -0
  21. data/lib/brick_ftp/restful_api/client.rb +179 -0
  22. data/lib/brick_ftp/restful_api/command.rb +21 -0
  23. data/lib/brick_ftp/restful_api/complete_upload.rb +46 -0
  24. data/lib/brick_ftp/restful_api/continue_upload.rb +50 -0
  25. data/lib/brick_ftp/restful_api/copy_folder.rb +46 -0
  26. data/lib/brick_ftp/restful_api/count_folder_contents.rb +36 -0
  27. data/lib/brick_ftp/restful_api/count_users.rb +22 -0
  28. data/lib/brick_ftp/restful_api/create_api_key.rb +46 -0
  29. data/lib/brick_ftp/restful_api/create_behavior.rb +42 -0
  30. data/lib/brick_ftp/restful_api/create_bundle.rb +40 -0
  31. data/lib/brick_ftp/restful_api/create_folder.rb +25 -0
  32. data/lib/brick_ftp/restful_api/create_group.rb +42 -0
  33. data/lib/brick_ftp/restful_api/create_notification.rb +44 -0
  34. data/lib/brick_ftp/restful_api/create_permission.rb +48 -0
  35. data/lib/brick_ftp/restful_api/create_public_key.rb +42 -0
  36. data/lib/brick_ftp/restful_api/create_user.rb +90 -0
  37. data/lib/brick_ftp/restful_api/create_user_in_group.rb +60 -0
  38. data/lib/brick_ftp/restful_api/delete_api_key.rb +23 -0
  39. data/lib/brick_ftp/restful_api/delete_behavior.rb +22 -0
  40. data/lib/brick_ftp/restful_api/delete_bundle.rb +22 -0
  41. data/lib/brick_ftp/restful_api/delete_folder.rb +32 -0
  42. data/lib/brick_ftp/restful_api/delete_group.rb +23 -0
  43. data/lib/brick_ftp/restful_api/delete_notification.rb +23 -0
  44. data/lib/brick_ftp/restful_api/delete_permission.rb +23 -0
  45. data/lib/brick_ftp/restful_api/delete_public_key.rb +23 -0
  46. data/lib/brick_ftp/restful_api/delete_user.rb +25 -0
  47. data/lib/brick_ftp/restful_api/download_file.rb +44 -0
  48. data/lib/brick_ftp/restful_api/get_api_key.rb +25 -0
  49. data/lib/brick_ftp/restful_api/get_behavior.rb +25 -0
  50. data/lib/brick_ftp/restful_api/get_bundle.rb +25 -0
  51. data/lib/brick_ftp/restful_api/get_bundle_zip.rb +48 -0
  52. data/lib/brick_ftp/restful_api/get_file_in_bundle.rb +49 -0
  53. data/lib/brick_ftp/restful_api/get_folder_size.rb +25 -0
  54. data/lib/brick_ftp/restful_api/get_group.rb +26 -0
  55. data/lib/brick_ftp/restful_api/get_public_key.rb +25 -0
  56. data/lib/brick_ftp/restful_api/get_site_usage.rb +19 -0
  57. data/lib/brick_ftp/restful_api/get_user.rb +26 -0
  58. data/lib/brick_ftp/restful_api/list_api_keys.rb +25 -0
  59. data/lib/brick_ftp/restful_api/list_behaviors.rb +23 -0
  60. data/lib/brick_ftp/restful_api/list_bundle_contents.rb +58 -0
  61. data/lib/brick_ftp/restful_api/list_bundles.rb +23 -0
  62. data/lib/brick_ftp/restful_api/list_folder_behaviors.rb +39 -0
  63. data/lib/brick_ftp/restful_api/list_folders.rb +108 -0
  64. data/lib/brick_ftp/restful_api/list_groups.rb +23 -0
  65. data/lib/brick_ftp/restful_api/list_notifications.rb +23 -0
  66. data/lib/brick_ftp/restful_api/list_permissions.rb +41 -0
  67. data/lib/brick_ftp/restful_api/list_public_keys.rb +25 -0
  68. data/lib/brick_ftp/restful_api/list_users.rb +60 -0
  69. data/lib/brick_ftp/restful_api/move_folder.rb +41 -0
  70. data/lib/brick_ftp/restful_api/remove_group_member.rb +23 -0
  71. data/lib/brick_ftp/restful_api/retrieve_file_history.rb +41 -0
  72. data/lib/brick_ftp/restful_api/retrieve_folder_history.rb +41 -0
  73. data/lib/brick_ftp/restful_api/retrieve_history.rb +54 -0
  74. data/lib/brick_ftp/restful_api/retrieve_login_history.rb +38 -0
  75. data/lib/brick_ftp/restful_api/retrieve_site_history.rb +38 -0
  76. data/lib/brick_ftp/restful_api/retrieve_user_history.rb +40 -0
  77. data/lib/brick_ftp/restful_api/search_user.rb +28 -0
  78. data/lib/brick_ftp/restful_api/start_upload.rb +27 -0
  79. data/lib/brick_ftp/restful_api/unlock_user.rb +25 -0
  80. data/lib/brick_ftp/restful_api/update_behavior.rb +39 -0
  81. data/lib/brick_ftp/restful_api/update_group.rb +44 -0
  82. data/lib/brick_ftp/restful_api/update_group_member.rb +40 -0
  83. data/lib/brick_ftp/restful_api/update_user.rb +101 -0
  84. data/lib/brick_ftp/restful_api/upload_file.rb +65 -0
  85. data/lib/brick_ftp/types.rb +23 -0
  86. data/lib/brick_ftp/types/behavior.rb +27 -0
  87. data/lib/brick_ftp/types/bundle.rb +34 -0
  88. data/lib/brick_ftp/types/bundle_content.rb +25 -0
  89. data/lib/brick_ftp/types/bundle_zip.rb +19 -0
  90. data/lib/brick_ftp/types/file.rb +45 -0
  91. data/lib/brick_ftp/types/file_in_bundle.rb +25 -0
  92. data/lib/brick_ftp/types/folder_contents_count.rb +24 -0
  93. data/lib/brick_ftp/types/group.rb +31 -0
  94. data/lib/brick_ftp/types/group_membership.rb +27 -0
  95. data/lib/brick_ftp/types/history.rb +43 -0
  96. data/lib/brick_ftp/types/notification.rb +31 -0
  97. data/lib/brick_ftp/types/permission.rb +35 -0
  98. data/lib/brick_ftp/types/site_usage.rb +23 -0
  99. data/lib/brick_ftp/types/upload.rb +45 -0
  100. data/lib/brick_ftp/types/user.rb +79 -0
  101. data/lib/brick_ftp/types/user_api_key.rb +33 -0
  102. data/lib/brick_ftp/types/user_public_key.rb +28 -0
  103. data/lib/brick_ftp/utils.rb +2 -0
  104. data/lib/brick_ftp/utils/chunk_io.rb +5 -4
  105. data/lib/brick_ftp/version.rb +3 -1
  106. metadata +123 -86
  107. data/lib/brick_ftp/api.rb +0 -9
  108. data/lib/brick_ftp/api/authentication.rb +0 -28
  109. data/lib/brick_ftp/api/authentication/session.rb +0 -25
  110. data/lib/brick_ftp/api/base.rb +0 -119
  111. data/lib/brick_ftp/api/behavior.rb +0 -19
  112. data/lib/brick_ftp/api/bundle.rb +0 -17
  113. data/lib/brick_ftp/api/bundle_content.rb +0 -18
  114. data/lib/brick_ftp/api/bundle_download.rb +0 -18
  115. data/lib/brick_ftp/api/file.rb +0 -20
  116. data/lib/brick_ftp/api/file_operation.rb +0 -6
  117. data/lib/brick_ftp/api/file_operation/copy.rb +0 -10
  118. data/lib/brick_ftp/api/file_operation/move.rb +0 -10
  119. data/lib/brick_ftp/api/file_operation/upload.rb +0 -66
  120. data/lib/brick_ftp/api/file_operation/uploading_result.rb +0 -22
  121. data/lib/brick_ftp/api/file_operation/uploading_session.rb +0 -95
  122. data/lib/brick_ftp/api/folder.rb +0 -22
  123. data/lib/brick_ftp/api/folder_behavior.rb +0 -12
  124. data/lib/brick_ftp/api/group.rb +0 -16
  125. data/lib/brick_ftp/api/history.rb +0 -6
  126. data/lib/brick_ftp/api/history/file.rb +0 -23
  127. data/lib/brick_ftp/api/history/folder.rb +0 -23
  128. data/lib/brick_ftp/api/history/login.rb +0 -23
  129. data/lib/brick_ftp/api/history/site.rb +0 -23
  130. data/lib/brick_ftp/api/history/user.rb +0 -23
  131. data/lib/brick_ftp/api/notification.rb +0 -14
  132. data/lib/brick_ftp/api/permission.rb +0 -17
  133. data/lib/brick_ftp/api/public_key.rb +0 -15
  134. data/lib/brick_ftp/api/site_usage.rb +0 -21
  135. data/lib/brick_ftp/api/user.rb +0 -36
  136. data/lib/brick_ftp/api_component.rb +0 -81
  137. data/lib/brick_ftp/api_definition.rb +0 -65
  138. data/lib/brick_ftp/cli.rb +0 -7
  139. data/lib/brick_ftp/cli/config.rb +0 -20
  140. data/lib/brick_ftp/cli/main.rb +0 -26
  141. data/lib/brick_ftp/cli/site.rb +0 -10
  142. data/lib/brick_ftp/configuration.rb +0 -121
  143. data/lib/brick_ftp/http_client.rb +0 -130
  144. data/lib/brick_ftp/log_formatter.rb +0 -15
  145. data/lib/brick_ftp/webhook.rb +0 -4
  146. data/lib/brick_ftp/webhook/request.rb +0 -28
data/lib/brick_ftp/cli.rb DELETED
@@ -1,7 +0,0 @@
1
- module BrickFTP
2
- module CLI
3
- def self.start(argv)
4
- Main.start(argv)
5
- end
6
- end
7
- end
@@ -1,20 +0,0 @@
1
- module BrickFTP
2
- module CLI
3
- class Config < Thor
4
- desc 'get NAME', 'Show specified configuration.'
5
- def get(name)
6
- abort 'No such configuration.' unless BrickFTP.config.respond_to?(name)
7
-
8
- puts BrickFTP.config.send(name)
9
- end
10
-
11
- desc 'set NAME VALUE', 'Set configuration value.'
12
- def set(name, value)
13
- abort 'No such configuration.' unless BrickFTP.config.respond_to?("#{name}=")
14
-
15
- BrickFTP.config.send("#{name}=", value)
16
- BrickFTP.config.save!
17
- end
18
- end
19
- end
20
- end
@@ -1,26 +0,0 @@
1
- module BrickFTP
2
- module CLI
3
- class Main < Thor
4
- class_option :profile,
5
- desc: 'Configuration profile.',
6
- default: 'global',
7
- type: :string
8
-
9
- class_option :config,
10
- desc: 'Path for configuration file.',
11
- default: BrickFTP::Configuration::CONFIG_FILE_PATH,
12
- type: :string
13
-
14
- def initialize(*args)
15
- super
16
- BrickFTP.config = BrickFTP::Configuration.new(profile: options[:profile], config_file_path: options[:config])
17
- end
18
-
19
- desc 'config COMMAND', 'Manage configuration.'
20
- subcommand 'config', BrickFTP::CLI::Config
21
-
22
- desc 'site COMMAND', 'Manage site.'
23
- subcommand 'site', BrickFTP::CLI::Site
24
- end
25
- end
26
- end
@@ -1,10 +0,0 @@
1
- module BrickFTP
2
- module CLI
3
- class Site < Thor
4
- desc 'usage', 'Show site usage.'
5
- def usage
6
- puts BrickFTP::API::SiteUsage.find.to_json
7
- end
8
- end
9
- end
10
- end
@@ -1,121 +0,0 @@
1
- require 'logger'
2
- require 'inifile'
3
-
4
- module BrickFTP
5
- class Configuration
6
- # Name of configuration profile.
7
- # @return [String] profile
8
- attr_reader :profile
9
- # Path for configuration file.
10
- # @return [String] path
11
- attr_reader :config_file_path
12
- # Subdomain of API endpoint. If not specified, get value from enviroment variable `BRICK_FTP_SUBDOMAIN`.
13
- # @return [String] subdomain
14
- attr_accessor :subdomain
15
- # Access key of API. If not specified, get value from enviroment variable `BRICK_FTP_API_KEY`.
16
- # @return [String] API access key
17
- attr_accessor :api_key
18
- # Authentication session for cookie auth.
19
- # @return [BrickFTP::API::Authentication::Session] authentication session object.
20
- attr_accessor :session
21
- # Logger.
22
- # @return [Logger] logger
23
- attr_accessor :logger
24
- # Log level
25
- # @return [Integer]
26
- attr_reader :log_level
27
- # Log formatter
28
- # @return [Logger::Formatter]
29
- attr_reader :log_formatter
30
- # Open timeout
31
- # @return [Integer]
32
- attr_accessor :open_timeout
33
- # Read timeout
34
- # @return [Integer]
35
- attr_accessor :read_timeout
36
-
37
- DEFAULT_OPEN_TIMEOUT = 10
38
- DEFAULT_READ_TIMEOUT = 30
39
-
40
- DEFAULT_PROFILE = 'global'.freeze
41
- # Name of storable configurations. (TODO: log_path, log_level, log_formatter)
42
- STORABLE_CONFIGURATION_KEYS = %w[subdomain api_key open_timeout read_timeout].freeze
43
-
44
- STORABLE_CONFIGURATION_KEYS.each do |name|
45
- define_method("#{name}=") do |value|
46
- dirty_attributes.add(name)
47
- instance_variable_set(:"@#{name}", value)
48
- end
49
- end
50
-
51
- def self.default_config_file_path
52
- File.expand_path('~/.brick_ftp/config')
53
- rescue ArgumentError
54
- # couldn't find login name -- expanding `~'
55
- nil
56
- end
57
- CONFIG_FILE_PATH = default_config_file_path
58
-
59
- def initialize(profile: DEFAULT_PROFILE, config_file_path: CONFIG_FILE_PATH)
60
- @profile = profile
61
- @config_file_path = config_file_path
62
- load_config_file(config_file_path)
63
-
64
- self.subdomain = ENV.fetch('BRICK_FTP_SUBDOMAIN', inifile[profile]['subdomain'])
65
- self.api_key = ENV.fetch('BRICK_FTP_API_KEY', inifile[profile]['api_key'])
66
- self.session = nil
67
- self.open_timeout = (inifile[profile]['open_timeout'] || DEFAULT_OPEN_TIMEOUT).to_i
68
- self.read_timeout = (inifile[profile]['read_timeout'] || DEFAULT_READ_TIMEOUT).to_i
69
- self.logger = Logger.new(STDOUT)
70
- self.log_level = Logger::WARN
71
- self.log_formatter = Logger::Formatter.new
72
- logger.level = log_level
73
-
74
- dirty_attributes.clear
75
- end
76
-
77
- # Host name of API endpoint.
78
- # @return [String] API host
79
- def api_host
80
- "#{subdomain}.brickftp.com"
81
- end
82
-
83
- # Set log level to logger object.
84
- # @param level [Integer] log level.
85
- def log_level=(level)
86
- @log_level = level
87
- logger.level = @log_level
88
- end
89
-
90
- # Set log formatter to logger object.
91
- # @param formatter [Logger::Formatter]
92
- def log_formatter=(formatter)
93
- @log_formatter = formatter
94
- logger.formatter = @log_formatter
95
- end
96
-
97
- def save!
98
- dirty_attributes.each do |key|
99
- inifile[profile][key] = send(key)
100
- end
101
- FileUtils.mkdir_p(File.dirname(inifile.filename)) unless File.exist?(File.dirname(inifile.filename))
102
- inifile.save
103
- end
104
-
105
- private
106
-
107
- attr_reader :inifile
108
-
109
- def load_config_file(config_file_path)
110
- @inifile = if config_file_path && File.exist?(config_file_path)
111
- IniFile.load(config_file_path, encoding: 'UTF-8')
112
- else
113
- IniFile.new(filename: config_file_path, encoding: 'UTF-8')
114
- end
115
- end
116
-
117
- def dirty_attributes
118
- @dirty_attributes ||= Set.new
119
- end
120
- end
121
- end
@@ -1,130 +0,0 @@
1
- require 'net/https'
2
- require 'json'
3
- require 'cgi'
4
-
5
- module BrickFTP
6
- class HTTPClient
7
- class Error < StandardError
8
- def initialize(response)
9
- begin
10
- error = JSON.parse(response.body)
11
- rescue StandardError
12
- error = { 'http-code' => response.code, 'error' => "#{response.message}, #{response.body}" }
13
- end
14
-
15
- case
16
- when !error.is_a?(Hash)
17
- super "#{response.code} #{response.message}"
18
- when error.key?('http-code')
19
- super "#{error['http-code']}: #{error['error']}"
20
- when error.key?('errors')
21
- super error['errors'].join('. ')
22
- else
23
- super 'unknown error.'
24
- end
25
- end
26
- end
27
-
28
- USER_AGENT = 'BrickFTP Client/0.1 (https://github.com/koshigoe/brick_ftp)'.freeze
29
-
30
- def initialize(host = nil)
31
- @host = host || BrickFTP.config.api_host
32
- @conn = Net::HTTP.new(@host, 443)
33
- @conn.use_ssl = true
34
- @conn.open_timeout = BrickFTP.config.open_timeout
35
- @conn.read_timeout = BrickFTP.config.read_timeout
36
- end
37
-
38
- def get(path, params: {}, headers: {})
39
- query = params.map { |k, v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join('&')
40
- path = "#{path}?#{query}" unless query.empty?
41
-
42
- case res = request(:get, path, headers: headers)
43
- when Net::HTTPSuccess
44
- res.body.nil? || res.body.empty? ? {} : JSON.parse(res.body)
45
- else
46
- # TODO: redirect
47
- raise Error, res
48
- end
49
- end
50
-
51
- def post(path, params: {}, headers: {})
52
- case res = request(:post, path, params: params, headers: headers)
53
- when Net::HTTPSuccess, Net::HTTPCreated
54
- res.body.nil? || res.body.empty? ? {} : JSON.parse(res.body)
55
- else
56
- # TODO: redirect
57
- raise Error, res
58
- end
59
- end
60
-
61
- def put(path, params: {}, headers: {})
62
- case res = request(:put, path, params: params, headers: headers)
63
- when Net::HTTPSuccess
64
- res.body.nil? || res.body.empty? ? {} : JSON.parse(res.body)
65
- else
66
- # TODO: redirect
67
- raise Error, res
68
- end
69
- end
70
-
71
- def delete(path, params: {}, headers: {})
72
- case res = request(:delete, path, params: params, headers: headers)
73
- when Net::HTTPSuccess
74
- true
75
- else
76
- # TODO: redirect
77
- raise Error, res
78
- end
79
- end
80
-
81
- private
82
-
83
- # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
84
- def request(method, path, params: {}, headers: {})
85
- req = Net::HTTP.const_get(method.to_s.capitalize).new(path, headers)
86
- req['User-Agent'] = USER_AGENT
87
-
88
- if @host == BrickFTP.config.api_host
89
- req['Content-Type'] = 'application/json'
90
- case
91
- when BrickFTP.config.session
92
- req['Cookie'] = BrickFTP::API::Authentication.cookie(BrickFTP.config.session).to_s
93
- when BrickFTP.config.api_key
94
- req.basic_auth(BrickFTP.config.api_key, 'x')
95
- end
96
- end
97
-
98
- case
99
- when params.is_a?(Hash)
100
- req.body = params.to_json unless params.empty?
101
- when params.is_a?(IO) || params.is_a?(StringIO)
102
- req.body_stream = params
103
- req['Content-Length'] = params.size
104
- end
105
-
106
- start = Time.now
107
- begin
108
- logger.debug format('Request headers: %{headers}', headers: req.each_capitalized.map { |k, v| "#{k}: #{v}" })
109
- logger.debug format('Request body: %{body}', body: req.body)
110
- @conn.request(req).tap do |res|
111
- logger.debug format('Response headers: %{headers}', headers: res.each_capitalized.map { |k, v| "#{k}: #{v}" })
112
- logger.debug format('Response body: %{body}', body: res.body)
113
- end
114
- ensure
115
- message = format(
116
- 'Complete %{method} %{path} (%{time} ms)',
117
- method: method.upcase,
118
- path: path,
119
- time: (Time.now - start) * 1000
120
- )
121
- logger.info message
122
- end
123
- end
124
- # rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
125
-
126
- def logger
127
- BrickFTP.logger
128
- end
129
- end
130
- end
@@ -1,15 +0,0 @@
1
- module BrickFTP
2
- class LogFormatter < Logger::Formatter
3
- FORMAT = "severity:%{severity}\tpid:%{pid}\ttime:%{time}\tmessage:%{message}\n".freeze
4
-
5
- def call(severity, time, _program_name, message)
6
- params = {
7
- severity: severity,
8
- pid: Process.pid,
9
- time: format_datetime(time),
10
- message: msg2str(message),
11
- }
12
- FORMAT % params
13
- end
14
- end
15
- end
@@ -1,4 +0,0 @@
1
- module BrickFTP
2
- module Webhook
3
- end
4
- end
@@ -1,28 +0,0 @@
1
- require 'cgi'
2
-
3
- module BrickFTP
4
- module Webhook
5
- class Request
6
- attr_reader :action, :interface, :path, :destination, :at, :username, :type
7
-
8
- def self.from_query_string(query_string)
9
- params = query_string.split('&').each_with_object({}) do |pair, res|
10
- key, value = pair.split('=').map { |x| CGI.unescape(x) }
11
- res[key.to_sym] = value
12
- end
13
-
14
- new(params)
15
- end
16
-
17
- def initialize(action: nil, interface: nil, path: nil, destination: nil, at: nil, username: nil, type: nil)
18
- @action = action
19
- @interface = interface
20
- @path = path
21
- @destination = destination
22
- @at = at
23
- @username = username
24
- @type = type
25
- end
26
- end
27
- end
28
- end