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 ⇒ Object
readonly
Returns the value of attribute engine.
-
#primary_resource_klass ⇒ Object
readonly
Returns the value of attribute primary_resource_klass.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
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
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/jsonapi/link_builder.rb', line 8 def initialize(config = {}) @base_url = config[:base_url] @primary_resource_klass = config[:primary_resource_klass] @engine = build_engine if engine? @routes = @engine.routes else @routes = Rails.application.routes end # ToDo: Use NaiveCache for values. For this we need to not return nils and create composite keys which work # as efficient cache lookups. This could be an array of the [source.identifier, relationship] since the # ResourceIdentity will compare equality correctly 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 ⇒ Object (readonly)
Returns the value of attribute engine.
3 4 5 |
# File 'lib/jsonapi/link_builder.rb', line 3 def engine @engine 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 |
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
3 4 5 |
# File 'lib/jsonapi/link_builder.rb', line 3 def routes @routes end |
Instance Method Details
#engine? ⇒ Boolean
24 25 26 |
# File 'lib/jsonapi/link_builder.rb', line 24 def engine? !!@engine end |
#primary_resources_url ⇒ Object
28 29 30 31 32 |
# File 'lib/jsonapi/link_builder.rb', line 28 def primary_resources_url @primary_resources_url_cached ||= "#{ base_url }#{ primary_resources_path }" rescue NoMethodError warn "primary_resources_url for #{@primary_resource_klass} could not be generated" if JSONAPI.configuration.warn_on_missing_routes end |
#query_link(query_params) ⇒ Object
34 35 36 |
# File 'lib/jsonapi/link_builder.rb', line 34 def query_link(query_params) "#{ primary_resources_url }?#{ query_params.to_query }" end |
#relationships_related_link(source, relationship, query_params = {}) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/jsonapi/link_builder.rb', line 38 def (source, relationship, query_params = {}) if relationship.parent_resource.singleton? url_helper_name = (relationship) url = call_url_helper(url_helper_name) else url_helper_name = (relationship) url = call_url_helper(url_helper_name, source.id) end url = "#{ base_url }#{ url }" url = "#{ url }?#{ query_params.to_query }" if query_params.present? url rescue NoMethodError warn "related_link for #{relationship} could not be generated" if JSONAPI.configuration.warn_on_missing_routes end |
#relationships_self_link(source, relationship) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/jsonapi/link_builder.rb', line 54 def relationships_self_link(source, relationship) if relationship.parent_resource.singleton? url_helper_name = singleton_relationship_self_url_helper_name(relationship) url = call_url_helper(url_helper_name) else url_helper_name = relationship_self_url_helper_name(relationship) url = call_url_helper(url_helper_name, source.id) end url = "#{ base_url }#{ url }" url rescue NoMethodError warn "self_link for #{relationship} could not be generated" if JSONAPI.configuration.warn_on_missing_routes end |
#self_link(source) ⇒ Object
69 70 71 72 73 |
# File 'lib/jsonapi/link_builder.rb', line 69 def self_link(source) "#{ base_url }#{ resource_path(source) }" rescue NoMethodError warn "self_link for #{source.class} could not be generated" if JSONAPI.configuration.warn_on_missing_routes end |