Class: JSONAPI::LinkBuilder
- Inherits:
-
Object
- Object
- JSONAPI::LinkBuilder
- Defined in:
- lib/jsonapi/link_builder.rb
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#engine_name ⇒ Object
readonly
Returns the value of attribute engine_name.
-
#primary_resource_klass ⇒ Object
readonly
Returns the value of attribute primary_resource_klass.
-
#route_formatter ⇒ Object
readonly
Returns the value of attribute route_formatter.
Instance Method Summary collapse
- #engine? ⇒ Boolean
-
#initialize(config = {}) ⇒ LinkBuilder
constructor
A new instance of LinkBuilder.
- #primary_resources_url ⇒ Object
- #query_link(query_params) ⇒ Object
- #relationships_related_link(source, relationship, query_params = {}) ⇒ Object
- #relationships_self_link(source, relationship) ⇒ Object
- #self_link(source) ⇒ Object
Constructor Details
#initialize(config = {}) ⇒ LinkBuilder
Returns a new instance of LinkBuilder.
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/jsonapi/link_builder.rb', line 8 def initialize(config = {}) @base_url = config[:base_url] @primary_resource_klass = config[:primary_resource_klass] @route_formatter = config[:route_formatter] @engine_name = build_engine_name # Warning: These make LinkBuilder non-thread-safe. That's not a problem with the # request-specific way it's currently used, though. @resources_path_cache = JSONAPI::NaiveCache.new do |source_klass| formatted_module_path_from_class(source_klass) + format_route(source_klass._type.to_s) end end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
3 4 5 |
# File 'lib/jsonapi/link_builder.rb', line 3 def base_url @base_url end |
#engine_name ⇒ Object (readonly)
Returns the value of attribute engine_name.
3 4 5 |
# File 'lib/jsonapi/link_builder.rb', line 3 def engine_name @engine_name end |
#primary_resource_klass ⇒ Object (readonly)
Returns the value of attribute primary_resource_klass.
3 4 5 |
# File 'lib/jsonapi/link_builder.rb', line 3 def primary_resource_klass @primary_resource_klass end |
#route_formatter ⇒ Object (readonly)
Returns the value of attribute route_formatter.
3 4 5 |
# File 'lib/jsonapi/link_builder.rb', line 3 def route_formatter @route_formatter end |
Instance Method Details
#engine? ⇒ Boolean
21 22 23 |
# File 'lib/jsonapi/link_builder.rb', line 21 def engine? !!@engine_name end |
#primary_resources_url ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/jsonapi/link_builder.rb', line 25 def primary_resources_url if engine? engine_primary_resources_url else regular_primary_resources_url end end |
#query_link(query_params) ⇒ Object
33 34 35 |
# File 'lib/jsonapi/link_builder.rb', line 33 def query_link(query_params) "#{ primary_resources_url }?#{ query_params.to_query }" end |
#relationships_related_link(source, relationship, query_params = {}) ⇒ Object
37 38 39 40 41 |
# File 'lib/jsonapi/link_builder.rb', line 37 def (source, relationship, query_params = {}) url = "#{ self_link(source) }/#{ route_for_relationship(relationship) }" url = "#{ url }?#{ query_params.to_query }" if query_params.present? url end |
#relationships_self_link(source, relationship) ⇒ Object
43 44 45 |
# File 'lib/jsonapi/link_builder.rb', line 43 def relationships_self_link(source, relationship) "#{ self_link(source) }/relationships/#{ route_for_relationship(relationship) }" end |
#self_link(source) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/jsonapi/link_builder.rb', line 47 def self_link(source) if engine? engine_resource_url(source) else regular_resource_url(source) end end |