Class: JSONAPI::LinkBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi/link_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ LinkBuilder

Returns a new instance of LinkBuilder.



7
8
9
10
11
12
# File 'lib/jsonapi/link_builder.rb', line 7

def initialize(config = {})
  @base_url               = config[:base_url]
  @primary_resource_klass = config[:primary_resource_klass]
  @route_formatter        = config[:route_formatter]
  @is_engine              = !!engine_name
end

Instance Attribute Details

#base_urlObject (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

#primary_resource_klassObject (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_formatterObject (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

Returns:

  • (Boolean)


14
15
16
# File 'lib/jsonapi/link_builder.rb', line 14

def engine?
  @is_engine
end

#engine_nameObject



18
19
20
# File 'lib/jsonapi/link_builder.rb', line 18

def engine_name
  @engine_name ||= build_engine_name
end

#primary_resources_urlObject



22
23
24
25
26
27
28
# File 'lib/jsonapi/link_builder.rb', line 22

def primary_resources_url
  if engine?
    engine_primary_resources_url
  else
    regular_primary_resources_url
  end
end


30
31
32
# File 'lib/jsonapi/link_builder.rb', line 30

def query_link(query_params)
  "#{ primary_resources_url }?#{ query_params.to_query }"
end


34
35
36
37
38
# File 'lib/jsonapi/link_builder.rb', line 34

def relationships_related_link(source, relationship, query_params = {})
  url = "#{ self_link(source) }/#{ route_for_relationship(relationship) }"
  url = "#{ url }?#{ query_params.to_query }" if query_params.present?
  url
end


40
41
42
# File 'lib/jsonapi/link_builder.rb', line 40

def relationships_self_link(source, relationship)
  "#{ self_link(source) }/relationships/#{ route_for_relationship(relationship) }"
end


44
45
46
47
48
49
50
# File 'lib/jsonapi/link_builder.rb', line 44

def self_link(source)
  if engine?
    engine_resource_url(source)
  else
    regular_resource_url(source)
  end
end