brick_ftp 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/lib/brick_ftp.rb +7 -4
  3. data/lib/brick_ftp/api.rb +5 -0
  4. data/lib/brick_ftp/api/authentication/session.rb +6 -4
  5. data/lib/brick_ftp/api/base.rb +28 -72
  6. data/lib/brick_ftp/api/behavior.rb +13 -7
  7. data/lib/brick_ftp/api/bundle.rb +11 -6
  8. data/lib/brick_ftp/api/bundle_content.rb +11 -13
  9. data/lib/brick_ftp/api/bundle_download.rb +11 -11
  10. data/lib/brick_ftp/api/file.rb +13 -3
  11. data/lib/brick_ftp/api/file_operation.rb +6 -0
  12. data/lib/brick_ftp/api/file_operation/copy.rb +10 -0
  13. data/lib/brick_ftp/api/file_operation/move.rb +10 -0
  14. data/lib/brick_ftp/api/file_operation/upload.rb +49 -0
  15. data/lib/brick_ftp/api/folder.rb +11 -6
  16. data/lib/brick_ftp/api/folder_behavior.rb +12 -0
  17. data/lib/brick_ftp/api/group.rb +10 -7
  18. data/lib/brick_ftp/api/history/file.rb +14 -13
  19. data/lib/brick_ftp/api/history/folder.rb +14 -13
  20. data/lib/brick_ftp/api/history/login.rb +14 -13
  21. data/lib/brick_ftp/api/history/site.rb +14 -13
  22. data/lib/brick_ftp/api/history/user.rb +14 -13
  23. data/lib/brick_ftp/api/notification.rb +8 -5
  24. data/lib/brick_ftp/api/permission.rb +11 -5
  25. data/lib/brick_ftp/api/public_key.rb +9 -5
  26. data/lib/brick_ftp/api/user.rb +30 -29
  27. data/lib/brick_ftp/api_component.rb +79 -0
  28. data/lib/brick_ftp/api_definition.rb +66 -0
  29. data/lib/brick_ftp/client.rb +5 -5
  30. data/lib/brick_ftp/http_client.rb +4 -4
  31. data/lib/brick_ftp/version.rb +1 -1
  32. metadata +9 -6
  33. data/lib/brick_ftp/api/behavior_folder.rb +0 -8
  34. data/lib/brick_ftp/api/file_copy.rb +0 -8
  35. data/lib/brick_ftp/api/file_move.rb +0 -8
  36. data/lib/brick_ftp/api/file_upload.rb +0 -43
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0b3bf69a33bce75c836d9f0a892647127e9a52d8
4
- data.tar.gz: ac40a241fa853a76d0b156de3d7a5946c00f037d
3
+ metadata.gz: 3865f1640bda9de3a80c620e958bf9de23901ca0
4
+ data.tar.gz: 12ebd6763744542fd4657910bb5e4cffd2635ada
5
5
  SHA512:
6
- metadata.gz: 39d59472f0eee719336531c6f79dad2605d426690452f01875036020c45f990e919674351b807f3794f817c023efa498c888aadac7ddd70f6357c214dcbfd209
7
- data.tar.gz: 3512bd4ce61bdcfe14cfca362bbd437f02be765b07aeae1db6e7fff0d7096c0077f90838780ccb13e31f37483739071f948172e566847f936bcd59a944edf73c
6
+ metadata.gz: 912c304822601600468a880b45d572f8ea744c912d589576448004a3bdee956fa83366892581a6f4116405d69ebef98f5eaf22e6c2f711cf0c42f07662587ff9
7
+ data.tar.gz: 4fdc3e3a5471577f2462b072242d15bb74a1bbde3b5dd45ee12e3bc9f8a1f3fbcc3b9927ad8dd1b0508486b953bedb46afd3db93a92484d08244e5bc56cb27d3
data/lib/brick_ftp.rb CHANGED
@@ -4,6 +4,8 @@ require 'brick_ftp/configuration'
4
4
  require 'brick_ftp/http_client'
5
5
  require 'brick_ftp/client'
6
6
  require 'brick_ftp/api'
7
+ require 'brick_ftp/api_component'
8
+ require 'brick_ftp/api_definition'
7
9
  require 'brick_ftp/api/base'
8
10
  require 'brick_ftp/api/authentication'
9
11
  require 'brick_ftp/api/authentication/session'
@@ -22,12 +24,13 @@ require 'brick_ftp/api/bundle'
22
24
  require 'brick_ftp/api/bundle_content'
23
25
  require 'brick_ftp/api/bundle_download'
24
26
  require 'brick_ftp/api/behavior'
25
- require 'brick_ftp/api/behavior_folder'
27
+ require 'brick_ftp/api/folder_behavior'
26
28
  require 'brick_ftp/api/folder'
27
29
  require 'brick_ftp/api/file'
28
- require 'brick_ftp/api/file_move'
29
- require 'brick_ftp/api/file_copy'
30
- require 'brick_ftp/api/file_upload'
30
+ require 'brick_ftp/api/file_operation'
31
+ require 'brick_ftp/api/file_operation/move'
32
+ require 'brick_ftp/api/file_operation/copy'
33
+ require 'brick_ftp/api/file_operation/upload'
31
34
 
32
35
  module BrickFTP
33
36
  # Return configuration.
data/lib/brick_ftp/api.rb CHANGED
@@ -1,4 +1,9 @@
1
1
  module BrickFTP
2
2
  module API
3
+ class Error < StandardError
4
+ end
5
+
6
+ class NoSuchAPI < Error
7
+ end
3
8
  end
4
9
  end
@@ -2,10 +2,12 @@ module BrickFTP
2
2
  module API
3
3
  module Authentication
4
4
  class Session < BrickFTP::API::Base
5
- define_api :create, '/api/rest/v1/sessions.json'
6
- define_api :delete, '/api/rest/v1/sessions.json'
7
- define_writable_attributes :username, :password
8
- define_readonly_attributes :id
5
+ endpoint :post, :create, '/api/rest/v1/sessions.json'
6
+ endpoint :delete, :delete, '/api/rest/v1/sessions.json'
7
+
8
+ attribute :id
9
+ attribute :username, writable: true
10
+ attribute :password, writable: true
9
11
 
10
12
  def self.create(params = {})
11
13
  super.tap { |x| BrickFTP.config.session = x }
@@ -3,107 +3,63 @@ require 'cgi'
3
3
  module BrickFTP
4
4
  module API
5
5
  class Base
6
- class NoSuchAPIError < StandardError; end
7
-
8
- class UndefinedAttributesError < StandardError
9
- def initialize(undefined_attributes = [])
10
- super "No such attributes: #{undefined_attributes.join(', ')}"
11
- end
12
- end
13
-
14
6
  def self.inherited(subclass)
15
- subclass.instance_eval do
16
- @api = {}
17
- @writable_attributes = []
18
- @readonly_attributes = []
19
- end
20
- end
21
-
22
- class << self
23
- attr_reader :api, :writable_attributes, :readonly_attributes
7
+ subclass.include APIDefinition
24
8
  end
25
9
 
26
- def self.attributes
27
- writable_attributes + readonly_attributes
28
- end
29
-
30
- def self.define_api(method, path)
31
- @api[method] = path
32
- end
33
-
34
- def self.define_writable_attributes(*attributes)
35
- @writable_attributes = attributes
36
- attr_reader *@writable_attributes.map { |x| x.to_s.tr('-', '_') }
37
- end
38
-
39
- def self.define_readonly_attributes(*attributes)
40
- @readonly_attributes = attributes
41
- attr_reader *@readonly_attributes.map { |x| x.to_s.tr('-', '_') }
42
- end
43
-
44
- def self.api_path_for(method, params = {})
45
- raise NoSuchAPIError, "#{method} #{self.name}" unless @api.key?(method)
46
- @api[method] % Hash[params.map { |k, v| [k, CGI.escape(v.to_s)] }]
47
- end
48
-
49
- QUERY_PARAMS = %i(page per_page start_at recursive).freeze
50
-
51
- def self.all(path_params = {})
52
- path_params.symbolize_keys!
53
- query_params = QUERY_PARAMS.each_with_object({}) do |name, res|
54
- res.update(name => path_params.delete(name)) if path_params.key?(name)
55
- end
56
-
57
- path = api_path_for(:index, path_params)
58
- unless query_params.empty?
59
- query = query_params.each_with_object([]) do |(k, v), res|
60
- res << "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"
61
- end.join('&')
62
- path = "#{path}?#{query}"
63
- end
10
+ def self.all(params = {})
11
+ params.symbolize_keys!
64
12
 
65
- BrickFTP::HTTPClient.new.get(path).map { |x| new(x.symbolize_keys) }
13
+ data = BrickFTP::HTTPClient.new.send(
14
+ endpoints[:index][:http_method],
15
+ api_path_for(:index, params),
16
+ params: api_component_for(:index).except_path_and_query(params)
17
+ )
18
+ data.map { |x| new(x.symbolize_keys) }
66
19
  end
67
20
 
68
21
  def self.find(id)
69
- params = {}
70
- api[:show].scan(/%\{([^}]+)\}/) { |m| params[m.first.to_sym] = id }
71
- data = BrickFTP::HTTPClient.new.get(api_path_for(:show, params))
22
+ data = BrickFTP::HTTPClient.new.send(
23
+ endpoints[:show][:http_method],
24
+ api_path_for(:show, id)
25
+ )
72
26
  data.empty? ? nil : new(data.symbolize_keys)
73
27
  end
74
28
 
75
- def self.create(params = {}, path_params = {})
29
+ def self.create(params = {})
76
30
  params.symbolize_keys!
77
- undefined_attributes = params.keys - writable_attributes
78
- raise UndefinedAttributesError, undefined_attributes unless undefined_attributes.empty?
79
31
 
80
- data = BrickFTP::HTTPClient.new.post(api_path_for(:create, path_params), params: params)
32
+ data = BrickFTP::HTTPClient.new.send(
33
+ endpoints[:create][:http_method],
34
+ api_path_for(:create, params),
35
+ params: api_component_for(:create).except_path_and_query(params)
36
+ )
81
37
  data = {} if data.is_a?(Array)
82
38
  new(data.symbolize_keys)
83
39
  end
84
40
 
85
41
  def initialize(params = {})
86
- undefined_attributes = params.keys - self.class.attributes
87
- raise UndefinedAttributesError, undefined_attributes unless undefined_attributes.empty?
88
-
89
42
  params.each { |k, v| instance_variable_set(:"@#{k}", v) }
90
43
  end
91
44
 
92
45
  def update(params = {})
93
46
  params.symbolize_keys!
94
- undefined_attributes = params.keys - self.class.writable_attributes
95
- raise UndefinedAttributesError, undefined_attributes unless undefined_attributes.empty?
96
47
 
97
- data = BrickFTP::HTTPClient.new.put(self.class.api_path_for(:update, id: id), params: params)
48
+ data = BrickFTP::HTTPClient.new.send(
49
+ self.class.endpoints[:update][:http_method],
50
+ self.class.api_path_for(:update, self),
51
+ params: self.class.api_component_for(:update).except_path_and_query(params)
52
+ )
98
53
  data.each { |k, v| instance_variable_set(:"@#{k}", v) }
99
54
 
100
55
  self
101
56
  end
102
57
 
103
58
  def destroy
104
- params = {}
105
- self.class.api[:delete].scan(/%\{([^}]+)\}/) { |m| params[m.first.to_sym] = send(m.first.to_sym) }
106
- BrickFTP::HTTPClient.new.delete(self.class.api_path_for(:delete, params))
59
+ BrickFTP::HTTPClient.new.send(
60
+ self.class.endpoints[:delete][:http_method],
61
+ self.class.api_path_for(:delete, self),
62
+ )
107
63
  true
108
64
  end
109
65
  end
@@ -1,13 +1,19 @@
1
1
  module BrickFTP
2
2
  module API
3
3
  class Behavior < Base
4
- define_api :index, '/api/rest/v1/behaviors.json'
5
- define_api :show, '/api/rest/v1/behaviors/%{id}.json'
6
- define_api :create, '/api/rest/v1/behaviors.json'
7
- define_api :update, '/api/rest/v1/behaviors/%{id}.json'
8
- define_api :delete, '/api/rest/v1/behaviors/%{id}.json'
9
- define_writable_attributes :path, :behavior, :value
10
- define_readonly_attributes :id, :path, :behavior, :value
4
+ endpoint :get, :index, '/api/rest/v1/behaviors.json'
5
+ endpoint :get, :show, '/api/rest/v1/behaviors/%{id}.json'
6
+ endpoint :post, :create, '/api/rest/v1/behaviors.json'
7
+ endpoint :put, :update, '/api/rest/v1/behaviors/%{id}.json'
8
+ endpoint :delete, :delete, '/api/rest/v1/behaviors/%{id}.json'
9
+
10
+ attribute :id
11
+ attribute :path
12
+ attribute :behavior
13
+ attribute :value
14
+ attribute :path, writable: true
15
+ attribute :behavior, writable: true
16
+ attribute :value, writable: true
11
17
  end
12
18
  end
13
19
  end
@@ -1,12 +1,17 @@
1
1
  module BrickFTP
2
2
  module API
3
3
  class Bundle < Base
4
- define_api :index, '/api/rest/v1/bundles.json'
5
- define_api :show, '/api/rest/v1/bundles/%{id}.json'
6
- define_api :create, '/api/rest/v1/bundles.json'
7
- define_api :delete, '/api/rest/v1/bundles/%{id}.json'
8
- define_writable_attributes :paths
9
- define_readonly_attributes :id, :code, :url, :user_id, :created_at
4
+ endpoint :get, :index, '/api/rest/v1/bundles.json'
5
+ endpoint :get, :show, '/api/rest/v1/bundles/%{id}.json'
6
+ endpoint :post, :create, '/api/rest/v1/bundles.json'
7
+ endpoint :delete, :delete, '/api/rest/v1/bundles/%{id}.json'
8
+
9
+ attribute :id
10
+ attribute :code
11
+ attribute :url
12
+ attribute :user_id
13
+ attribute :created_at
14
+ attribute :paths, writable: true
10
15
  end
11
16
  end
12
17
  end
@@ -1,20 +1,18 @@
1
1
  module BrickFTP
2
2
  module API
3
3
  class BundleContent < Base
4
- define_api :index, '/api/rest/v1/bundles/folders%{path}'
5
- define_writable_attributes :code, :host
6
- define_readonly_attributes :id, :path, :type, :size, :crc32, :md5
4
+ endpoint :post, :index, ->(params) {
5
+ params.key?(:path) ? '/api/rest/v1/bundles/folders/%{path}' : '/api/rest/v1/bundles/folders'
6
+ }
7
7
 
8
- def self.all(params, path_params = {})
9
- params.symbolize_keys!
10
- undefined_attributes = params.keys - writable_attributes
11
- raise UndefinedAttributesError, undefined_attributes unless undefined_attributes.empty?
12
-
13
- path_params[:path] = '' unless path_params.key?(:path)
14
-
15
- data = BrickFTP::HTTPClient.new.post(api_path_for(:index, path_params), params: params)
16
- data.map { |x| new(x.symbolize_keys) }
17
- end
8
+ attribute :id
9
+ attribute :path
10
+ attribute :type
11
+ attribute :size
12
+ attribute :crc32
13
+ attribute :md5
14
+ attribute :code, writable: true
15
+ attribute :host, writable: true
18
16
  end
19
17
  end
20
18
  end
@@ -1,18 +1,18 @@
1
1
  module BrickFTP
2
2
  module API
3
3
  class BundleDownload < Base
4
- define_api :index, '/api/rest/v1/bundles/files.json'
5
- define_writable_attributes :code, :host, :paths
6
- define_readonly_attributes :id, :path, :type, :size, :crc32, :md5, :download_uri
4
+ endpoint :post, :index, '/api/rest/v1/bundles/files.json'
7
5
 
8
- def self.all(params, path_params = {})
9
- params.symbolize_keys!
10
- undefined_attributes = params.keys - writable_attributes
11
- raise UndefinedAttributesError, undefined_attributes unless undefined_attributes.empty?
12
-
13
- data = BrickFTP::HTTPClient.new.post(api_path_for(:index, path_params), params: params)
14
- data.map { |x| new(x.symbolize_keys) }
15
- end
6
+ attribute :id
7
+ attribute :path
8
+ attribute :type
9
+ attribute :size
10
+ attribute :crc32
11
+ attribute :md5
12
+ attribute :download_uri
13
+ attribute :code, writable: true
14
+ attribute :host, writable: true
15
+ attribute :paths, writable: true
16
16
  end
17
17
  end
18
18
  end
@@ -1,9 +1,19 @@
1
1
  module BrickFTP
2
2
  module API
3
3
  class File < Base
4
- define_api :show, '/api/rest/v1/files/%{path}'
5
- define_api :delete, '/api/rest/v1/files/%{path}'
6
- define_readonly_attributes :id, :path, :type, :size, :mtime, :crc32, :md5, :download_uri, :provided_mtime, :permissions
4
+ endpoint :get, :show, '/api/rest/v1/files/%{path}'
5
+ endpoint :delete, :delete, '/api/rest/v1/files/%{path}'
6
+
7
+ attribute :id
8
+ attribute :path
9
+ attribute :type
10
+ attribute :size
11
+ attribute :mtime
12
+ attribute :crc32
13
+ attribute :md5
14
+ attribute :download_uri
15
+ attribute :provided_mtime
16
+ attribute :permissions
7
17
  end
8
18
  end
9
19
  end
@@ -0,0 +1,6 @@
1
+ module BrickFTP
2
+ module API
3
+ module FileOperation
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,10 @@
1
+ module BrickFTP
2
+ module API
3
+ module FileOperation
4
+ class Copy < BrickFTP::API::Base
5
+ endpoint :post, :create, '/api/rest/v1/files/%{path}'
6
+ attribute :'copy-destination', writable: true
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module BrickFTP
2
+ module API
3
+ module FileOperation
4
+ class Move < BrickFTP::API::Base
5
+ endpoint :post, :create, '/api/rest/v1/files/%{path}'
6
+ attribute :'move-destination', writable: true
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,49 @@
1
+ module BrickFTP
2
+ module API
3
+ module FileOperation
4
+ class Upload < BrickFTP::API::Base
5
+ endpoint :post, :create, '/api/rest/v1/files/%{path}'
6
+
7
+ attribute :id
8
+ attribute :ref
9
+ attribute :http_method
10
+ attribute :upload_uri
11
+ attribute :partsize
12
+ attribute :part_number
13
+ attribute :available_parts
14
+ attribute :headers
15
+ attribute :parameters
16
+ attribute :send
17
+ attribute :path
18
+ attribute :action
19
+ attribute :ask_about_overwrites
20
+ attribute :type
21
+ attribute :size
22
+ attribute :mtime
23
+ attribute :crc32
24
+ attribute :md5
25
+ attribute :expires
26
+ attribute :next_partsize
27
+ attribute :provided_mtime
28
+ attribute :permission
29
+ attribute :action, writable: true
30
+ attribute :ref, writable: true
31
+ attribute :part, writable: true
32
+ attribute :restart, writable: true
33
+
34
+ def self.create(path:, source:)
35
+ api_client = BrickFTP::HTTPClient.new
36
+ step1 = api_client.post(api_path_for(:create, path: path), params: { action: 'put' })
37
+
38
+ upload_uri = URI.parse(step1['upload_uri'])
39
+ upload_client = BrickFTP::HTTPClient.new(upload_uri.host)
40
+ upload_client.put(step1['upload_uri'], params: source)
41
+
42
+ step3 = api_client.post(api_path_for(:create, path: path), params: { action: 'end', ref: step1['ref'] })
43
+
44
+ new(step1.merge(step3).symbolize_keys)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -1,13 +1,18 @@
1
1
  module BrickFTP
2
2
  module API
3
3
  class Folder < Base
4
- define_api :index, '/api/rest/v1/folders/%{path}'
5
- define_api :create, '/api/rest/v1/folders/%{path}'
6
- define_readonly_attributes :id, :path, :type, :size, :mtime, :crc32, :md5, :provided_mtime, :permissions
4
+ endpoint :get, :index, '/api/rest/v1/folders/%{path}', :page, :per_page, :search, :'sort_by[path]', :'sort_by[size]', :'sort_by[modified_at_datetime]'
5
+ endpoint :post, :create, '/api/rest/v1/folders/%{path}'
7
6
 
8
- def self.create(path_params = {})
9
- super({}, path_params)
10
- end
7
+ attribute :id
8
+ attribute :path
9
+ attribute :type
10
+ attribute :size
11
+ attribute :mtime
12
+ attribute :crc32
13
+ attribute :md5
14
+ attribute :provided_mtime
15
+ attribute :permissions
11
16
  end
12
17
  end
13
18
  end
@@ -0,0 +1,12 @@
1
+ module BrickFTP
2
+ module API
3
+ class FolderBehavior < Base
4
+ endpoint :get, :index, '/api/rest/v1/behaviors/folders/%{path}', :recursive
5
+
6
+ attribute :id
7
+ attribute :path
8
+ attribute :behavior
9
+ attribute :value
10
+ end
11
+ end
12
+ end
@@ -1,13 +1,16 @@
1
1
  module BrickFTP
2
2
  module API
3
3
  class Group < Base
4
- define_api :index, '/api/rest/v1/groups.json'
5
- define_api :show, '/api/rest/v1/groups/%{id}.json'
6
- define_api :create, '/api/rest/v1/groups.json'
7
- define_api :update, '/api/rest/v1/groups/%{id}.json'
8
- define_api :delete, '/api/rest/v1/groups/%{id}.json'
9
- define_writable_attributes :name, :notes, :user_ids
10
- define_readonly_attributes :id
4
+ endpoint :get, :index, '/api/rest/v1/groups.json'
5
+ endpoint :get, :show, '/api/rest/v1/groups/%{id}.json'
6
+ endpoint :post, :create, '/api/rest/v1/groups.json'
7
+ endpoint :put, :update, '/api/rest/v1/groups/%{id}.json'
8
+ endpoint :delete, :delete, '/api/rest/v1/groups/%{id}.json'
9
+
10
+ attribute :id
11
+ attribute :name, writable: true
12
+ attribute :notes, writable: true
13
+ attribute :user_ids, writable: true
11
14
  end
12
15
  end
13
16
  end
@@ -2,19 +2,20 @@ module BrickFTP
2
2
  module API
3
3
  module History
4
4
  class File < BrickFTP::API::Base
5
- define_api :index, '/api/rest/v1/history/files/%{path}'
6
- define_readonly_attributes :id,
7
- :when,
8
- :user_id,
9
- :username,
10
- :action,
11
- :failure_type,
12
- :path,
13
- :source,
14
- :destination,
15
- :targets,
16
- :ip,
17
- :interface
5
+ endpoint :get, :index, '/api/rest/v1/history/files/%{path}', :page, :per_page, :start_at
6
+
7
+ attribute :id
8
+ attribute :when
9
+ attribute :user_id
10
+ attribute :username
11
+ attribute :action
12
+ attribute :failure_type
13
+ attribute :path
14
+ attribute :source
15
+ attribute :destination
16
+ attribute :targets
17
+ attribute :ip
18
+ attribute :interface
18
19
  end
19
20
  end
20
21
  end
@@ -2,19 +2,20 @@ module BrickFTP
2
2
  module API
3
3
  module History
4
4
  class Folder < BrickFTP::API::Base
5
- define_api :index, '/api/rest/v1/history/folders/%{path}'
6
- define_readonly_attributes :id,
7
- :when,
8
- :user_id,
9
- :username,
10
- :action,
11
- :failure_type,
12
- :path,
13
- :source,
14
- :destination,
15
- :targets,
16
- :ip,
17
- :interface
5
+ endpoint :get, :index, '/api/rest/v1/history/folders/%{path}', :page, :per_page, :start_at
6
+
7
+ attribute :id
8
+ attribute :when
9
+ attribute :user_id
10
+ attribute :username
11
+ attribute :action
12
+ attribute :failure_type
13
+ attribute :path
14
+ attribute :source
15
+ attribute :destination
16
+ attribute :targets
17
+ attribute :ip
18
+ attribute :interface
18
19
  end
19
20
  end
20
21
  end
@@ -2,19 +2,20 @@ module BrickFTP
2
2
  module API
3
3
  module History
4
4
  class Login < BrickFTP::API::Base
5
- define_api :index, '/api/rest/v1/history/login.json'
6
- define_readonly_attributes :id,
7
- :when,
8
- :user_id,
9
- :username,
10
- :action,
11
- :failure_type,
12
- :path,
13
- :source,
14
- :destination,
15
- :targets,
16
- :ip,
17
- :interface
5
+ endpoint :get, :index, '/api/rest/v1/history/login.json', :page, :per_page, :start_at
6
+
7
+ attribute :id
8
+ attribute :when
9
+ attribute :user_id
10
+ attribute :username
11
+ attribute :action
12
+ attribute :failure_type
13
+ attribute :path
14
+ attribute :source
15
+ attribute :destination
16
+ attribute :targets
17
+ attribute :ip
18
+ attribute :interface
18
19
  end
19
20
  end
20
21
  end
@@ -2,19 +2,20 @@ module BrickFTP
2
2
  module API
3
3
  module History
4
4
  class Site < BrickFTP::API::Base
5
- define_api :index, '/api/rest/v1/history.json'
6
- define_readonly_attributes :id,
7
- :when,
8
- :user_id,
9
- :username,
10
- :action,
11
- :failure_type,
12
- :path,
13
- :source,
14
- :destination,
15
- :targets,
16
- :ip,
17
- :interface
5
+ endpoint :get, :index, '/api/rest/v1/history.json', :page, :per_page, :start_at
6
+
7
+ attribute :id
8
+ attribute :when
9
+ attribute :user_id
10
+ attribute :username
11
+ attribute :action
12
+ attribute :failure_type
13
+ attribute :path
14
+ attribute :source
15
+ attribute :destination
16
+ attribute :targets
17
+ attribute :ip
18
+ attribute :interface
18
19
  end
19
20
  end
20
21
  end
@@ -2,19 +2,20 @@ module BrickFTP
2
2
  module API
3
3
  module History
4
4
  class User < BrickFTP::API::Base
5
- define_api :index, '/api/rest/v1/history/users/%{user_id}.json'
6
- define_readonly_attributes :id,
7
- :when,
8
- :user_id,
9
- :username,
10
- :action,
11
- :failure_type,
12
- :path,
13
- :source,
14
- :destination,
15
- :targets,
16
- :ip,
17
- :interface
5
+ endpoint :get, :index, '/api/rest/v1/history/users/%{user_id}.json', :page, :per_page, :start_at
6
+
7
+ attribute :id
8
+ attribute :when
9
+ attribute :user_id
10
+ attribute :username
11
+ attribute :action
12
+ attribute :failure_type
13
+ attribute :path
14
+ attribute :source
15
+ attribute :destination
16
+ attribute :targets
17
+ attribute :ip
18
+ attribute :interface
18
19
  end
19
20
  end
20
21
  end
@@ -1,11 +1,14 @@
1
1
  module BrickFTP
2
2
  module API
3
3
  class Notification < Base
4
- define_api :index, '/api/rest/v1/notifications.json'
5
- define_api :create, '/api/rest/v1/notifications.json'
6
- define_api :delete, '/api/rest/v1/notifications/%{id}.json'
7
- define_writable_attributes :path, :user_id, :username
8
- define_readonly_attributes :id
4
+ endpoint :get, :index, '/api/rest/v1/notifications.json'
5
+ endpoint :post, :create, '/api/rest/v1/notifications.json'
6
+ endpoint :delete, :delete, '/api/rest/v1/notifications/%{id}.json'
7
+
8
+ attribute :id
9
+ attribute :path, writable: true
10
+ attribute :user_id, writable: true
11
+ attribute :username, writable: true
9
12
  end
10
13
  end
11
14
  end
@@ -1,11 +1,17 @@
1
1
  module BrickFTP
2
2
  module API
3
3
  class Permission < Base
4
- define_api :index, '/api/rest/v1/permissions.json'
5
- define_api :create, '/api/rest/v1/permissions.json'
6
- define_api :delete, '/api/rest/v1/permissions/%{id}.json'
7
- define_writable_attributes :path, :permission, :user_id, :username, :group_id
8
- define_readonly_attributes :id, :recursive
4
+ endpoint :get, :index, '/api/rest/v1/permissions.json'
5
+ endpoint :post, :create, '/api/rest/v1/permissions.json'
6
+ endpoint :delete, :delete, '/api/rest/v1/permissions/%{id}.json'
7
+
8
+ attribute :id
9
+ attribute :recursive
10
+ attribute :path, writable: true
11
+ attribute :permission, writable: true
12
+ attribute :user_id, writable: true
13
+ attribute :username, writable: true
14
+ attribute :group_id, writable: true
9
15
  end
10
16
  end
11
17
  end
@@ -1,11 +1,15 @@
1
1
  module BrickFTP
2
2
  module API
3
3
  class PublicKey < Base
4
- define_api :index, '/api/rest/v1/users/%{user_id}/public_keys.json'
5
- define_api :create, '/api/rest/v1/users/%{user_id}/public_keys.json'
6
- define_api :delete, '/api/rest/v1/public_keys/%{id}.json'
7
- define_writable_attributes :title, :public_key
8
- define_readonly_attributes :id, :fingerprint, :created_at
4
+ endpoint :get, :index, '/api/rest/v1/users/%{user_id}/public_keys.json'
5
+ endpoint :post, :create, '/api/rest/v1/users/%{user_id}/public_keys.json'
6
+ endpoint :delete, :delete, '/api/rest/v1/public_keys/%{id}.json'
7
+
8
+ attribute :id
9
+ attribute :fingerprint
10
+ attribute :created_at
11
+ attribute :title, writable: true
12
+ attribute :public_key, writable: true
9
13
  end
10
14
  end
11
15
  end
@@ -1,35 +1,36 @@
1
1
  module BrickFTP
2
2
  module API
3
3
  class User < Base
4
- define_api :index, '/api/rest/v1/users.json'
5
- define_api :show, '/api/rest/v1/users/%{id}.json'
6
- define_api :create, '/api/rest/v1/users.json'
7
- define_api :update, '/api/rest/v1/users/%{id}.json'
8
- define_api :delete, '/api/rest/v1/users/%{id}.json'
9
- define_writable_attributes :username,
10
- :password,
11
- :name,
12
- :email,
13
- :notes,
14
- :group_ids,
15
- :ftp_permission,
16
- :web_permission,
17
- :sftp_permission,
18
- :dav_permission,
19
- :restapi_permission,
20
- :attachments_permission,
21
- :self_managed,
22
- :require_password_change,
23
- :allowed_ips,
24
- :user_root,
25
- :grant_permission,
26
- :ssl_required,
27
- :authentication_method
28
- define_readonly_attributes :id,
29
- :last_login_at,
30
- :time_zone,
31
- :language,
32
- :site_admin
4
+ endpoint :get, :index, '/api/rest/v1/users.json'
5
+ endpoint :get, :show, '/api/rest/v1/users/%{id}.json'
6
+ endpoint :post, :create, '/api/rest/v1/users.json'
7
+ endpoint :put, :update, '/api/rest/v1/users/%{id}.json'
8
+ endpoint :delete, :delete, '/api/rest/v1/users/%{id}.json'
9
+
10
+ attribute :id
11
+ attribute :last_login_at
12
+ attribute :time_zone
13
+ attribute :language
14
+ attribute :site_admin
15
+ attribute :username, writable: true
16
+ attribute :password, writable: true
17
+ attribute :name, writable: true
18
+ attribute :email, writable: true
19
+ attribute :notes, writable: true
20
+ attribute :group_ids, writable: true
21
+ attribute :ftp_permission, writable: true
22
+ attribute :web_permission, writable: true
23
+ attribute :sftp_permission, writable: true
24
+ attribute :dav_permission, writable: true
25
+ attribute :restapi_permission, writable: true
26
+ attribute :attachments_permission, writable: true
27
+ attribute :self_managed, writable: true
28
+ attribute :require_password_change, writable: true
29
+ attribute :allowed_ips, writable: true
30
+ attribute :user_root, writable: true
31
+ attribute :grant_permission, writable: true
32
+ attribute :ssl_required, writable: true
33
+ attribute :authentication_method, writable: true
33
34
  end
34
35
  end
35
36
  end
@@ -0,0 +1,79 @@
1
+ module BrickFTP
2
+ class APIComponent
3
+ attr_reader :path_template, :query_keys
4
+
5
+ def initialize(path_template, query_keys = [])
6
+ @path_template = path_template.respond_to?(:call) ? path_template : ->(_) { path_template }
7
+ @query_keys = query_keys.map(&:to_sym)
8
+ end
9
+
10
+ def path(params)
11
+ params = normalize_params(params)
12
+
13
+ path_params = build_path_params_from(params)
14
+ escaped_path_params = path_params.each_with_object({}) do |(k, v), res|
15
+ res[k] = CGI.escape(v.to_s)
16
+ end
17
+
18
+ path = path_template.call(params) % escaped_path_params
19
+ query = build_query_string_from(params)
20
+ query.empty? ? path : "#{path}?#{query}"
21
+ end
22
+
23
+ def build_path_params_from(params = {})
24
+ params = normalize_params(params)
25
+
26
+ path_variables(params).each_with_object({}) do |key, res|
27
+ res[key] = params[key]
28
+ end
29
+ end
30
+
31
+ def build_query_params_from(params = {})
32
+ params = normalize_params(params)
33
+
34
+ query_keys.each_with_object({}) do |name, res|
35
+ next unless params.key?(name)
36
+ res[name] = params[name]
37
+ end
38
+ end
39
+
40
+ def build_query_string_from(params = {})
41
+ params = normalize_params(params)
42
+
43
+ pairs = build_query_params_from(params).each_with_object([]) do |(k, v), res|
44
+ res << "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"
45
+ end
46
+ pairs.join('&')
47
+ end
48
+
49
+ def except_path_and_query(params = {})
50
+ params = normalize_params(params)
51
+
52
+ exceptions = path_variables(params) + query_keys
53
+ params.reject { |k, _| exceptions.include?(k) }
54
+ end
55
+
56
+ private
57
+
58
+ def path_variables(params = {})
59
+ @path_variables ||= path_template.call(params).scan(/%\{([^}]+)\}/).map { |m| m.first.to_sym }
60
+ end
61
+
62
+ def normalize_params(params = {})
63
+ case params
64
+ when Hash
65
+ params.symbolize_keys
66
+ when BrickFTP::API::Base
67
+ keys = path_variables(params) + query_keys
68
+ keys.each_with_object({}) do |key, res|
69
+ res[key] = params.send(key)
70
+ end
71
+ else
72
+ keys = path_variables(params) + query_keys
73
+ keys.each_with_object({}) do |key, res|
74
+ res[key] = params
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,66 @@
1
+ module BrickFTP
2
+ module APIDefinition
3
+ def self.included(klass)
4
+ klass.class_eval do
5
+ @endpoints = {}
6
+ @writable_attributes = []
7
+ @readonly_attributes = []
8
+
9
+ class << self
10
+ attr_reader :endpoints, :writable_attributes, :readonly_attributes
11
+ end
12
+ end
13
+
14
+ klass.extend ClassMethods
15
+ end
16
+
17
+ module ClassMethods
18
+ # Define API endpoint.
19
+ # @param http_method [:Symbol] any one of :get, :post, :put, :delete
20
+ # @param name [Symbol] any one of :index, :show, :create, :update, :destroy
21
+ # @param path_template [Stryng] template of endpoint path.
22
+ # @param query_keys [Array] array of query_string's parameter name.
23
+ def endpoint(http_method, name, path_template, *query_keys)
24
+ endpoints[name] = {
25
+ http_method: http_method,
26
+ path_template: path_template,
27
+ query_keys: query_keys,
28
+ }
29
+ end
30
+
31
+ # Define attribute.
32
+ # @param name [Symbol] name of attribute.
33
+ # @param writable [Boolean]
34
+ def attribute(name, writable: false)
35
+ if writable
36
+ writable_attributes << name.to_sym
37
+ else
38
+ readonly_attributes << name.to_sym
39
+ end
40
+ attr_reader name.to_s.tr('-', '_')
41
+ end
42
+
43
+ # Return all attributes.
44
+ # @return [Array]
45
+ def attributes
46
+ writable_attributes + readonly_attributes
47
+ end
48
+
49
+ # Build path for API endpoint.
50
+ # @param name [Symbol]
51
+ # @param params [Hash] mixed path parameters and query parameters.
52
+ # @return [String]
53
+ def api_path_for(name, params = {})
54
+ api_component_for(name).path(params)
55
+ end
56
+
57
+ # Build BrickFTP::APIComponent.
58
+ # @param name [Symbol]
59
+ # @return [BrickFTP::APIComponent]
60
+ def api_component_for(name)
61
+ raise BrickFTP::API::NoSuchAPI, "#{name} #{self.name}" unless endpoints.key?(name)
62
+ BrickFTP::APIComponent.new(endpoints[name][:path_template], endpoints[name][:query_keys])
63
+ end
64
+ end
65
+ end
66
+ end
@@ -281,9 +281,9 @@ module BrickFTP
281
281
 
282
282
  # shows the behaviors that apply to the given path.
283
283
  # @see https://brickftp.com/ja/docs/rest-api/behaviors/
284
- # @return [Array] array of BrickFTP::API::BehaviorFolder
284
+ # @return [Array] array of BrickFTP::API::FolderBehavior
285
285
  def list_folder_behaviors(path:)
286
- BrickFTP::API::BehaviorFolder.all(path: path)
286
+ BrickFTP::API::FolderBehavior.all(path: path)
287
287
  end
288
288
 
289
289
  # Lists the contents of the folder provided in the URL.
@@ -326,7 +326,7 @@ module BrickFTP
326
326
  # @param move_destination [String]
327
327
  # @return [BrickFTP::API::FileMove]
328
328
  def move_file(path:, move_destination:)
329
- BrickFTP::API::FileMove.create(path: path, move_destination: move_destination)
329
+ BrickFTP::API::FileOperation::Move.create(path: path, move_destination: move_destination)
330
330
  end
331
331
 
332
332
  # Copy a file or folder to the destination provided in the copy_destination parameter.
@@ -335,7 +335,7 @@ module BrickFTP
335
335
  # @param copy_destination [String]
336
336
  # @return [BrickFTP::API::FileCopy]
337
337
  def copy_file(path:, copy_destination:)
338
- BrickFTP::API::FileCopy.create(path: path, copy_destination: copy_destination)
338
+ BrickFTP::API::FileOperation::Copy.create(path: path, copy_destination: copy_destination)
339
339
  end
340
340
 
341
341
  # Delete a file.
@@ -352,7 +352,7 @@ module BrickFTP
352
352
  # @param source [String] path for source file.
353
353
  # @return [BrickFTP::API::FileUpload]
354
354
  def upload_file(path:, source:)
355
- BrickFTP::API::FileUpload.create(path: path, source: source)
355
+ BrickFTP::API::FileOperation::Upload.create(path: path, source: source)
356
356
  end
357
357
  end
358
358
  end
@@ -30,8 +30,8 @@ module BrickFTP
30
30
  @conn.use_ssl = true
31
31
  end
32
32
 
33
- def get(path)
34
- case res = request(:get, path)
33
+ def get(path, params: {})
34
+ case res = request(:get, path, params: params)
35
35
  when Net::HTTPSuccess
36
36
  res.body.nil? || res.body.empty? ? {} : JSON.parse(res.body)
37
37
  else
@@ -60,8 +60,8 @@ module BrickFTP
60
60
  end
61
61
  end
62
62
 
63
- def delete(path)
64
- case res = request(:delete, path)
63
+ def delete(path, params: {})
64
+ case res = request(:delete, path, params: params)
65
65
  when Net::HTTPSuccess
66
66
  true
67
67
  else
@@ -1,3 +1,3 @@
1
1
  module BrickFTP
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.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.2.0
4
+ version: 0.3.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-03 00:00:00.000000000 Z
11
+ date: 2016-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -117,15 +117,16 @@ files:
117
117
  - lib/brick_ftp/api/authentication/session.rb
118
118
  - lib/brick_ftp/api/base.rb
119
119
  - lib/brick_ftp/api/behavior.rb
120
- - lib/brick_ftp/api/behavior_folder.rb
121
120
  - lib/brick_ftp/api/bundle.rb
122
121
  - lib/brick_ftp/api/bundle_content.rb
123
122
  - lib/brick_ftp/api/bundle_download.rb
124
123
  - lib/brick_ftp/api/file.rb
125
- - lib/brick_ftp/api/file_copy.rb
126
- - lib/brick_ftp/api/file_move.rb
127
- - lib/brick_ftp/api/file_upload.rb
124
+ - lib/brick_ftp/api/file_operation.rb
125
+ - lib/brick_ftp/api/file_operation/copy.rb
126
+ - lib/brick_ftp/api/file_operation/move.rb
127
+ - lib/brick_ftp/api/file_operation/upload.rb
128
128
  - lib/brick_ftp/api/folder.rb
129
+ - lib/brick_ftp/api/folder_behavior.rb
129
130
  - lib/brick_ftp/api/group.rb
130
131
  - lib/brick_ftp/api/history.rb
131
132
  - lib/brick_ftp/api/history/file.rb
@@ -137,6 +138,8 @@ files:
137
138
  - lib/brick_ftp/api/permission.rb
138
139
  - lib/brick_ftp/api/public_key.rb
139
140
  - lib/brick_ftp/api/user.rb
141
+ - lib/brick_ftp/api_component.rb
142
+ - lib/brick_ftp/api_definition.rb
140
143
  - lib/brick_ftp/client.rb
141
144
  - lib/brick_ftp/configuration.rb
142
145
  - lib/brick_ftp/http_client.rb
@@ -1,8 +0,0 @@
1
- module BrickFTP
2
- module API
3
- class BehaviorFolder < Base
4
- define_api :index, '/api/rest/v1/behaviors/folders/%{path}'
5
- define_readonly_attributes :id, :path, :behavior, :value
6
- end
7
- end
8
- end
@@ -1,8 +0,0 @@
1
- module BrickFTP
2
- module API
3
- class FileCopy < Base
4
- define_api :create, '/api/rest/v1/files/%{path}'
5
- define_writable_attributes :'copy-destination'
6
- end
7
- end
8
- end
@@ -1,8 +0,0 @@
1
- module BrickFTP
2
- module API
3
- class FileMove < Base
4
- define_api :create, '/api/rest/v1/files/%{path}'
5
- define_writable_attributes :'move-destination'
6
- end
7
- end
8
- end
@@ -1,43 +0,0 @@
1
- module BrickFTP
2
- module API
3
- class FileUpload < Base
4
- define_api :create, '/api/rest/v1/files/%{path}'
5
- define_writable_attributes :action, :ref, :part, :restart
6
- define_readonly_attributes :id,
7
- :ref,
8
- :http_method,
9
- :upload_uri,
10
- :partsize,
11
- :part_number,
12
- :available_parts,
13
- :headers,
14
- :parameters,
15
- :send,
16
- :path,
17
- :action,
18
- :ask_about_overwrites,
19
- :type,
20
- :size,
21
- :mtime,
22
- :crc32,
23
- :md5,
24
- :expires,
25
- :next_partsize,
26
- :provided_mtime,
27
- :permissions
28
-
29
- def self.create(path: , source:)
30
- api_client = BrickFTP::HTTPClient.new
31
- step1 = api_client.post(api_path_for(:create, path: path), params: { action: 'put' })
32
-
33
- upload_uri = URI.parse(step1['upload_uri'])
34
- upload_client = BrickFTP::HTTPClient.new(upload_uri.host)
35
- upload_client.put(step1['upload_uri'], params: source)
36
-
37
- step3 = api_client.post(api_path_for(:create, path: path), params: { action: 'end', ref: step1['ref'] })
38
-
39
- new(step1.merge(step3).symbolize_keys)
40
- end
41
- end
42
- end
43
- end