module LineMessageCreator

Constants

VERSION

Attributes

helper_dir[RW]

@param [Pathname] line_message_dir Your line_message dir @param [Pathname] helper_dir Your helper dir

line_message_dir[RW]

@param [Pathname] line_message_dir Your line_message dir @param [Pathname] helper_dir Your helper dir

Public Class Methods

create_from(*file_names, **locals) click to toggle source

@param [Array] file_names File name to read (Exclude extension). @param [Hash] locals Local variable used in erb file. @return [Array] [ { type: 'text', text: 'message', quickReply: '…' }, … ]

# File lib/line_message_creator.rb, line 16
def create_from(*file_names, **locals)
  check_initial_settings

  messages      = file_names.map  { |file_name| read_message(file_name, **locals) }
  text_messages = messages.select { |message| message.is_a?(String) }
  quick_reply   = messages.select { |message| message.is_a?(Hash) }.last

  check_text_message(text_messages)

  if quick_reply
    text_messages.map { |text_message| { type: 'text', text: text_message, quickReply: quick_reply } }
  else
    text_messages.map { |text_message| { type: 'text', text: text_message } }
  end
end

Private Class Methods

check_initial_settings() click to toggle source
# File lib/line_message_creator.rb, line 112
def check_initial_settings
  if line_message_dir.nil?
    raise(TypeError, "Set LineMessageCreator.line_message_dir= e.g. Rails.root.join('app/line_messages').")
  end

  if helper_dir.nil?
    raise(TypeError, "Set LineMessageCreator.helper_dir= e.g. Rails.root.join('app/line_messages/helpers').")
  end
end
check_text_message(text_messages) click to toggle source
# File lib/line_message_creator.rb, line 122
def check_text_message(text_messages)
  if text_messages.empty?
    raise(ArgumentError, 'Specify a text message file.')
  end
end
find_helper_file(helper_name) click to toggle source
# File lib/line_message_creator.rb, line 52
def find_helper_file(helper_name)
  search_path = helper_dir.join("**/#{helper_name}.rb")
  Dir[search_path].first
end
find_helper_module(file_path_str) click to toggle source
# File lib/line_message_creator.rb, line 76
def find_helper_module(file_path_str)
  retry_counter = 0
  helper_name   = file_path_str.split('/').last.split('.').first + '_helper'
  return unless find_helper_file(helper_name)

  begin
    to_const(helper_name)
  rescue NameError
    retry_counter += 1
    if retry_counter == 1
      require_helper(helper_name) && retry
    else
      false
    end
  end
end
find_message_file(file_name) click to toggle source
# File lib/line_message_creator.rb, line 47
def find_message_file(file_name)
  search_path = line_message_dir.join("**/#{file_name}.*")
  Dir[search_path].first || raise(LoadError, "No such file '#{search_path}' .")
end
read_erb_message(file_path_str, **locals) click to toggle source
# File lib/line_message_creator.rb, line 61
def read_erb_message(file_path_str, **locals)
  erb_file  = File.open(file_path_str)
  variables = {}

  if (helper_module = find_helper_module(file_path_str))
    extend helper_module
    method_names = helper_module.public_instance_methods(false)
    variables    = method_names.map { |method_name| [method_name, send(method_name)] }.to_h
  end

  ERB.new(erb_file.read)
    .result_with_hash(**variables, **locals)
    .gsub(/^\s+/, '')
end
read_message(file_name, **locals) click to toggle source
# File lib/line_message_creator.rb, line 34
def read_message(file_name, **locals)
  file_path_str = find_message_file(file_name)

  case File.extname(file_path_str)
  when '.txt'
    read_text_message(file_path_str)
  when '.erb'
    read_erb_message(file_path_str, **locals)
  when '.json'
    read_quick_reply(file_path_str)
  end
end
read_quick_reply(file_path_str) click to toggle source
# File lib/line_message_creator.rb, line 107
def read_quick_reply(file_path_str)
  json_file = File.open(file_path_str)
  JSON.parse(json_file.read)
end
read_text_message(file_path_str) click to toggle source
# File lib/line_message_creator.rb, line 57
def read_text_message(file_path_str)
  File.open(file_path_str).read
end
require_helper(helper_name) click to toggle source
# File lib/line_message_creator.rb, line 93
def require_helper(helper_name)
  helper_path = find_helper_file(helper_name)
  require helper_path
end
to_camel(str) click to toggle source
# File lib/line_message_creator.rb, line 103
def to_camel(str)
  str.split(/_/).map(&:capitalize).join
end
to_const(str) click to toggle source
# File lib/line_message_creator.rb, line 98
def to_const(str)
  camel_str = to_camel(str)
  Object.const_get(camel_str)
end