Class: JSON::LD::ContentNegotiation

Inherits:
Object
  • Object
show all
Defined in:
lib/json/ld/conneg.rb

Overview

Rack middleware for JSON-LD content negotiation.

Uses HTTP Content Negotiation to serialize Array and Hash results as JSON-LD using 'profile' accept-params to invoke appropriate JSON-LD API methods.

Allows black-listing and white-listing of two-part profiles where the second part denotes a URL of a context or frame. (See Writer.accept?)

Works along with rack-linkeddata for serializing data which is not in the form of an RDF::Repository.

Constant Summary collapse

VARY =
{'Vary' => 'Accept'}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ContentNegotiation

Returns a new instance of ContentNegotiation.


39
40
41
# File 'lib/json/ld/conneg.rb', line 39

def initialize(app)
  @app = app
end

Instance Attribute Details

#app#call (readonly)

Returns:


26
27
28
# File 'lib/json/ld/conneg.rb', line 26

def app
  @app
end

Class Method Details

.registered(app)

This method returns an undefined value.

  • Registers JSON::LD::Rack, suitable for Sinatra application
  • adds helpers

Parameters:

  • app (Sinatra::Base)

34
35
36
37
# File 'lib/json/ld/conneg.rb', line 34

def self.registered(app)
  options = {}
  app.use(JSON::LD::Rack, **options)
end

Instance Method Details

#accept_entry(entry) ⇒ Object (protected)

Returns an array of quality, number of '*' in content-type, and number of non-'q' parameters


144
145
146
147
148
149
# File 'lib/json/ld/conneg.rb', line 144

def accept_entry(entry)
  type, *options = entry.split(';').map(&:strip)
  quality = 0 # we sort smallest first
  options.delete_if { |e| quality = 1 - e[2..-1].to_f if e.start_with? 'q=' }
  [options.unshift(type).join(';'), [quality, type.count('*'), 1 - options.size]]
end

#call(env) ⇒ Array(Integer, Hash, #each)

Handles a Rack protocol request. Parses Accept header to find appropriate mime-type and sets content_type accordingly.

Parameters:

  • env (Hash{String => String})

Returns:

  • (Array(Integer, Hash, #each))

    Status, Headers and Body

See Also:


50
51
52
53
54
55
56
57
58
59
# File 'lib/json/ld/conneg.rb', line 50

def call(env)
  response = app.call(env)
  body = response[2].respond_to?(:body) ? response[2].body : response[2]
  case body
    when Array, Hash
      response[2] = body  # Put it back in the response, it might have been a proxy
      serialize(env, *response)
    else response
  end
end

#find_content_type_for_media_range(media_range) ⇒ String? (protected)

Returns a content type appropriate for the given media_range, returns nil if media_range contains a wildcard subtype that is not mapped.

Parameters:

  • media_range (String, #to_s)

Returns:

  • (String, nil)

158
159
160
161
162
163
164
# File 'lib/json/ld/conneg.rb', line 158

def find_content_type_for_media_range(media_range)
  media_range = media_range.sub('*/*', 'application/ld+json') if media_range.to_s.start_with?('*/*')
  media_range = media_range.sub('application/*', 'application/ld+json') if media_range.to_s.start_with?('application/*')
  media_range = media_range.sub('application/json', 'application/ld+json') if media_range.to_s.start_with?('application/json')
  
  media_range.start_with?('application/ld+json') ? media_range : nil
end

#http_error(code, message = nil, headers = {}) ⇒ Array(Integer, Hash, #each) (protected)

Outputs an HTTP 4xx or 5xx response.

Parameters:

  • code (Integer, #to_i)
  • message (String, #to_s) (defaults to: nil)
  • headers (Hash{String => String}) (defaults to: {})

Returns:

  • (Array(Integer, Hash, #each))

182
183
184
185
186
# File 'lib/json/ld/conneg.rb', line 182

def http_error(code, message = nil, headers = {})
  message = [code, Rack::Utils::HTTP_STATUS_CODES[code]].join(' ') +
    (message.nil? ? "\n" : " (#{message})\n")
  [code, {'Content-Type' => "text/plain"}.merge(headers), [message]]
end

#not_acceptable(message = nil) ⇒ Array(Integer, Hash, #each) (protected)

Outputs an HTTP 406 Not Acceptable response.

Parameters:

  • message (String, #to_s) (defaults to: nil)

Returns:

  • (Array(Integer, Hash, #each))

171
172
173
# File 'lib/json/ld/conneg.rb', line 171

def not_acceptable(message = nil)
  http_error(406, message, VARY)
end

#parse_accept_header(header) ⇒ Array<String> (protected)

Parses an HTTP Accept header, returning an array of MIME content types ordered by the precedence rules defined in HTTP/1.1 §14.1.

Parameters:

  • header (String, #to_s)

Returns:

See Also:


134
135
136
137
138
139
140
141
# File 'lib/json/ld/conneg.rb', line 134

def parse_accept_header(header)
  entries = header.to_s.split(',')
  entries = entries.
    map { |e| accept_entry(e) }.
    sort_by(&:last).
    map(&:first)
  entries.map { |e| find_content_type_for_media_range(e) }.compact
end

#serialize(env, status, headers, body) ⇒ Array(Integer, Hash, #each)

Serializes objects as JSON-LD. Defaults to expanded form, other forms determined by presense of profile in accept-parms.

Parameters:

  • env (Hash{String => String})
  • status (Integer)
  • headers (Hash{String => Object})
  • body (RDF::Enumerable)

Returns:

  • (Array(Integer, Hash, #each))

    Status, Headers and Body


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/json/ld/conneg.rb', line 70

def serialize(env, status, headers, body)
  # This will only return json-ld content types, possibly with parameters
  content_types = parse_accept_header(env['HTTP_ACCEPT'] || 'application/ld+json')
  content_types = content_types.select do |content_type|
    _, *params = content_type.split(';').map(&:strip)
    accept_params = params.inject({}) do |memo, pv|
      p, v = pv.split('=').map(&:strip)
      memo.merge(p.downcase.to_sym => v.sub(/^["']?([^"']*)["']?$/, '\1'))
    end
    JSON::LD::Writer.accept?(accept_params)
  end
  if content_types.empty?
    not_acceptable("No appropriate combinaion of media-type and parameters found")
  else
    ct, *params = content_types.first.split(';').map(&:strip)
    accept_params = params.inject({}) do |memo, pv|
      p, v = pv.split('=').map(&:strip)
      memo.merge(p.downcase.to_sym => v.sub(/^["']?([^"']*)["']?$/, '\1'))
    end

    # Determine API method from profile
    profile = accept_params[:profile].to_s.split(' ')

    # Get context from Link header
    links = LinkHeader.parse(env['HTTP_LINK'])
    context = links.find_link(['rel', JSON_LD_NS+"context"]).href rescue nil
    frame = links.find_link(['rel', JSON_LD_NS+"frame"]).href rescue nil

    if profile.include?(JSON_LD_NS+"framed") && frame.nil?
      return not_acceptable("framed profile without a frame")
    end

    # accept? already determined that there are appropriate contexts
    # If profile also includes a URI which is not a namespace, use it for compaction.
    context ||= Writer.default_context if profile.include?(JSON_LD_NS+"compacted")

    result = if profile.include?(JSON_LD_NS+"flattened")
      API.flatten(body, context)
    elsif profile.include?(JSON_LD_NS+"framed")
      API.frame(body, frame)
    elsif context
      API.compact(body, context)
    elsif profile.include?(JSON_LD_NS+"expanded")
      API.expand(body)
    else
      body
    end

    headers = headers.merge(VARY).merge('Content-Type' => ct)
    [status, headers, [result.to_json]]
  end
rescue
  http_error(500, $!.message)
end