adhearsion 2.0.0.alpha2 → 2.0.0.alpha3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. data/.gitignore +16 -14
  2. data/CHANGELOG.md +17 -1
  3. data/adhearsion.gemspec +14 -13
  4. data/features/cli_basic.feature +30 -0
  5. data/features/cli_create.feature +24 -0
  6. data/features/cli_daemon.feature +21 -0
  7. data/features/cli_generate.feature +9 -0
  8. data/features/cli_start.feature +33 -0
  9. data/features/cli_stop.feature +38 -0
  10. data/features/controller_generator.feature +19 -0
  11. data/features/plugin_generator.feature +55 -0
  12. data/lib/adhearsion.rb +5 -1
  13. data/lib/adhearsion/call.rb +57 -51
  14. data/lib/adhearsion/call_controller.rb +4 -20
  15. data/lib/adhearsion/call_controller/dial.rb +34 -4
  16. data/lib/adhearsion/calls.rb +4 -1
  17. data/lib/adhearsion/cli_commands.rb +31 -6
  18. data/lib/adhearsion/configuration.rb +2 -6
  19. data/lib/adhearsion/console.rb +56 -17
  20. data/lib/adhearsion/generators.rb +53 -2
  21. data/lib/adhearsion/generators/app/app_generator.rb +2 -24
  22. data/lib/adhearsion/generators/app/templates/config/adhearsion.rb +1 -1
  23. data/lib/adhearsion/generators/controller/controller_generator.rb +18 -0
  24. data/lib/adhearsion/generators/controller/templates/lib/controller.rb +4 -0
  25. data/lib/adhearsion/generators/controller/templates/spec/controller_spec.rb +3 -0
  26. data/lib/adhearsion/generators/generator.rb +77 -0
  27. data/lib/adhearsion/generators/plugin/plugin_generator.rb +33 -0
  28. data/lib/adhearsion/generators/plugin/templates/.gitignore +9 -0
  29. data/lib/adhearsion/generators/plugin/templates/Gemfile.tt +4 -0
  30. data/lib/adhearsion/generators/plugin/templates/README.md.tt +2 -0
  31. data/lib/adhearsion/generators/plugin/templates/Rakefile.tt +1 -0
  32. data/lib/adhearsion/generators/plugin/templates/lib/plugin-template.rb.tt +5 -0
  33. data/lib/adhearsion/generators/plugin/templates/lib/plugin-template/controller_methods.rb.tt +10 -0
  34. data/lib/adhearsion/generators/plugin/templates/lib/plugin-template/plugin.rb.tt +29 -0
  35. data/lib/adhearsion/generators/plugin/templates/lib/plugin-template/version.rb.tt +3 -0
  36. data/lib/adhearsion/generators/plugin/templates/plugin-template.gemspec.tt +35 -0
  37. data/lib/adhearsion/generators/plugin/templates/spec/plugin-template/controller_methods_spec.rb.tt +26 -0
  38. data/lib/adhearsion/generators/plugin/templates/spec/spec_helper.rb.tt +13 -0
  39. data/lib/adhearsion/initializer.rb +14 -22
  40. data/lib/adhearsion/logging.rb +25 -16
  41. data/lib/adhearsion/outbound_call.rb +3 -3
  42. data/lib/adhearsion/plugin.rb +14 -0
  43. data/lib/adhearsion/punchblock_plugin.rb +6 -1
  44. data/lib/adhearsion/punchblock_plugin/initializer.rb +8 -8
  45. data/lib/adhearsion/tasks/configuration.rb +1 -1
  46. data/lib/adhearsion/version.rb +1 -1
  47. data/spec/adhearsion/call_controller/dial_spec.rb +108 -17
  48. data/spec/adhearsion/call_controller_spec.rb +7 -64
  49. data/spec/adhearsion/call_spec.rb +48 -29
  50. data/spec/adhearsion/calls_spec.rb +7 -0
  51. data/spec/adhearsion/configuration_spec.rb +14 -14
  52. data/spec/adhearsion/console_spec.rb +124 -4
  53. data/spec/adhearsion/generators_spec.rb +17 -0
  54. data/spec/adhearsion/initializer_spec.rb +22 -18
  55. data/spec/adhearsion/logging_spec.rb +78 -48
  56. data/spec/adhearsion/outbound_call_spec.rb +6 -15
  57. data/spec/adhearsion/plugin_spec.rb +18 -0
  58. data/spec/adhearsion/process_spec.rb +10 -1
  59. data/spec/adhearsion/punchblock_plugin/initializer_spec.rb +8 -6
  60. data/spec/spec_helper.rb +5 -2
  61. data/spec/support/call_controller_test_helpers.rb +1 -1
  62. data/spec/support/initializer_stubs.rb +1 -1
  63. metadata +106 -66
  64. data/features/cli.feature +0 -108
  65. data/lib/adhearsion/initializer/logging.rb +0 -33
  66. data/spec/adhearsion/initializer/logging_spec.rb +0 -55
@@ -0,0 +1,4 @@
1
+ class <%= @controller_name %> < Adhearsion::CallController
2
+ def run
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ describe <%= @controller_name %> do
2
+
3
+ end
@@ -0,0 +1,77 @@
1
+ begin
2
+ require 'thor/group'
3
+ rescue LoadError
4
+ puts "Thor is not available.\nIf you ran this command from a git checkout " \
5
+ "of Adhearsion, please make sure thor is installed,\nand run this command " \
6
+ "as `ruby #{$0} #{(ARGV | ['--dev']).join(" ")}`"
7
+ exit
8
+ end
9
+
10
+ module Adhearsion
11
+ module Generators
12
+
13
+ class Generator < Thor::Group
14
+ include Thor::Actions
15
+
16
+ argument :generate_command, :type => :string
17
+ argument :generator_name, :type => :string
18
+
19
+ # Returns the source root for this generator using default_source_root as default.
20
+ def self.source_root(path = nil)
21
+ @_source_root = path if path
22
+ @_source_root ||= default_source_root
23
+ end
24
+
25
+ # Tries to get the description from a USAGE file one folder above the source
26
+ # root otherwise uses a default description.
27
+ def self.desc(description = nil)
28
+ return super if description
29
+ usage = source_root && File.expand_path("../USAGE", source_root)
30
+
31
+ @desc ||= if usage && File.exist?(usage)
32
+ ERB.new(File.read(usage)).result(binding)
33
+ else
34
+ "Description:\n Create #{base_name.humanize.downcase} files for #{generator_name} generator."
35
+ end
36
+ end
37
+
38
+ # Convenience method to get the namespace from the class name. It's the
39
+ # same as Thor default except that the Generator at the end of the class
40
+ # is removed.
41
+ def self.namespace(name = nil)
42
+ return super if name
43
+ @namespace ||= super.sub(/_generator$/, '').sub(/:generators:/, ':')
44
+ end
45
+
46
+ # Returns the default source root for a given generator. This is used internally
47
+ # by adhearsion to set its generators source root. If you want to customize your source
48
+ # root, you should use source_root.
49
+ def self.default_source_root
50
+ return unless generator_name
51
+ path = File.expand_path File.join(generator_name, 'templates'), base_root
52
+ path if File.exists?(path)
53
+ end
54
+
55
+ # Returns the base root for a common set of generators. This is used to dynamically
56
+ # guess the default source root.
57
+ def self.base_root
58
+ File.dirname __FILE__
59
+ end
60
+
61
+ protected
62
+
63
+ # Removes the namespaces and get the generator name. For example,
64
+ # Adhearsion::Generators::ModelGenerator will return "model" as generator name.
65
+ #
66
+ def self.generator_name
67
+ @generator_name ||= begin
68
+ if generator = name.to_s.split('::').last
69
+ generator.sub! /Generator$/, ''
70
+ generator.underscore
71
+ end
72
+ end
73
+ end
74
+
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,33 @@
1
+ module Adhearsion
2
+ module Generators
3
+ class PluginGenerator < Generator
4
+
5
+ argument :plugin_name, :type => :string
6
+
7
+
8
+ def create_plugin
9
+ @plugin_file = @plugin_name.underscore
10
+ self.destination_root = '.'
11
+
12
+ empty_directory @plugin_file
13
+ empty_directory "#{@plugin_file}/lib"
14
+ empty_directory "#{@plugin_file}/lib/#{@plugin_file}"
15
+ empty_directory "#{@plugin_file}/spec"
16
+
17
+ template 'plugin-template.gemspec.tt', "#{@plugin_file}/#{@plugin_file}.gemspec"
18
+ template 'Rakefile.tt', "#{@plugin_file}/Rakefile"
19
+ template 'README.md.tt', "#{@plugin_file}/README.md"
20
+ template 'Gemfile.tt', "#{@plugin_file}/Gemfile"
21
+
22
+ template 'lib/plugin-template.rb.tt', "#{@plugin_file}/lib/#{@plugin_file}.rb"
23
+ template 'lib/plugin-template/version.rb.tt', "#{@plugin_file}/lib/#{@plugin_file}/version.rb"
24
+ template 'lib/plugin-template/plugin.rb.tt', "#{@plugin_file}/lib/#{@plugin_file}/plugin.rb"
25
+ template 'lib/plugin-template/controller_methods.rb.tt', "#{@plugin_file}/lib/#{@plugin_file}/controller_methods.rb"
26
+
27
+ template 'spec/spec_helper.rb.tt', "#{@plugin_file}/spec/spec_helper.rb"
28
+ template 'spec/plugin-template/controller_methods_spec.rb.tt', "#{@plugin_file}/spec/#{@plugin_file}/controller_methods_spec.rb"
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,9 @@
1
+ *.swp
2
+ *.swo
3
+ Gemfile.lock
4
+ coverage/
5
+ vendor/
6
+ .bundle/
7
+ .DS_Store
8
+ pkg/*
9
+ *.gem
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in plugin-template.gemspec
4
+ gemspec
@@ -0,0 +1,2 @@
1
+ <%= @plugin_name %>
2
+ ==========================
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,5 @@
1
+ module <%= @plugin_name %>
2
+ require "<%= @plugin_file %>/version"
3
+ require "<%= @plugin_file %>/plugin"
4
+ require "<%= @plugin_file %>/controller_methods"
5
+ end
@@ -0,0 +1,10 @@
1
+ module <%= @plugin_name %>
2
+ module ControllerMethods
3
+ # The methods are defined in a normal method the user will then mix in their CallControllers
4
+ # The following also contains an example of configuration usage
5
+ #
6
+ def greet(name)
7
+ play "#{Adhearsion.config[:<%= @plugin_file %>].greeting}, #{name}"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,29 @@
1
+ module <%= @plugin_name %>
2
+ class Plugin < Adhearsion::Plugin
3
+ # Actions to perform when the plugin is loaded
4
+ #
5
+ init :<%= @plugin_file %> do
6
+ logger.warn "<%= @plugin_name %> has been loaded"
7
+ end
8
+
9
+ # Basic configuration for the plugin
10
+ #
11
+ config :<%= @plugin_file %> do
12
+ greeting "Hello", :desc => "What to use to greet users"
13
+ end
14
+
15
+ # Defining a Rake task is easy
16
+ # The following can be invoked with:
17
+ # rake plugin_demo:info
18
+ #
19
+ tasks do
20
+ namespace :<%= @plugin_file %> do
21
+ desc "Prints the PluginTemplate information"
22
+ task :info do
23
+ STDOUT.puts "<%= @plugin_name %> plugin v. #{VERSION}"
24
+ end
25
+ end
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module <%= @plugin_name %>
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,35 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "<%= @plugin_name.underscore %>/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "<%= @plugin_name.underscore %>"
7
+ s.version = <%= @plugin_name %>::VERSION
8
+ s.authors = ["Plugin Author"]
9
+ s.email = ["author@plugin.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{TODO: Write a gem summary}
12
+ s.description = %q{TODO: Write a gem description}
13
+
14
+ s.rubyforge_project = "<%= @plugin_name.underscore %>"
15
+
16
+ # Use the following if using Git
17
+ # s.files = `git ls-files`.split("\n")
18
+ # s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.files = Dir.glob("{lib}/**/*") + %w( README.md Rakefile Gemfile)
20
+ s.test_files = Dir.glob("{spec}/**/*")
21
+ s.require_paths = ["lib"]
22
+
23
+ s.add_runtime_dependency %q<adhearsion>, [">= <%= Adhearsion::VERSION %>"]
24
+ s.add_runtime_dependency %q<activesupport>, [">= 3.0.10"]
25
+
26
+ s.add_development_dependency %q<bundler>, ["~> 1.0.0"]
27
+ s.add_development_dependency %q<rspec>, [">= 2.5.0"]
28
+ s.add_development_dependency %q<ci_reporter>, [">= 1.6.3"]
29
+ s.add_development_dependency %q<simplecov>, [">= 0"]
30
+ s.add_development_dependency %q<simplecov-rcov>, [">= 0"]
31
+ s.add_development_dependency %q<yard>, ["~> 0.6.0"]
32
+ s.add_development_dependency %q<rake>, [">= 0"]
33
+ s.add_development_dependency %q<mocha>, [">= 0"]
34
+ s.add_development_dependency %q<guard-rspec>
35
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ module <%= @plugin_name %>
4
+ describe ControllerMethods do
5
+ describe "mixed in to a CallController" do
6
+
7
+ class TestController < Adhearsion::CallController
8
+ include <%= @plugin_name %>::ControllerMethods
9
+ end
10
+
11
+ let(:mock_call) { mock 'Call' }
12
+
13
+ subject do
14
+ TestController.new mock_call
15
+ end
16
+
17
+ describe "#greet" do
18
+ it "greets with the correct parameter" do
19
+ subject.expects(:play).once.with("Hello, Luca")
20
+ subject.greet "Luca"
21
+ end
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,13 @@
1
+ require 'adhearsion'
2
+ require 'mocha'
3
+ require '<%= @plugin_file %>'
4
+
5
+ RSpec.configure do |config|
6
+ config.color_enabled = true
7
+ config.tty = true
8
+
9
+ config.mock_with :mocha
10
+ config.filter_run :focus => true
11
+ config.run_all_when_everything_filtered = true
12
+ end
13
+
@@ -4,16 +4,14 @@ require 'adhearsion/linux_proc_name'
4
4
  module Adhearsion
5
5
  class Initializer
6
6
 
7
- extend ActiveSupport::Autoload
8
-
9
- autoload :Logging
10
-
11
7
  class << self
12
8
  def start(*args, &block)
13
9
  new(*args, &block).start
14
10
  end
15
11
  end
16
12
 
13
+ DEFAULT_PID_FILE_NAME = 'adhearsion.pid'
14
+
17
15
  attr_reader :path, :daemon, :pid_file
18
16
 
19
17
  # Creation of pid_files
@@ -47,17 +45,18 @@ module Adhearsion
47
45
  catch_termination_signal
48
46
  create_pid_file
49
47
  set_ahn_proc_name
50
- logger.info "Loaded config in <#{Adhearsion.config.platform.environment}> environment"
51
48
  initialize_exception_logger
52
49
  update_rails_env_var
53
50
  init_plugins
54
51
 
55
- logger.info "Adhearsion v#{Adhearsion::VERSION} initialized!"
56
- Adhearsion::Process.booted
57
-
58
52
  run_plugins
59
53
  trigger_after_initialized_hooks
60
54
 
55
+ if Adhearsion.status == :booting
56
+ Adhearsion::Process.booted
57
+ logger.info "Adhearsion v#{Adhearsion::VERSION} initialized with environment <#{Adhearsion.config.platform.environment}>!"
58
+ end
59
+
61
60
  # This method will block until all important threads have finished.
62
61
  # When it does, the process will exit.
63
62
  join_important_threads
@@ -67,10 +66,10 @@ module Adhearsion
67
66
  def update_rails_env_var
68
67
  env = ENV['AHN_ENV']
69
68
  if env && Adhearsion.config.valid_environment?(env.to_sym)
70
- if ENV['RAILS_ENV'] == env
71
- logger.info "Using the configured value for RAILS_ENV : <#{env}>"
69
+ if ENV['RAILS_ENV']
70
+ logger.info "Using provided RAILS_ENV value of <#{ENV['RAILS_ENV']}>"
72
71
  else
73
- logger.warn "Updating AHN_RAILS variable to <#{env}>"
72
+ logger.warn "Setting RAILS_ENV variable to <#{env}>"
74
73
  ENV['RAILS_ENV'] = env
75
74
  end
76
75
  else
@@ -79,7 +78,7 @@ module Adhearsion
79
78
  logger.info "Using the configured value for RAILS_ENV : <#{env}>"
80
79
  else
81
80
  env = Adhearsion.config.platform.environment.to_s
82
- logger.info "Defining AHN_RAILS variable to <#{env}>"
81
+ logger.info "Defining RAILS_ENV variable to <#{env}>"
83
82
  ENV['RAILS_ENV'] = env
84
83
  end
85
84
  end
@@ -87,7 +86,7 @@ module Adhearsion
87
86
  end
88
87
 
89
88
  def default_pid_path
90
- File.join Adhearsion.config.root, 'adhearsion.pid'
89
+ File.join Adhearsion.config.root, DEFAULT_PID_FILE_NAME
91
90
  end
92
91
 
93
92
  def resolve_pid_file_path
@@ -174,14 +173,7 @@ module Adhearsion
174
173
  if should_daemonize?
175
174
  appenders
176
175
  else
177
- stdout = ::Logging.appenders.stdout(
178
- 'stdout',
179
- :layout => ::Logging.layouts.pattern(
180
- :pattern => Adhearsion::Logging.adhearsion_pattern,
181
- :color_scheme => 'bright'
182
- )
183
- )
184
- appenders << stdout
176
+ appenders += Adhearsion::Logging.default_appenders
185
177
  end
186
178
  end
187
179
 
@@ -238,7 +230,7 @@ module Adhearsion
238
230
 
239
231
  def start_logging
240
232
  outputters = init_get_logging_appenders
241
- Logging.start outputters, Adhearsion.config.platform.logging.level, Adhearsion.config.platform.logging.formatter
233
+ Adhearsion::Logging.start outputters, Adhearsion.config.platform.logging.level, Adhearsion.config.platform.logging.formatter
242
234
  end
243
235
 
244
236
  def initialize_exception_logger
@@ -36,25 +36,40 @@ module Adhearsion
36
36
  ::Logging.reset
37
37
  end
38
38
 
39
- def start
39
+ def init
40
40
  ::Logging.init LOG_LEVELS
41
- ::Logging::Logger[:root].level = :info
42
- ::Logging.logger.root.appenders = [::Logging.appenders.stdout('stdout')]
43
- self.send :_set_formatter, ::Logging::Layouts.basic(:format_as => :string, :backtrace => true)
44
41
 
45
42
  LOG_LEVELS.each do |level|
46
43
  Adhearsion::Logging.const_defined?(level) or Adhearsion::Logging.const_set(level, ::Logging::LEVELS[::Logging.levelify(level)])
47
44
  end
48
45
  end
49
46
 
47
+ def start(_appenders = nil, level = :info, formatter = nil)
48
+ ::Logging.logger.root.appenders = _appenders.nil? ? default_appenders : _appenders
49
+
50
+ ::Logging.logger.root.level = level
51
+
52
+ formatter = formatter if formatter
53
+ end
54
+
55
+ def default_appenders
56
+ [::Logging.appenders.stdout(
57
+ 'stdout',
58
+ :layout => ::Logging.layouts.pattern(
59
+ :pattern => adhearsion_pattern,
60
+ :color_scheme => 'bright'
61
+ )
62
+ )]
63
+ end
64
+
50
65
  def logging_level=(new_logging_level)
51
- ::Logging::Logger[:root].level = new_logging_level
66
+ ::Logging.logger.root.level = new_logging_level
52
67
  end
53
68
 
54
69
  alias :level= :logging_level=
55
70
 
56
71
  def logging_level
57
- ::Logging::Logger[:root].level
72
+ ::Logging.logger.root.level
58
73
  end
59
74
 
60
75
  def get_logger(logger_name)
@@ -80,7 +95,9 @@ module Adhearsion
80
95
  alias :appenders :outputters
81
96
 
82
97
  def formatter=(formatter)
83
- _set_formatter(formatter)
98
+ ::Logging.logger.root.appenders.each do |appender|
99
+ appender.layout = formatter
100
+ end
84
101
  end
85
102
 
86
103
  alias :layout= :formatter=
@@ -91,16 +108,8 @@ module Adhearsion
91
108
 
92
109
  alias :layout :formatter
93
110
 
94
- private
95
-
96
- def _set_formatter(formatter)
97
- ::Logging.logger.root.appenders.each do |appender|
98
- appender.layout = formatter
99
- end
100
- end
101
-
102
111
  end
103
112
 
104
- start unless ::Logging.const_defined? :MAX_LEVEL_LENGTH
113
+ init unless ::Logging.const_defined? :MAX_LEVEL_LENGTH
105
114
  end
106
115
  end