Class: BrickFTP::API::Base
- Inherits:
-
Object
- Object
- BrickFTP::API::Base
show all
- Defined in:
- lib/brick_ftp/api/base.rb
Direct Known Subclasses
Authentication::Session, Behavior, BehaviorFolder, Bundle, BundleContent, BundleDownload, File, FileCopy, FileMove, FileUpload, Folder, Group, History::File, History::Folder, History::Login, History::Site, History::User, Notification, Permission, PublicKey, User
Defined Under Namespace
Classes: NoSuchAPIError, UndefinedAttributesError
Constant Summary
collapse
- QUERY_PARAMS =
%i(page per_page start_at recursive).freeze
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(params = {}) ⇒ Base
Returns a new instance of Base.
85
86
87
88
89
90
|
# File 'lib/brick_ftp/api/base.rb', line 85
def initialize(params = {})
undefined_attributes = params.keys - self.class.attributes
raise UndefinedAttributesError, undefined_attributes unless undefined_attributes.empty?
params.each { |k, v| instance_variable_set(:"@#{k}", v) }
end
|
Class Attribute Details
.api ⇒ Object
Returns the value of attribute api.
23
24
25
|
# File 'lib/brick_ftp/api/base.rb', line 23
def api
@api
end
|
.readonly_attributes ⇒ Object
Returns the value of attribute readonly_attributes.
23
24
25
|
# File 'lib/brick_ftp/api/base.rb', line 23
def readonly_attributes
@readonly_attributes
end
|
.writable_attributes ⇒ Object
Returns the value of attribute writable_attributes.
23
24
25
|
# File 'lib/brick_ftp/api/base.rb', line 23
def writable_attributes
@writable_attributes
end
|
Class Method Details
.all(path_params = {}) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/brick_ftp/api/base.rb', line 51
def self.all(path_params = {})
path_params.symbolize_keys!
query_params = QUERY_PARAMS.each_with_object({}) do |name, res|
res.update(name => path_params.delete(name)) if path_params.key?(name)
end
path = api_path_for(:index, path_params)
unless query_params.empty?
query = query_params.each_with_object([]) do |(k, v), res|
res << "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"
end.join('&')
path = "#{path}?#{query}"
end
BrickFTP::HTTPClient.new.get(path).map { |x| new(x.symbolize_keys) }
end
|
.api_path_for(method, params = {}) ⇒ Object
44
45
46
47
|
# File 'lib/brick_ftp/api/base.rb', line 44
def self.api_path_for(method, params = {})
raise NoSuchAPIError, "#{method} #{self.name}" unless @api.key?(method)
@api[method] % Hash[params.map { |k, v| [k, CGI.escape(v.to_s)] }]
end
|
.attributes ⇒ Object
26
27
28
|
# File 'lib/brick_ftp/api/base.rb', line 26
def self.attributes
writable_attributes + readonly_attributes
end
|
.create(params = {}, path_params = {}) ⇒ Object
75
76
77
78
79
80
81
82
83
|
# File 'lib/brick_ftp/api/base.rb', line 75
def self.create(params = {}, path_params = {})
params.symbolize_keys!
undefined_attributes = params.keys - writable_attributes
raise UndefinedAttributesError, undefined_attributes unless undefined_attributes.empty?
data = BrickFTP::HTTPClient.new.post(api_path_for(:create, path_params), params: params)
data = {} if data.is_a?(Array)
new(data.symbolize_keys)
end
|
.define_api(method, path) ⇒ Object
30
31
32
|
# File 'lib/brick_ftp/api/base.rb', line 30
def self.define_api(method, path)
@api[method] = path
end
|
.define_readonly_attributes(*attributes) ⇒ Object
39
40
41
42
|
# File 'lib/brick_ftp/api/base.rb', line 39
def self.define_readonly_attributes(*attributes)
@readonly_attributes = attributes
attr_reader *@readonly_attributes.map { |x| x.to_s.tr('-', '_') }
end
|
.define_writable_attributes(*attributes) ⇒ Object
34
35
36
37
|
# File 'lib/brick_ftp/api/base.rb', line 34
def self.define_writable_attributes(*attributes)
@writable_attributes = attributes
attr_reader *@writable_attributes.map { |x| x.to_s.tr('-', '_') }
end
|
.find(id) ⇒ Object
68
69
70
71
72
73
|
# File 'lib/brick_ftp/api/base.rb', line 68
def self.find(id)
params = {}
api[:show].scan(/%\{([^}]+)\}/) { |m| params[m.first.to_sym] = id }
data = BrickFTP::HTTPClient.new.get(api_path_for(:show, params))
data.empty? ? nil : new(data.symbolize_keys)
end
|
.inherited(subclass) ⇒ Object
14
15
16
17
18
19
20
|
# File 'lib/brick_ftp/api/base.rb', line 14
def self.inherited(subclass)
subclass.instance_eval do
@api = {}
@writable_attributes = []
@readonly_attributes = []
end
end
|
Instance Method Details
#destroy ⇒ Object
103
104
105
106
107
108
|
# File 'lib/brick_ftp/api/base.rb', line 103
def destroy
params = {}
self.class.api[:delete].scan(/%\{([^}]+)\}/) { |m| params[m.first.to_sym] = send(m.first.to_sym) }
BrickFTP::HTTPClient.new.delete(self.class.api_path_for(:delete, params))
true
end
|
#update(params = {}) ⇒ Object
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/brick_ftp/api/base.rb', line 92
def update(params = {})
params.symbolize_keys!
undefined_attributes = params.keys - self.class.writable_attributes
raise UndefinedAttributesError, undefined_attributes unless undefined_attributes.empty?
data = BrickFTP::HTTPClient.new.put(self.class.api_path_for(:update, id: id), params: params)
data.each { |k, v| instance_variable_set(:"@#{k}", v) }
self
end
|