Class: Murlsh::JsonBody

Inherits:
Object
  • Object
show all
Defined in:
lib/murlsh/json_body.rb

Overview

Recent urls json response builder.

Instance Method Summary collapse

Constructor Details

#initialize(config, req, result_set) ⇒ JsonBody

Returns a new instance of JsonBody.



8
9
10
# File 'lib/murlsh/json_body.rb', line 8

def initialize(config, req, result_set)
  @config, @req, @result_set = config, req, result_set
end

Instance Method Details

#buildObject

Recent urls json response builder.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/murlsh/json_body.rb', line 16

def build
  if defined?(@body)
    @body
  else
    urls = @result_set.results.map do |mu|
      Murlsh::Plugin.hooks('url_display_pre') do |p|
        p.run mu, @req, @config
      end

      h = mu.attributes

      h['title'] = mu.title_stripped

      # add site root url to relative thumbnail urls
      if h['thumbnail_url'] and
        not URI(h['thumbnail_url']).scheme.to_s.downcase[/https?/]
        h['thumbnail_url'] = URI.join(@config.fetch('root_url'),
          h['thumbnail_url']).to_s
      end

      h['enclosures'] = mu.enclosures.map do |e|
        new_e = e.attributes.reject { |k,v| k == 'url_id' }
        new_e['title'] = e.title_stripped
        new_e
      end

      h
    end
    @body = urls.to_json
  end
end

#each {|build| ... } ⇒ Object

Yield body for Rack.

Yields:



13
# File 'lib/murlsh/json_body.rb', line 13

def each; yield build; end