Class: BrickFTP::APIComponent
- Inherits:
-
Object
- Object
- BrickFTP::APIComponent
- Defined in:
- lib/brick_ftp/api_component.rb
Instance Attribute Summary collapse
-
#path_template ⇒ Object
readonly
Returns the value of attribute path_template.
-
#query_keys ⇒ Object
readonly
Returns the value of attribute query_keys.
Instance Method Summary collapse
- #build_path_params_from(params = {}) ⇒ Object
- #build_query_params_from(params = {}) ⇒ Object
- #build_query_string_from(params = {}) ⇒ Object
- #except_path_and_query(params = {}) ⇒ Object
-
#initialize(path_template, query_keys = []) ⇒ APIComponent
constructor
A new instance of APIComponent.
- #path(params) ⇒ Object
Constructor Details
#initialize(path_template, query_keys = []) ⇒ APIComponent
Returns a new instance of APIComponent.
7 8 9 10 |
# File 'lib/brick_ftp/api_component.rb', line 7 def initialize(path_template, query_keys = []) @path_template = path_template.respond_to?(:call) ? path_template : ->(_) { path_template } @query_keys = query_keys.map(&:to_sym) end |
Instance Attribute Details
#path_template ⇒ Object (readonly)
Returns the value of attribute path_template.
5 6 7 |
# File 'lib/brick_ftp/api_component.rb', line 5 def path_template @path_template end |
#query_keys ⇒ Object (readonly)
Returns the value of attribute query_keys.
5 6 7 |
# File 'lib/brick_ftp/api_component.rb', line 5 def query_keys @query_keys end |
Instance Method Details
#build_path_params_from(params = {}) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/brick_ftp/api_component.rb', line 25 def build_path_params_from(params = {}) params = normalize_params(params) path_variables(params).each_with_object({}) do |key, res| res[key] = params[key] end end |
#build_query_params_from(params = {}) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/brick_ftp/api_component.rb', line 33 def build_query_params_from(params = {}) params = normalize_params(params) query_keys.each_with_object({}) do |name, res| next unless params.key?(name) res[name] = params[name] end end |
#build_query_string_from(params = {}) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/brick_ftp/api_component.rb', line 42 def build_query_string_from(params = {}) params = normalize_params(params) pairs = build_query_params_from(params).each_with_object([]) do |(k, v), res| res << "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" end pairs.join('&') end |
#except_path_and_query(params = {}) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/brick_ftp/api_component.rb', line 51 def except_path_and_query(params = {}) params = normalize_params(params) exceptions = path_variables(params) + query_keys params.reject { |k, _| exceptions.include?(k) } end |
#path(params) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/brick_ftp/api_component.rb', line 12 def path(params) params = normalize_params(params) path_params = build_path_params_from(params) escaped_path_params = path_params.each_with_object({}) do |(k, v), res| res[k] = ERB::Util.url_encode(v.to_s) end path = path_template.call(params) % escaped_path_params query = build_query_string_from(params) query.empty? ? path : "#{path}?#{query}" end |