Class: RemoteResource::UrlNamingDetermination

Inherits:
Object
  • Object
show all
Defined in:
lib/remote_resource/url_naming_determination.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_klass, connection_options = {}) ⇒ UrlNamingDetermination

Returns a new instance of UrlNamingDetermination.



6
7
8
9
# File 'lib/remote_resource/url_naming_determination.rb', line 6

def initialize(resource_klass, connection_options = {})
  @resource_klass     = resource_klass
  @connection_options = connection_options
end

Instance Attribute Details

#connection_optionsObject (readonly)

Returns the value of attribute connection_options.



4
5
6
# File 'lib/remote_resource/url_naming_determination.rb', line 4

def connection_options
  @connection_options
end

#resource_klassObject (readonly)

Returns the value of attribute resource_klass.



4
5
6
# File 'lib/remote_resource/url_naming_determination.rb', line 4

def resource_klass
  @resource_klass
end

Instance Method Details

#base_url(id = nil, check_collection_options: false) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/remote_resource/url_naming_determination.rb', line 11

def base_url(id = nil, check_collection_options: false)
  site         = connection_options.fetch(:site, resource_klass.site)
  version      = connection_options.fetch(:version, resource_klass.version)
  path_prefix  = connection_options.fetch(:path_prefix, resource_klass.path_prefix)
  path_postfix = connection_options.fetch(:path_postfix, resource_klass.path_postfix)

  File.join(site.to_s, version.to_s, path_prefix.to_s, collection_prefix(check_collection_options).to_s, url_safe_relative_name, id.to_s, path_postfix.to_s).chomp('/')
end

#collection_prefix(check_collection_options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/remote_resource/url_naming_determination.rb', line 20

def collection_prefix(check_collection_options)
  prefix = connection_options.fetch(:collection_prefix, resource_klass.collection_prefix)

  if prefix.present?
    prefix = "/#{prefix}" unless prefix.chr == '/'
    collection_options = connection_options.fetch(:collection_options, {}).with_indifferent_access

    prefix.gsub(/:\w+/) do |key|
      value = collection_options.fetch(key[1..-1], nil)
      if value.nil?
        raise(RemoteResource::CollectionOptionKeyError, "`collection_prefix` variable `#{key}` is missing from `collection_options`") if check_collection_options
        value = key
      end
      URI.parser.escape(value.to_s)
    end
  end
end

#relative_nameObject



48
49
50
51
52
# File 'lib/remote_resource/url_naming_determination.rb', line 48

def relative_name
  collection_name = connection_options.fetch(:collection_name, resource_klass.collection_name)

  collection_name.to_s.presence || resource_klass.name.to_s.demodulize
end

#url_safe_relative_nameObject



38
39
40
41
42
43
44
45
46
# File 'lib/remote_resource/url_naming_determination.rb', line 38

def url_safe_relative_name
  collection = connection_options.fetch(:collection, resource_klass.collection)

  if collection
    relative_name.underscore.downcase.pluralize
  else
    relative_name.underscore.downcase
  end
end