Class: Bianchi::USSD::Page

Inherits:
Object
  • Object
show all
Includes:
PageDelegators
Defined in:
lib/bianchi/ussd/page.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PageDelegators

#delegate_redirected_to, #delegate_render_and, #delegate_session, #handle_redirect_to_cases, #method_missing, #page_number, #to_menu_page, #to_next_or_previous_page

Constructor Details

#initialize(session) ⇒ Page

Returns a new instance of Page.



10
11
12
# File 'lib/bianchi/ussd/page.rb', line 10

def initialize(session)
  @session = session
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Bianchi::USSD::PageDelegators

Instance Attribute Details

#sessionObject (readonly)

Returns the value of attribute session.



6
7
8
# File 'lib/bianchi/ussd/page.rb', line 6

def session
  @session
end

Class Method Details

.call(session, action) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/bianchi/ussd/page.rb', line 14

def self.call(session, action)
  new(session).tap do |p|
    p.ensure_methods_defined(%i[request response])
    p.send(action)
  rescue DispatchRenderException
    p
  end
end

Instance Method Details

#ensure_methods_defined(methods) ⇒ Object

Raises:



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bianchi/ussd/page.rb', line 54

def ensure_methods_defined(methods)
  error_messages = methods.each_with_object([]) do |method, messages|
    next if methods_defined? method

    messages << "#{self.class.name} is supposed to have method #{method} defined"
  end

  return if error_messages.empty?

  raise PageLoadError, error_messages.join(", ")
end

#load_page(page_number, menu_name) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/bianchi/ussd/page.rb', line 38

def load_page(page_number, menu_name)
  constant_name = "USSD::#{menu_name.camelize}Menu::#{page_number}"
  page = constant_name.safe_constantize

  unless page
    raise PageLoadError, <<~MSG
      \n
      #{constant_name} is supposed to be defined to process #{menu_name} menu #{page_number}.
      generate menu page with `bianchi g menu #{menu_name} page #{page_number[4..]}`
    MSG
  end

  session.menu = Menu.new(menu_name)
  page.new(session).send(:request)
end

#methods_defined?(method) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/bianchi/ussd/page.rb', line 66

def methods_defined?(method)
  respond_to? method
end

#render(body, options = {}) ⇒ Object

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bianchi/ussd/page.rb', line 23

def render(body, options = {})
  raise ArgumentError, "render body expected to be a string" unless body.is_a? String

  session.tap do |s|
    s.prompt_data = s.params.merge(
      { activity_state: options[:state], body: body }
    )

    s.page_number = self.class.name.split("::").last
    s.store.track_session
  end

  raise DispatchRenderException
end