brick_ftp 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +5 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +61 -0
  8. data/Rakefile +6 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +8 -0
  11. data/brick_ftp.gemspec +27 -0
  12. data/lib/brick_ftp.rb +44 -0
  13. data/lib/brick_ftp/api.rb +4 -0
  14. data/lib/brick_ftp/api/authentication.rb +21 -0
  15. data/lib/brick_ftp/api/authentication/session.rb +23 -0
  16. data/lib/brick_ftp/api/base.rb +111 -0
  17. data/lib/brick_ftp/api/behavior.rb +13 -0
  18. data/lib/brick_ftp/api/behavior_folder.rb +8 -0
  19. data/lib/brick_ftp/api/bundle.rb +12 -0
  20. data/lib/brick_ftp/api/bundle_content.rb +20 -0
  21. data/lib/brick_ftp/api/bundle_download.rb +18 -0
  22. data/lib/brick_ftp/api/file.rb +9 -0
  23. data/lib/brick_ftp/api/file_copy.rb +8 -0
  24. data/lib/brick_ftp/api/file_move.rb +8 -0
  25. data/lib/brick_ftp/api/file_upload.rb +43 -0
  26. data/lib/brick_ftp/api/folder.rb +13 -0
  27. data/lib/brick_ftp/api/group.rb +13 -0
  28. data/lib/brick_ftp/api/history.rb +6 -0
  29. data/lib/brick_ftp/api/history/file.rb +21 -0
  30. data/lib/brick_ftp/api/history/folder.rb +21 -0
  31. data/lib/brick_ftp/api/history/login.rb +21 -0
  32. data/lib/brick_ftp/api/history/site.rb +21 -0
  33. data/lib/brick_ftp/api/history/user.rb +21 -0
  34. data/lib/brick_ftp/api/notification.rb +11 -0
  35. data/lib/brick_ftp/api/permission.rb +11 -0
  36. data/lib/brick_ftp/api/public_key.rb +11 -0
  37. data/lib/brick_ftp/api/user.rb +35 -0
  38. data/lib/brick_ftp/client.rb +9 -0
  39. data/lib/brick_ftp/configuration.rb +23 -0
  40. data/lib/brick_ftp/http_client.rb +100 -0
  41. data/lib/brick_ftp/version.rb +3 -0
  42. metadata +153 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 763c13ebed0e36da20c18284f3a679314c991d6b
4
+ data.tar.gz: d04fbfdbfd3c579010d5880d0e755a0b65f156e7
5
+ SHA512:
6
+ metadata.gz: 35aabedc78ef3f7c9af8574551705321be517eedd940b0b52a1dd3f59aedd9fca38893303d344167e5559f8bed684e31c7e09794d7a215368741dd19cc68bfe9
7
+ data.tar.gz: de4d25dd868eb8b1f8f90238ee30fc2d66062553841f931f141d4ea0aecc767f4adb73746b5409f3dbdcba55386e4be150789761634607db86742e194b7a2009
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in brick_ftp.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016 Masataka Suzuki
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ [![CircleCI](https://circleci.com/gh/koshigoe/brick_ftp/tree/master.svg?style=svg)](https://circleci.com/gh/koshigoe/brick_ftp/tree/master)
2
+
3
+ # BrickFTP
4
+
5
+ This is a [BrickFTP](https://brickftp.com/)'s _unofficial_ [REST API](https://brickftp.com/ja/docs/rest-api/) Client.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'brick_ftp'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install brick_ftp
22
+
23
+ ## Usage
24
+
25
+ ### Configuration
26
+
27
+ ```ruby
28
+ BrickFTP.configure do |c|
29
+ c.subdomain = 'koshigoe' # Used by build API URL(e.g. https://{subdomain}.brickftp.com/api/rest/v1/...)
30
+ c.api_key = 'xxxxx' # Used by set REST API key.
31
+ c.logger = Logger.new(STDOUT) # Used by logging.
32
+ end
33
+ ```
34
+
35
+ - Environment value `BRICK_FTP_SUBDOMAIN` is set to `subdomain`.
36
+ - Environment value `BRICK_FTP_API_KEY` is set to `api_key`.
37
+
38
+ ### Authentication
39
+
40
+ If you authenticate by API key, you set API key to configuration.
41
+ If you authenticate by session cooki, you must authenticate by API.
42
+
43
+ ```ruby
44
+ # Authenticate and set authentication session to configuration.
45
+ BrickFTP::API::Authentication.login('koshigoe', 'password')
46
+ ```
47
+
48
+ ### Other APIs
49
+
50
+ see API document or source code.
51
+
52
+ ## Development
53
+
54
+ 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.
55
+
56
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
57
+
58
+ ## Contributing
59
+
60
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/brick_ftp.
61
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "brick_ftp"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/brick_ftp.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'brick_ftp/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "brick_ftp"
8
+ spec.version = BrickFTP::VERSION
9
+ spec.authors = ["koshigoe"]
10
+ spec.email = ["koshigoeb@gmail.com"]
11
+
12
+ spec.summary = %q{BrickFTP's REST API client.}
13
+ spec.description = %q{BrickFTP's REST API client.}
14
+ spec.homepage = "https://github.com/koshigoe/brick_ftp"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.12"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency "webmock", "~> 2.1"
25
+
26
+ spec.add_dependency 'deep_hash_transform', '~> 1.0'
27
+ end
data/lib/brick_ftp.rb ADDED
@@ -0,0 +1,44 @@
1
+ require 'deep_hash_transform'
2
+ require "brick_ftp/version"
3
+ require 'brick_ftp/configuration'
4
+ require 'brick_ftp/http_client'
5
+ require 'brick_ftp/api'
6
+ require 'brick_ftp/api/base'
7
+ require 'brick_ftp/api/authentication'
8
+ require 'brick_ftp/api/authentication/session'
9
+ require 'brick_ftp/api/user'
10
+ require 'brick_ftp/api/public_key'
11
+ require 'brick_ftp/api/group'
12
+ require 'brick_ftp/api/permission'
13
+ require 'brick_ftp/api/notification'
14
+ require 'brick_ftp/api/history'
15
+ require 'brick_ftp/api/history/site'
16
+ require 'brick_ftp/api/history/login'
17
+ require 'brick_ftp/api/history/user'
18
+ require 'brick_ftp/api/history/folder'
19
+ require 'brick_ftp/api/history/file'
20
+ require 'brick_ftp/api/bundle'
21
+ require 'brick_ftp/api/bundle_content'
22
+ require 'brick_ftp/api/bundle_download'
23
+ require 'brick_ftp/api/behavior'
24
+ require 'brick_ftp/api/behavior_folder'
25
+ require 'brick_ftp/api/folder'
26
+ require 'brick_ftp/api/file'
27
+ require 'brick_ftp/api/file_move'
28
+ require 'brick_ftp/api/file_copy'
29
+ require 'brick_ftp/api/file_upload'
30
+
31
+ module BrickFTP
32
+ def self.config
33
+ @config ||= BrickFTP::Configuration.new
34
+ end
35
+
36
+ def self.config=(config)
37
+ raise TypeError unless config.is_a?(BrickFTP::Configuration)
38
+ @config = config
39
+ end
40
+
41
+ def self.configure
42
+ yield(config)
43
+ end
44
+ end
@@ -0,0 +1,4 @@
1
+ module BrickFTP
2
+ module API
3
+ end
4
+ end
@@ -0,0 +1,21 @@
1
+ require 'cgi'
2
+
3
+ module BrickFTP
4
+ module API
5
+ module Authentication
6
+ COOKIE_NAME = 'BrickFTP'.freeze
7
+
8
+ def self.cookie(session)
9
+ CGI::Cookie.new(COOKIE_NAME, session.id)
10
+ end
11
+
12
+ def self.login(username, password)
13
+ Session.create(username: username, password: password)
14
+ end
15
+
16
+ def self.logout
17
+ BrickFTP.config.session.destroy
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ module BrickFTP
2
+ module API
3
+ module Authentication
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
9
+
10
+ def self.create(params = {})
11
+ super.tap { |x| BrickFTP.config.session = x }
12
+ end
13
+
14
+ def destroy
15
+ super.tap do
16
+ @id = nil
17
+ BrickFTP.config.session = nil
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,111 @@
1
+ require 'cgi'
2
+
3
+ module BrickFTP
4
+ module API
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
+ 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
24
+ end
25
+
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
64
+
65
+ BrickFTP::HTTPClient.new.get(path).map { |x| new(x.symbolize_keys) }
66
+ end
67
+
68
+ 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))
72
+ data.empty? ? nil : new(data.symbolize_keys)
73
+ end
74
+
75
+ def self.create(params = {}, path_params = {})
76
+ params.symbolize_keys!
77
+ undefined_attributes = params.keys - writable_attributes
78
+ raise UndefinedAttributesError, undefined_attributes unless undefined_attributes.empty?
79
+
80
+ data = BrickFTP::HTTPClient.new.post(api_path_for(:create, path_params), params: params)
81
+ data = {} if data.is_a?(Array)
82
+ new(data.symbolize_keys)
83
+ end
84
+
85
+ def initialize(params = {})
86
+ undefined_attributes = params.keys - self.class.attributes
87
+ raise UndefinedAttributesError, undefined_attributes unless undefined_attributes.empty?
88
+
89
+ params.each { |k, v| instance_variable_set(:"@#{k}", v) }
90
+ end
91
+
92
+ def update(params = {})
93
+ params.symbolize_keys!
94
+ undefined_attributes = params.keys - self.class.writable_attributes
95
+ raise UndefinedAttributesError, undefined_attributes unless undefined_attributes.empty?
96
+
97
+ data = BrickFTP::HTTPClient.new.put(self.class.api_path_for(:update, id: id), params: params)
98
+ data.each { |k, v| instance_variable_set(:"@#{k}", v) }
99
+
100
+ self
101
+ end
102
+
103
+ 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))
107
+ true
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,13 @@
1
+ module BrickFTP
2
+ module API
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
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,8 @@
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
@@ -0,0 +1,12 @@
1
+ module BrickFTP
2
+ module API
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
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,20 @@
1
+ module BrickFTP
2
+ module API
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
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
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,18 @@
1
+ module BrickFTP
2
+ module API
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
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
+ data = BrickFTP::HTTPClient.new.post(api_path_for(:index, path_params), params: params)
14
+ data.map { |x| new(x.symbolize_keys) }
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,9 @@
1
+ module BrickFTP
2
+ module API
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
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,8 @@
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
@@ -0,0 +1,8 @@
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
@@ -0,0 +1,43 @@
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(params, path_params = {})
30
+ api_client = BrickFTP::HTTPClient.new
31
+ step1 = api_client.post(api_path_for(:create, path_params), 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: params)
36
+
37
+ step3 = api_client.post(api_path_for(:create, path_params), params: { action: 'end', ref: step1['ref'] })
38
+
39
+ new(step1.merge(step3).symbolize_keys)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,13 @@
1
+ module BrickFTP
2
+ module API
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
7
+
8
+ def self.create(path_params = {})
9
+ super({}, path_params)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module BrickFTP
2
+ module API
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
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,6 @@
1
+ module BrickFTP
2
+ module APi
3
+ module History
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,21 @@
1
+ module BrickFTP
2
+ module API
3
+ module History
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
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module BrickFTP
2
+ module API
3
+ module History
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
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module BrickFTP
2
+ module API
3
+ module History
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
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module BrickFTP
2
+ module API
3
+ module History
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
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module BrickFTP
2
+ module API
3
+ module History
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
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,11 @@
1
+ module BrickFTP
2
+ module API
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
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module BrickFTP
2
+ module API
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
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module BrickFTP
2
+ module API
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
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,35 @@
1
+ module BrickFTP
2
+ module API
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
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,9 @@
1
+ module BrickFTP
2
+ class Client
3
+ attr_reader :api_key
4
+
5
+ def initialize(api_key: nil)
6
+ @api_key = api_key
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,23 @@
1
+ require 'logger'
2
+
3
+ module BrickFTP
4
+ class Configuration
5
+ attr_accessor :subdomain
6
+ attr_accessor :api_key
7
+ attr_accessor :session
8
+ attr_accessor :logger
9
+
10
+ def initialize
11
+ self.subdomain = ENV['BRICK_FTP_SUBDOMAIN']
12
+ self.api_key = ENV['BRICK_FTP_API_KEY']
13
+ self.session = nil
14
+
15
+ self.logger = Logger.new(STDOUT)
16
+ logger.level = Logger::WARN
17
+ end
18
+
19
+ def api_host
20
+ "#{subdomain}.brickftp.com"
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,100 @@
1
+ require 'net/https'
2
+ require 'json'
3
+
4
+ module BrickFTP
5
+ class HTTPClient
6
+ class Error < StandardError
7
+ def initialize(response)
8
+ begin
9
+ error = JSON.parse(response.body)
10
+ rescue
11
+ error = { 'http-code' => response.code, 'error' => "#{response.message}, #{response.body}" }
12
+ end
13
+
14
+ case
15
+ when error.key?('http-code')
16
+ super "#{error['http-code']}: #{error['error']}"
17
+ when error.key?('errors')
18
+ super error['errors'].join('. ')
19
+ else
20
+ super 'unknown error.'
21
+ end
22
+ end
23
+ end
24
+
25
+ USER_AGENT = 'BrickFTP Client/0.1 (https://github.com/koshigoe/brick_ftp)'.freeze
26
+
27
+ def initialize(host = nil)
28
+ @host = host || BrickFTP.config.api_host
29
+ @conn = Net::HTTP.new(@host, 443)
30
+ @conn.use_ssl = true
31
+ end
32
+
33
+ def get(path)
34
+ case res = request(:get, path)
35
+ when Net::HTTPSuccess
36
+ res.body.nil? || res.body.empty? ? {} : JSON.parse(res.body)
37
+ else
38
+ # TODO: redirect
39
+ raise Error, res
40
+ end
41
+ end
42
+
43
+ def post(path, params: {})
44
+ case res = request(:post, path, params: params)
45
+ when Net::HTTPSuccess, Net::HTTPCreated
46
+ res.body.nil? || res.body.empty? ? {} : JSON.parse(res.body)
47
+ else
48
+ # TODO: redirect
49
+ raise Error, res
50
+ end
51
+ end
52
+
53
+ def put(path, params: {})
54
+ case res = request(:put, path, params: params)
55
+ when Net::HTTPSuccess
56
+ res.body.nil? || res.body.empty? ? {} : JSON.parse(res.body)
57
+ else
58
+ # TODO: redirect
59
+ raise Error, res
60
+ end
61
+ end
62
+
63
+ def delete(path)
64
+ case res = request(:delete, path)
65
+ when Net::HTTPSuccess
66
+ true
67
+ else
68
+ # TODO: redirect
69
+ raise Error, res
70
+ end
71
+ end
72
+
73
+ private
74
+
75
+ def request(method, path, params: {}, headers: {})
76
+ req = Net::HTTP.const_get(method.to_s.capitalize).new(path, headers)
77
+ req['User-Agent'] = USER_AGENT
78
+
79
+ if @host == BrickFTP.config.api_host
80
+ req['Content-Type'] = 'application/json'
81
+ case
82
+ when BrickFTP.config.session
83
+ req['Cookie'] = BrickFTP::API::Authentication.cookie(BrickFTP.config.session).to_s
84
+ when BrickFTP.config.api_key
85
+ req.basic_auth(BrickFTP.config.api_key, 'x')
86
+ end
87
+ end
88
+
89
+ case
90
+ when params.is_a?(Hash)
91
+ req.body = params.to_json unless params.empty?
92
+ when params.is_a?(IO)
93
+ req.body_stream = params
94
+ req["Content-Length"] = params.size
95
+ end
96
+
97
+ @conn.request(req)
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,3 @@
1
+ module BrickFTP
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: brick_ftp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - koshigoe
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-10-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: deep_hash_transform
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.0'
83
+ description: BrickFTP's REST API client.
84
+ email:
85
+ - koshigoeb@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - brick_ftp.gemspec
100
+ - lib/brick_ftp.rb
101
+ - lib/brick_ftp/api.rb
102
+ - lib/brick_ftp/api/authentication.rb
103
+ - lib/brick_ftp/api/authentication/session.rb
104
+ - lib/brick_ftp/api/base.rb
105
+ - lib/brick_ftp/api/behavior.rb
106
+ - lib/brick_ftp/api/behavior_folder.rb
107
+ - lib/brick_ftp/api/bundle.rb
108
+ - lib/brick_ftp/api/bundle_content.rb
109
+ - lib/brick_ftp/api/bundle_download.rb
110
+ - lib/brick_ftp/api/file.rb
111
+ - lib/brick_ftp/api/file_copy.rb
112
+ - lib/brick_ftp/api/file_move.rb
113
+ - lib/brick_ftp/api/file_upload.rb
114
+ - lib/brick_ftp/api/folder.rb
115
+ - lib/brick_ftp/api/group.rb
116
+ - lib/brick_ftp/api/history.rb
117
+ - lib/brick_ftp/api/history/file.rb
118
+ - lib/brick_ftp/api/history/folder.rb
119
+ - lib/brick_ftp/api/history/login.rb
120
+ - lib/brick_ftp/api/history/site.rb
121
+ - lib/brick_ftp/api/history/user.rb
122
+ - lib/brick_ftp/api/notification.rb
123
+ - lib/brick_ftp/api/permission.rb
124
+ - lib/brick_ftp/api/public_key.rb
125
+ - lib/brick_ftp/api/user.rb
126
+ - lib/brick_ftp/client.rb
127
+ - lib/brick_ftp/configuration.rb
128
+ - lib/brick_ftp/http_client.rb
129
+ - lib/brick_ftp/version.rb
130
+ homepage: https://github.com/koshigoe/brick_ftp
131
+ licenses: []
132
+ metadata: {}
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ requirements: []
148
+ rubyforge_project:
149
+ rubygems_version: 2.5.1
150
+ signing_key:
151
+ specification_version: 4
152
+ summary: BrickFTP's REST API client.
153
+ test_files: []