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


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_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

#engine_nameObject (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_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


21
22
23
# File 'lib/jsonapi/link_builder.rb', line 21

def engine?
  !!@engine_name
end

#primary_resources_urlObject


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

33
34
35
# File 'lib/jsonapi/link_builder.rb', line 33

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

37
38
39
40
41
# File 'lib/jsonapi/link_builder.rb', line 37

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

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

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