Module: LogicalModel::UrlHelper::ClassMethods

Defined in:
lib/logical_model/url_helper.rb

Overview

adds following setters

  • force_ssl

  • set_resource_host

  • set_resource_path

add reader

  • resource_uri

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hostObject

Returns the value of attribute host.



17
18
19
# File 'lib/logical_model/url_helper.rb', line 17

def host
  @host
end

#resource_pathObject

Returns the value of attribute resource_path.



17
18
19
# File 'lib/logical_model/url_helper.rb', line 17

def resource_path
  @resource_path
end

#use_sslObject

Returns the value of attribute use_ssl.



17
18
19
# File 'lib/logical_model/url_helper.rb', line 17

def use_ssl
  @use_ssl
end

Instance Method Details

#do_with_resource_path(new_path) ⇒ Object

Requests done within the block will go to new path.

Examples:

@resource_path # '/comments'
do_with_resource_path("users/#{@user_id}/#{@resource_path}"}/") do
  @resource_path # '/users/23/comments'
end

Parameters:

  • new_path (String)


84
85
86
87
88
89
# File 'lib/logical_model/url_helper.rb', line 84

def do_with_resource_path(new_path)
  bkp_path = @resource_path
  @resource_path = new_path
  yield
  @resource_path = bkp_path
end

#force_sslObject

If called in class, will make al request through SSL.

Examples:

class Client < LogicalModel
  force_ssl
  ...
end


34
35
36
# File 'lib/logical_model/url_helper.rb', line 34

def force_ssl
  @use_ssl = true
end

#resource_uri(id = nil) ⇒ Object

Will return path to resource

Parameters:

  • id (String) (defaults to: nil)

    (nil)



23
24
25
26
# File 'lib/logical_model/url_helper.rb', line 23

def resource_uri(id=nil)
  sufix  = (id.nil?)? "" : "/#{id}"
  "#{url_protocol_prefix}#{host}#{resource_path}#{sufix}"
end

#set_resource_host(new_host) ⇒ Object



45
46
47
# File 'lib/logical_model/url_helper.rb', line 45

def set_resource_host(new_host)
  @host = new_host
end

#set_resource_path(new_path) ⇒ Object



49
50
51
# File 'lib/logical_model/url_helper.rb', line 49

def set_resource_path(new_path)
  @resource_path = new_path
end

#set_resource_url(new_host, new_path) ⇒ Object

Parameters:

  • new_host (String)

    resource host. Should NOT include protocol (http)

  • new_path (String)

    resource path in host



40
41
42
43
# File 'lib/logical_model/url_helper.rb', line 40

def set_resource_url(new_host,new_path)
  @host = new_host
  @resource_path = new_path
end

#ssl_recommended?Boolean

Returns true if ssl is recommended

  • requests to localhost -> false

  • other -> true

Returns:

  • (Boolean)


71
72
73
# File 'lib/logical_model/url_helper.rb', line 71

def ssl_recommended?
  (@host && !(@host =~ /localhost/))
end

#url_protocol_prefixString

Returns:

  • (String)


61
62
63
# File 'lib/logical_model/url_helper.rb', line 61

def url_protocol_prefix
  (use_ssl?)? "https://" : "http://"
end

#use_ssl?Boolean

Default use_ssl to ssl_recommend?

Returns:

  • (Boolean)


56
57
58
# File 'lib/logical_model/url_helper.rb', line 56

def use_ssl?
  @use_ssl ||= ssl_recommended?
end