Class: Lurker::ServicePresenter

Inherits:
BasePresenter show all
Extended by:
Forwardable
Defined in:
lib/lurker/presenters/service_presenter.rb

Overview

An BasePresenter for Lurker::Service

Instance Attribute Summary collapse

Attributes inherited from BasePresenter

#options

Instance Method Summary collapse

Methods inherited from BasePresenter

#css_path, #get_binding, #html_directory, #index_path, #render, #render_erb, #tag_with_anchor, #url_base_path

Constructor Details

#initialize(service, options = {}, &block) ⇒ ServicePresenter

Returns a new instance of ServicePresenter.



8
9
10
11
12
# File 'lib/lurker/presenters/service_presenter.rb', line 8

def initialize(service, options = {}, &block)
  super(options)
  @service = service
  @filtering_block = block
end

Instance Attribute Details

#serviceObject (readonly)

Returns the value of attribute service.



3
4
5
# File 'lib/lurker/presenters/service_presenter.rb', line 3

def service
  @service
end

Instance Method Details

#default_domainObject



29
30
31
32
# File 'lib/lurker/presenters/service_presenter.rb', line 29

def default_domain
  return service_domains.to_a[0][1] if service_domains.present?
  '/'
end

#default_request_media_typeObject



39
40
41
# File 'lib/lurker/presenters/service_presenter.rb', line 39

def default_request_media_type
  request_media_types[0]
end

#domainsObject



24
25
26
27
# File 'lib/lurker/presenters/service_presenter.rb', line 24

def domains
  return service_domains if service_domains.present?
  { '/' => 'Local' }
end

#endpointsObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/lurker/presenters/service_presenter.rb', line 56

def endpoints
  unless @endpoints
    @endpoints = []
    prefix = nil

    service.endpoints.sort_by(&:endpoint_path).each do |endpoint|
      presenter = Lurker::EndpointPresenter.new(endpoint, options)
      presenter.service_presenter = self

      if @filtering_block
        presenter = @filtering_block.call(presenter)
        next if presenter.nil?
      end

      current_prefix = presenter.prefix

      @endpoints << [] if prefix != current_prefix
      @endpoints.last << presenter

      prefix = current_prefix
    end
  end

  @endpoints
end

#endpoints_by_prefixObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/lurker/presenters/service_presenter.rb', line 82

def endpoints_by_prefix
  @endpoints_by_prefix ||= begin
    hash = Hash.new { |h, k| h[k] = Array.new }
    service.endpoints.sort_by(&:endpoint_path).each do |endpoint|
      presenter = Lurker::EndpointPresenter.new(endpoint, options)
      presenter.service_presenter = self

      if @filtering_block
        presenter = @filtering_block.call(presenter)
        next if presenter.nil?
      end

      hash[presenter.prefix] << presenter
    end
    hash
  end
end


43
44
45
46
# File 'lib/lurker/presenters/service_presenter.rb', line 43

def name_as_link(options = {})
  path = index_path
  '<a href="%s">%s %s</a>' % [path, options[:prefix], service.name]
end

#request_media_typesObject



34
35
36
37
# File 'lib/lurker/presenters/service_presenter.rb', line 34

def request_media_types
  return service.request_media_types if service.request_media_types.present?
  ['application/x-www-form-urlencoded']
end

#slug_nameObject



48
49
50
# File 'lib/lurker/presenters/service_presenter.rb', line 48

def slug_name
  service.name.downcase.gsub(/[ \/]/, '_')
end

#titleObject



20
21
22
# File 'lib/lurker/presenters/service_presenter.rb', line 20

def title
  "#{name}"
end

#to_html(&block) ⇒ Object

TODO move to controller



15
16
17
18
# File 'lib/lurker/presenters/service_presenter.rb', line 15

def to_html(&block)
  @service_presenter = self
  render('index')
end

#url(extension = ".html") ⇒ Object



52
53
54
# File 'lib/lurker/presenters/service_presenter.rb', line 52

def url(extension = ".html")
  '%s-%s%s' % [@endpoint.path, @endpoint.verb, extension]
end