Module: LazyResource::UrlGeneration::ClassMethods
- Defined in:
- lib/lazy_resource/url_generation.rb
Instance Method Summary collapse
-
#collection_path(prefix_options = {}, query_options = nil, from = nil, include_query = true) ⇒ Object
Gets the collection path for the REST resources.
-
#element_path(id, prefix_options = {}, query_options = nil, from = nil) ⇒ Object
Gets the element path for the given ID in
id
. -
#new_element_path(prefix_options = {}, from = nil) ⇒ Object
Gets the new element path for REST resources.
-
#prefix(options = {}) ⇒ Object
Gets the prefix for a resource’s nested URL (e.g.,
prefix/collectionname/1
). -
#query_string(options) ⇒ Object
Builds the query string for the request.
-
#split_options(options = {}) ⇒ Object
split an option hash into two hashes, one containing the prefix options, and the other containing the leftovers.
Instance Method Details
#collection_path(prefix_options = {}, query_options = nil, from = nil, include_query = true) ⇒ Object
Gets the collection path for the REST resources. If the query_options
parameter is omitted, Rails will split from the prefix_options
.
Options
-
prefix_options
- A hash to add a prefix to the request for nested URLs (e.g.,:account_id => 19
would yield a URL like/accounts/19/purchases.json
). -
query_options
- A hash to add items to the query string for the request.
75 76 77 78 79 80 81 |
# File 'lib/lazy_resource/url_generation.rb', line 75 def collection_path( = {}, = nil, from = nil, include_query = true) , = () if .nil? from = self.from if from.nil? && respond_to?(:from) path = "#{prefix()}#{from || collection_name}" path += "#{query_string()}" if include_query path end |
#element_path(id, prefix_options = {}, query_options = nil, from = nil) ⇒ Object
Gets the element path for the given ID in id
. If the query_options
parameter is omitted, Rails will split from the prefix options.
Options
prefix_options
- A hash to add a prefix to the request for nested URLs (e.g., :account_id => 19
would yield a URL like /accounts/19/purchases.json
).
query_options
- A hash to add items to the query string for the request.
52 53 54 55 56 |
# File 'lib/lazy_resource/url_generation.rb', line 52 def element_path(id, = {}, = nil, from = nil) , = () if .nil? from = self.from if from.nil? && respond_to?(:from) "#{prefix()}#{from || collection_name}/#{URI.escape id.to_s}#{query_string()}" end |
#new_element_path(prefix_options = {}, from = nil) ⇒ Object
Gets the new element path for REST resources.
Options
-
prefix_options
- A hash to add a prefix to the request for nested URLs (e.g.,:account_id => 19
would yield a URL like /accounts/19/purchases/new.json
).
63 64 65 66 |
# File 'lib/lazy_resource/url_generation.rb', line 63 def new_element_path( = {}, from = nil) from = self.from if from.nil? && respond_to?(:from) "#{prefix()}#{from || collection_name}/new" end |
#prefix(options = {}) ⇒ Object
Gets the prefix for a resource’s nested URL (e.g., prefix/collectionname/1
)
33 34 35 36 37 38 39 40 41 |
# File 'lib/lazy_resource/url_generation.rb', line 33 def prefix(={}) path = '/' = .to_a.uniq path = .inject(path) do |uri, option| key, value = option[0].to_s, option[1] uri << ActiveSupport::Inflector.pluralize(key.gsub("_id", '')) uri << "/#{value}/" end end |
#query_string(options) ⇒ Object
Builds the query string for the request.
84 85 86 |
# File 'lib/lazy_resource/url_generation.rb', line 84 def query_string() "?#{.to_query}" unless .nil? || .empty? end |
#split_options(options = {}) ⇒ Object
split an option hash into two hashes, one containing the prefix options, and the other containing the leftovers.
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/lazy_resource/url_generation.rb', line 90 def ( = {}) , = {}, {} ( || {}).each do |key, value| next if key.blank? (key =~ /\w*_id$/ ? : )[key.to_sym] = value end [, ] end |