brick_ftp 0.3.8 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a7c48f0dbe9dcae8e1a850c64f07214a6537ee18
4
- data.tar.gz: 296971fe21f4e09eec0991ed0ad623fa3dba7570
3
+ metadata.gz: 5b78f7e304c91c69572da2f15f66b53240beb5b5
4
+ data.tar.gz: dd5b06efbee8b4f23f91f33de2c36350cbb50d31
5
5
  SHA512:
6
- metadata.gz: d51a198f2a54dd57fa7deea8f6ede25e9f3583a879cb85120cff35e558e771f37971fa802243d22696120a0030bf655a7505c3bfae13ae50220dc329ed0c2950
7
- data.tar.gz: 4cd55009c9dc5b90c777f7f94391de5eddd554f335f73d43d937db37715f9115f3b8cba9c7e63b6ca5f110247ab12b5a403a69b0152c021255bb45fce84b6827
6
+ metadata.gz: 7f0b2464782ee7d0bb7660d3da573abfd302818492aa8f6675df312c70e9c0940c36524e0db692c1806686864ed12a565e8e9e0407a199d95bd0f5f779579f9f
7
+ data.tar.gz: d43aa3d504faf23f03b7d73f43e2f58593c68c04228cc4790fe96b6fadc311415d92cf16ba4fc6158fbaa441aa61304a642d78988d1110e689f349d5f1b17bae
@@ -0,0 +1,26 @@
1
+ Changelog
2
+ ====
3
+
4
+
5
+ [unreleased](https://github.com/koshigoe/brick_ftp/compare/v0.4.0...master)
6
+ ----
7
+
8
+ [Full Changelog](https://github.com/koshigoe/brick_ftp/compare/v0.4.0...master)
9
+
10
+ ### Enhancements:
11
+
12
+ ### Fixed Bugs:
13
+
14
+
15
+ [0.4.0](https://github.com/koshigoe/brick_ftp/compare/v0.3.8...v0.4.0)
16
+ ----
17
+
18
+ [Full Changelog](https://github.com/koshigoe/brick_ftp/compare/v0.3.8...v0.4.0)
19
+
20
+ ### Enhancements:
21
+
22
+ - [#36](https://github.com/koshigoe/brick_ftp/pull/36) Add Changelog.
23
+ - [#46](https://github.com/koshigoe/brick_ftp/pull/46) Add CLI.
24
+ - [#53](https://github.com/koshigoe/brick_ftp/pull/53) Allow to pass `action=stat` to Files: Download API.
25
+
26
+ ### Fixed Bugs:
data/README.md CHANGED
@@ -49,6 +49,26 @@ BrickFTP::API::Authentication.login('koshigoe', 'password')
49
49
 
50
50
  see [API document](http://www.rubydoc.info/github/koshigoe/brick_ftp/master) or source code.
51
51
 
52
+ ### CLI (experimental)
53
+
54
+ #### Configuration
55
+
56
+ ```
57
+ $ brick_ftp config set subdomain mydomain
58
+ $ brick_ftp config set api_key XXXXXXXXXX
59
+ ```
60
+
61
+ #### e.g. Get site usage (disk usage)
62
+
63
+ ```
64
+ $ brick_ftp site usage
65
+ ```
66
+
67
+ ### More
68
+
69
+ Please read help by run `brick_ftp help`, or read source code.
70
+
71
+
52
72
  ## Development
53
73
 
54
74
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -27,4 +27,6 @@ Gem::Specification.new do |spec|
27
27
  spec.add_development_dependency "pry", "~> 0.10"
28
28
 
29
29
  spec.add_dependency 'deep_hash_transform', '~> 1.0'
30
+ spec.add_dependency 'thor', '~> 0.19.1'
31
+ spec.add_dependency 'inifile', '~> 3.0.0'
30
32
  end
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ root = File.dirname(File.dirname(__FILE__))
4
+ $:.unshift(File.join(root, 'lib'))
5
+
6
+ require 'brick_ftp'
7
+
8
+ BrickFTP::CLI.start(ARGV)
@@ -1,4 +1,5 @@
1
1
  require 'deep_hash_transform'
2
+ require 'thor'
2
3
  require "brick_ftp/version"
3
4
  require 'brick_ftp/configuration'
4
5
  require 'brick_ftp/log_formatter'
@@ -35,6 +36,10 @@ require 'brick_ftp/api/file_operation/upload'
35
36
  require 'brick_ftp/api/site_usage'
36
37
  require 'brick_ftp/webhook'
37
38
  require 'brick_ftp/webhook/request'
39
+ require 'brick_ftp/cli'
40
+ require 'brick_ftp/cli/config'
41
+ require 'brick_ftp/cli/site'
42
+ require 'brick_ftp/cli/main'
38
43
 
39
44
  module BrickFTP
40
45
  # https://brickftp.com/redundancy/
@@ -18,10 +18,11 @@ module BrickFTP
18
18
  data.map { |x| new(x.symbolize_keys) }
19
19
  end
20
20
 
21
- def self.find(id)
21
+ def self.find(id, params: {})
22
22
  data = BrickFTP::HTTPClient.new.send(
23
23
  endpoints[:show][:http_method],
24
- api_path_for(:show, id)
24
+ api_path_for(:show, id),
25
+ params: params
25
26
  )
26
27
  data.empty? ? nil : new(data.symbolize_keys)
27
28
  end
@@ -66,6 +67,14 @@ module BrickFTP
66
67
  )
67
68
  true
68
69
  end
70
+
71
+ def as_json
72
+ self.class.attributes.each_with_object({}) { |name, res| res[name] = send(name) }
73
+ end
74
+
75
+ def to_json
76
+ as_json.to_json
77
+ end
69
78
  end
70
79
  end
71
80
  end
@@ -0,0 +1,7 @@
1
+ module BrickFTP
2
+ module CLI
3
+ def self.start(argv)
4
+ Main.start(argv)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,20 @@
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
@@ -0,0 +1,26 @@
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
@@ -0,0 +1,10 @@
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
@@ -315,10 +315,13 @@ module BrickFTP
315
315
 
316
316
  # provides a download URL that will enable you to download a file.
317
317
  # @see https://brickftp.com/ja/docs/rest-api/file-operations/
318
- # @param id file id.
318
+ # @param path [String] path for file.
319
+ # @param omit_download_uri [Boolean] If true, omit download_uri. (Add query `action=stat`)
319
320
  # @return [BrickFTP::API::File] file object.
320
- def show_file(id)
321
- BrickFTP::API::File.find(id)
321
+ def show_file(path, omit_download_uri: false)
322
+ params = {}
323
+ params[:action] = 'stat' if omit_download_uri
324
+ BrickFTP::API::File.find(path, params: params)
322
325
  end
323
326
 
324
327
  # Move or renames a file or folder to the destination provided in the move_destination parameter.
@@ -1,7 +1,14 @@
1
1
  require 'logger'
2
+ require 'inifile'
2
3
 
3
4
  module BrickFTP
4
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
5
12
  # Subdomain of API endpoint. If not specified, get value from enviroment variable `BRICK_FTP_SUBDOMAIN`.
6
13
  # @return [String] subdomain
7
14
  attr_accessor :subdomain
@@ -16,10 +23,10 @@ module BrickFTP
16
23
  attr_accessor :logger
17
24
  # Log level
18
25
  # @return [Integer]
19
- attr_reader :log_level
26
+ attr_accessor :log_level
20
27
  # Log formatter
21
28
  # @return [Logger::Formatter]
22
- attr_reader :log_formatter
29
+ attr_accessor :log_formatter
23
30
  # Open timeout
24
31
  # @return [Integer]
25
32
  attr_accessor :open_timeout
@@ -30,16 +37,34 @@ module BrickFTP
30
37
  DEFAULT_OPEN_TIMEOUT = 10
31
38
  DEFAULT_READ_TIMEOUT = 30
32
39
 
33
- def initialize
34
- self.subdomain = ENV['BRICK_FTP_SUBDOMAIN']
35
- self.api_key = ENV['BRICK_FTP_API_KEY']
40
+ DEFAULT_PROFILE = 'global'.freeze
41
+ CONFIG_FILE_PATH = File.expand_path('~/.brick_ftp/config').freeze
42
+ # Name of storable configurations. (TODO: log_path, log_level, log_formatter)
43
+ STORABLE_CONFIGURATION_KEYS = %w(subdomain api_key open_timeout read_timeout).freeze
44
+
45
+ STORABLE_CONFIGURATION_KEYS.each do |name|
46
+ define_method("#{name}=") do |value|
47
+ dirty_attributes.add(name)
48
+ instance_variable_set(:"@#{name}", value)
49
+ end
50
+ end
51
+
52
+ def initialize(profile: DEFAULT_PROFILE, config_file_path: CONFIG_FILE_PATH)
53
+ @profile = profile
54
+ @config_file_path = config_file_path
55
+ load_config_file(config_file_path)
56
+
57
+ self.subdomain = ENV.fetch('BRICK_FTP_SUBDOMAIN', inifile[profile]['subdomain'])
58
+ self.api_key = ENV.fetch('BRICK_FTP_API_KEY', inifile[profile]['api_key'])
36
59
  self.session = nil
37
- self.open_timeout = DEFAULT_OPEN_TIMEOUT
38
- self.read_timeout = DEFAULT_READ_TIMEOUT
60
+ self.open_timeout = (inifile[profile]['open_timeout'] || DEFAULT_OPEN_TIMEOUT).to_i
61
+ self.read_timeout = (inifile[profile]['read_timeout'] || DEFAULT_READ_TIMEOUT).to_i
39
62
  self.logger = Logger.new(STDOUT)
40
63
  self.log_level = Logger::WARN
41
64
  self.log_formatter = Logger::Formatter.new
42
65
  logger.level = log_level
66
+
67
+ dirty_attributes.clear
43
68
  end
44
69
 
45
70
  # Host name of API endpoint.
@@ -61,5 +86,29 @@ module BrickFTP
61
86
  @log_formatter = formatter
62
87
  logger.formatter = @log_formatter
63
88
  end
89
+
90
+ def save!
91
+ dirty_attributes.each do |key|
92
+ inifile[profile][key] = send(key)
93
+ end
94
+ FileUtils.mkdir_p(File.dirname(inifile.filename)) unless File.exist?(File.dirname(inifile.filename))
95
+ inifile.save
96
+ end
97
+
98
+ private
99
+
100
+ attr_reader :inifile, :dirty_attributes
101
+
102
+ def load_config_file(config_file_path)
103
+ @inifile = if File.exist?(config_file_path)
104
+ IniFile.load(config_file_path, encoding: 'UTF-8')
105
+ else
106
+ IniFile.new(filename: config_file_path, encoding: 'UTF-8')
107
+ end
108
+ end
109
+
110
+ def dirty_attributes
111
+ @dirty_attributes ||= Set.new
112
+ end
64
113
  end
65
114
  end
@@ -1,5 +1,6 @@
1
1
  require 'net/https'
2
2
  require 'json'
3
+ require 'cgi'
3
4
 
4
5
  module BrickFTP
5
6
  class HTTPClient
@@ -35,7 +36,10 @@ module BrickFTP
35
36
  end
36
37
 
37
38
  def get(path, params: {}, headers: {})
38
- case res = request(:get, path, params: params, headers: 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)
39
43
  when Net::HTTPSuccess
40
44
  res.body.nil? || res.body.empty? ? {} : JSON.parse(res.body)
41
45
  else
@@ -1,3 +1,3 @@
1
1
  module BrickFTP
2
- VERSION = "0.3.8"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brick_ftp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.8
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - koshigoe
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-21 00:00:00.000000000 Z
11
+ date: 2016-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -108,16 +108,46 @@ dependencies:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
110
  version: '1.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: thor
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.19.1
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.19.1
125
+ - !ruby/object:Gem::Dependency
126
+ name: inifile
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 3.0.0
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 3.0.0
111
139
  description: BrickFTP's REST API client.
112
140
  email:
113
141
  - koshigoeb@gmail.com
114
- executables: []
142
+ executables:
143
+ - brick_ftp
115
144
  extensions: []
116
145
  extra_rdoc_files: []
117
146
  files:
118
147
  - ".gitignore"
119
148
  - ".rspec"
120
149
  - ".travis.yml"
150
+ - CHANGELOG.md
121
151
  - Gemfile
122
152
  - LICENSE.txt
123
153
  - README.md
@@ -125,6 +155,7 @@ files:
125
155
  - bin/console
126
156
  - bin/setup
127
157
  - brick_ftp.gemspec
158
+ - exe/brick_ftp
128
159
  - lib/brick_ftp.rb
129
160
  - lib/brick_ftp/api.rb
130
161
  - lib/brick_ftp/api/authentication.rb
@@ -155,6 +186,10 @@ files:
155
186
  - lib/brick_ftp/api/user.rb
156
187
  - lib/brick_ftp/api_component.rb
157
188
  - lib/brick_ftp/api_definition.rb
189
+ - lib/brick_ftp/cli.rb
190
+ - lib/brick_ftp/cli/config.rb
191
+ - lib/brick_ftp/cli/main.rb
192
+ - lib/brick_ftp/cli/site.rb
158
193
  - lib/brick_ftp/client.rb
159
194
  - lib/brick_ftp/configuration.rb
160
195
  - lib/brick_ftp/http_client.rb