Module: LazyResource::UrlGeneration::ClassMethods

Defined in:
lib/lazy_resource/url_generation.rb

Instance Method Summary collapse

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(prefix_options = {}, query_options = nil, from = nil, include_query = true)
  prefix_options, query_options = split_options(prefix_options) if query_options.nil?
  from = self.from if from.nil? && respond_to?(:from)
  path = "#{prefix(prefix_options)}#{from || collection_name}"
  path += "#{query_string(query_options)}" 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, prefix_options = {}, query_options = nil, from = nil)
  prefix_options, query_options = split_options(prefix_options) if query_options.nil?
  from = self.from if from.nil? && respond_to?(:from)
  "#{prefix(prefix_options)}#{from || collection_name}/#{URI.escape id.to_s}#{query_string(query_options)}"
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(prefix_options = {}, from = nil)
  from = self.from if from.nil? && respond_to?(:from)
  "#{prefix(prefix_options)}#{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(options={})
  path = '/'
  options = options.to_a.uniq
  path = options.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(options)
  "?#{options.to_query}" unless options.nil? || options.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 split_options(options = {})
    prefix_options, query_options = {}, {}

    (options || {}).each do |key, value|
      next if key.blank?
      (key =~ /\w*_id$/ ? prefix_options : query_options)[key.to_sym] = value
    end

    [prefix_options, query_options]
end