asterisk-ari-client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (100) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +85 -0
  6. data/Rakefile +54 -0
  7. data/asterisk-ari-client.gemspec +32 -0
  8. data/examples/list_channels.rb +14 -0
  9. data/examples/websocket.rb +33 -0
  10. data/lib/ari.rb +12 -0
  11. data/lib/ari/client.rb +124 -0
  12. data/lib/ari/generators/api.rb +23 -0
  13. data/lib/ari/generators/attribute.rb +35 -0
  14. data/lib/ari/generators/model.rb +38 -0
  15. data/lib/ari/generators/operation.rb +43 -0
  16. data/lib/ari/generators/parameter.rb +35 -0
  17. data/lib/ari/generators/property.rb +32 -0
  18. data/lib/ari/generators/resource_generator.rb +94 -0
  19. data/lib/ari/generators/templates/model.rb.erb +36 -0
  20. data/lib/ari/generators/templates/resource.rb.erb +97 -0
  21. data/lib/ari/list_resource.rb +23 -0
  22. data/lib/ari/model.rb +26 -0
  23. data/lib/ari/models.rb +47 -0
  24. data/lib/ari/models/.DS_Store +0 -0
  25. data/lib/ari/models/application_replaced.rb +17 -0
  26. data/lib/ari/models/asterisk_info.rb +35 -0
  27. data/lib/ari/models/bridge_attended_transfer.rb +63 -0
  28. data/lib/ari/models/bridge_blind_transfer.rb +35 -0
  29. data/lib/ari/models/bridge_created.rb +23 -0
  30. data/lib/ari/models/bridge_destroyed.rb +23 -0
  31. data/lib/ari/models/bridge_merged.rb +27 -0
  32. data/lib/ari/models/build_info.rb +19 -0
  33. data/lib/ari/models/caller_id.rb +19 -0
  34. data/lib/ari/models/channel_caller_id.rb +23 -0
  35. data/lib/ari/models/channel_created.rb +23 -0
  36. data/lib/ari/models/channel_destroyed.rb +23 -0
  37. data/lib/ari/models/channel_dialplan.rb +23 -0
  38. data/lib/ari/models/channel_dtmf_received.rb +23 -0
  39. data/lib/ari/models/channel_entered_bridge.rb +27 -0
  40. data/lib/ari/models/channel_hangup_request.rb +23 -0
  41. data/lib/ari/models/channel_left_bridge.rb +27 -0
  42. data/lib/ari/models/channel_state_change.rb +23 -0
  43. data/lib/ari/models/channel_talking_finished.rb +23 -0
  44. data/lib/ari/models/channel_talking_started.rb +23 -0
  45. data/lib/ari/models/channel_userevent.rb +35 -0
  46. data/lib/ari/models/channel_varset.rb +23 -0
  47. data/lib/ari/models/config_info.rb +27 -0
  48. data/lib/ari/models/device_state_changed.rb +23 -0
  49. data/lib/ari/models/dial.rb +31 -0
  50. data/lib/ari/models/dialed.rb +17 -0
  51. data/lib/ari/models/dialplan_cep.rb +19 -0
  52. data/lib/ari/models/endpoint_state_change.rb +23 -0
  53. data/lib/ari/models/format_lang_pair.rb +19 -0
  54. data/lib/ari/models/live_recording.rb +19 -0
  55. data/lib/ari/models/message.rb +19 -0
  56. data/lib/ari/models/missing_params.rb +19 -0
  57. data/lib/ari/models/playback_finished.rb +23 -0
  58. data/lib/ari/models/playback_started.rb +23 -0
  59. data/lib/ari/models/recording_failed.rb +23 -0
  60. data/lib/ari/models/recording_finished.rb +23 -0
  61. data/lib/ari/models/recording_started.rb +23 -0
  62. data/lib/ari/models/set_id.rb +19 -0
  63. data/lib/ari/models/stasis_end.rb +23 -0
  64. data/lib/ari/models/stasis_start.rb +27 -0
  65. data/lib/ari/models/status_info.rb +27 -0
  66. data/lib/ari/models/stored_recording.rb +19 -0
  67. data/lib/ari/models/system_info.rb +19 -0
  68. data/lib/ari/models/text_message.rb +23 -0
  69. data/lib/ari/models/text_message_received.rb +27 -0
  70. data/lib/ari/models/text_message_variable.rb +19 -0
  71. data/lib/ari/models/variable.rb +19 -0
  72. data/lib/ari/request_error.rb +8 -0
  73. data/lib/ari/resource.rb +37 -0
  74. data/lib/ari/resources.rb +11 -0
  75. data/lib/ari/resources/application.rb +101 -0
  76. data/lib/ari/resources/asterisk.rb +72 -0
  77. data/lib/ari/resources/bridge.rb +286 -0
  78. data/lib/ari/resources/channel.rb +610 -0
  79. data/lib/ari/resources/device_state.rb +98 -0
  80. data/lib/ari/resources/endpoint.rb +124 -0
  81. data/lib/ari/resources/event.rb +25 -0
  82. data/lib/ari/resources/mailbox.rb +99 -0
  83. data/lib/ari/resources/playback.rb +86 -0
  84. data/lib/ari/resources/recording.rb +241 -0
  85. data/lib/ari/resources/sound.rb +64 -0
  86. data/lib/ari/server_error.rb +8 -0
  87. data/lib/asterisk/ari/client.rb +10 -0
  88. data/lib/asterisk/ari/client/version.rb +7 -0
  89. data/test/fixtures/bridge_create.json +74 -0
  90. data/test/fixtures/bridge_get.json +74 -0
  91. data/test/fixtures/bridges_list.json +76 -0
  92. data/test/fixtures/channel_get.json +83 -0
  93. data/test/fixtures/channel_originate.json +83 -0
  94. data/test/fixtures/channel_originate_with_id.json +83 -0
  95. data/test/fixtures/channels_list.json +105 -0
  96. data/test/lib/asterisk-ari-client/.DS_Store +0 -0
  97. data/test/lib/asterisk-ari-client/bridge_test.rb +46 -0
  98. data/test/lib/asterisk-ari-client/channel_test.rb +59 -0
  99. data/test/test_helper.rb +42 -0
  100. metadata +265 -0
@@ -0,0 +1,38 @@
1
+ module Ari
2
+ module Generators
3
+ class Model
4
+
5
+ def initialize(klass_name, resource, specification)
6
+ @klass_name = klass_name
7
+ @resource = resource
8
+ @specification = specification
9
+ end
10
+
11
+ def name
12
+ klass_name.underscore
13
+ end
14
+
15
+ def klass_name
16
+ @klass_name
17
+ end
18
+
19
+ def description
20
+ @specification['description']
21
+ end
22
+
23
+ def properties
24
+ @properties ||= @specification['properties'].map { |name, options| Property.new(name, options) }
25
+ end
26
+
27
+ def sub_types
28
+ @specification['subTypes'] || []
29
+ end
30
+
31
+ def inherits_from
32
+ inherits_from_model = @resource.models.detect { |m| m.sub_types.include?(klass_name) }
33
+ inherits_from_model ? inherits_from_model.klass_name : 'Model'
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,43 @@
1
+ module Ari
2
+ module Generators
3
+ class Operation
4
+
5
+ def initialize(specification)
6
+ @specification = specification
7
+ end
8
+
9
+ def http_method
10
+ @specification['httpMethod']
11
+ end
12
+
13
+ def description
14
+ @specification['summary']
15
+ end
16
+
17
+ def method_name
18
+ @specification['nickname'].underscore
19
+ end
20
+
21
+ def original_method_name
22
+ @specification['nickname']
23
+ end
24
+
25
+ def return_klass
26
+ if @specification['responseClass'].start_with?('List[')
27
+ @specification['responseClass'][5..-2]
28
+ else
29
+ @specification['responseClass'] == 'void' ? nil : @specification['responseClass']
30
+ end
31
+ end
32
+
33
+ def returns_array?
34
+ @specification['responseClass'].start_with?('List[')
35
+ end
36
+
37
+ def parameters
38
+ @parameters ||= @specification['parameters'].map { |p| Parameter.new(p) } rescue []
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,35 @@
1
+ module Ari
2
+ module Generators
3
+ class Parameter
4
+
5
+ def initialize(specification)
6
+ @specification = specification
7
+ end
8
+
9
+ def name
10
+ @specification['name']
11
+ end
12
+
13
+ def description
14
+ @specification['description']
15
+ end
16
+
17
+ def location
18
+ @specification['paramType']
19
+ end
20
+
21
+ def required?
22
+ @specification['required'] || location == 'path'
23
+ end
24
+
25
+ def multiple?
26
+ @specification['allowMultiple']
27
+ end
28
+
29
+ def type
30
+ @specification['dataType']
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,32 @@
1
+ module Ari
2
+ module Generators
3
+ class Property
4
+
5
+ def initialize(name, specification)
6
+ @name = name
7
+ @specification = specification
8
+ end
9
+
10
+ def name
11
+ @name
12
+ end
13
+
14
+ def required?
15
+ @specification['required']
16
+ end
17
+
18
+ def type
19
+ if @specification['type'].start_with?('List[')
20
+ @specification['type'][5..-2]
21
+ else
22
+ @specification['type'] == 'void' ? nil : @specification['type']
23
+ end
24
+ end
25
+
26
+ def description
27
+ @specification['description']
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,94 @@
1
+ require 'erb'
2
+ require 'active_support'
3
+ require 'active_support/core_ext'
4
+ require 'ari/generators/api'
5
+ require 'ari/generators/attribute'
6
+ require 'ari/generators/model'
7
+ require 'ari/generators/operation'
8
+ require 'ari/generators/parameter'
9
+ require 'ari/generators/property'
10
+
11
+ module Ari
12
+ module Generators
13
+ class ResourceGenerator
14
+
15
+ def initialize(resource_name, specification, options = {})
16
+ @resource_name = resource_name.underscore
17
+ @klass_name = @resource_name.classify
18
+ @specification = specification
19
+ @options = options
20
+ end
21
+
22
+ def template_path(klass)
23
+ File.join(__dir__, 'templates', "#{klass}.rb.erb")
24
+ end
25
+
26
+ def destination_path(klass)
27
+ File.join(__dir__, '..', klass)
28
+ end
29
+
30
+ def generate_only_models?
31
+ @options.fetch(:only_models, false)
32
+ end
33
+
34
+ def generate
35
+ generate_resource
36
+ generate_models
37
+ end
38
+
39
+ def generate_resource
40
+ erb = ERB.new(IO.read(template_path('resource')), nil, '-')
41
+ File.open(File.join(destination_path('resources'), "#{resource_name}.rb"), 'w') do |f|
42
+ f.write erb.result(binding)
43
+ end
44
+ end
45
+
46
+ def generate_models
47
+ erb = ERB.new(IO.read(template_path('model')), nil, '-')
48
+ models.each do |model|
49
+ next if model.name == resource_name
50
+ File.open(File.join(destination_path('models'), "#{model.name}.rb"), 'w') do |f|
51
+ f.write erb.result(model.instance_eval { binding })
52
+ end
53
+ end
54
+ end
55
+
56
+ def klass_name
57
+ @klass_name
58
+ end
59
+
60
+ def resource_klass_name
61
+ options.fetch(:resource_klass_name, klass_name)
62
+ end
63
+
64
+ def resource_attributes
65
+ models.detect { |m| m.klass_name == klass_name }.properties rescue []
66
+ end
67
+
68
+ def resource_name
69
+ @resource_name.singularize
70
+ end
71
+
72
+ def resource_plural_name
73
+ @resource_name
74
+ end
75
+
76
+ def id_attribute_name
77
+ @options.fetch(:id_attribute_name, "#{klass_name.camelcase(:lower)}Id")
78
+ end
79
+
80
+ def apis
81
+ @apis ||= @specification['apis'].map { |api| Api.new(api) }
82
+ end
83
+
84
+ def models
85
+ @models ||= @specification['models'].map { |klass_name, options| Model.new(klass_name, self, options) }
86
+ end
87
+
88
+ def inherits_from
89
+ generate_only_models? ? 'Model' : 'Resource'
90
+ end
91
+
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,36 @@
1
+ #------------------------------------------------------------------------------
2
+ #
3
+ # WARNING !
4
+ #
5
+ # This is a generated file. DO NOT EDIT THIS FILE! Your changes will
6
+ # be lost the next time this file is regenerated.
7
+ #
8
+ # This file was generated using asterisk-ari-client ruby gem.
9
+ #
10
+ #------------------------------------------------------------------------------
11
+
12
+ module Ari
13
+ class <%= klass_name %> < <%= inherits_from %>
14
+
15
+ <%- if properties.present? -%>
16
+ attr_reader <%= properties.map { |a| ":#{a.name}" }.join(', ') %>
17
+
18
+ <%- end -%>
19
+ <%- properties.each do |attribute| -%>
20
+ <%- unless %{ string int long boolean }.include?(attribute.type) -%>
21
+ <%- attribute_value = begin
22
+ if attribute.type == 'Date'
23
+ 'Time.parse(val)'
24
+ else
25
+ attribute.type + '.new(val)'
26
+ end
27
+ end -%>
28
+ def <%= attribute.name %>=(val)
29
+ @<%= attribute.name %> ||= <%= attribute_value %>
30
+ end
31
+
32
+ <%- end -%>
33
+ <%- end -%>
34
+
35
+ end
36
+ end
@@ -0,0 +1,97 @@
1
+ #------------------------------------------------------------------------------
2
+ #
3
+ # WARNING !
4
+ #
5
+ # This is a generated file. DO NOT EDIT THIS FILE! Your changes will
6
+ # be lost the next time this file is regenerated.
7
+ #
8
+ # This file was generated using asterisk-ari-client ruby gem.
9
+ #
10
+ #------------------------------------------------------------------------------
11
+
12
+ module Ari
13
+ class <%= klass_name %> < <%= inherits_from %>
14
+
15
+ <%- if resource_attributes.present? -%>
16
+ attr_reader <%= resource_attributes.map { |a| ":#{a.name}" }.join(', ') %>
17
+
18
+ <%- end -%>
19
+ <%- resource_attributes.each do |attribute| -%>
20
+ <%- unless %{ string int long boolean }.include?(attribute.type) -%>
21
+ <%- attribute_value = begin
22
+ if attribute.type == 'Date'
23
+ 'Time.parse(val)'
24
+ else
25
+ attribute.type + '.new(val)'
26
+ end
27
+ end -%>
28
+ def <%= attribute.name %>=(val)
29
+ @<%= attribute.name %> ||= <%= attribute_value %>
30
+ end
31
+
32
+ <%- end -%>
33
+ <%- end -%>
34
+
35
+ <%- apis.each do |api| -%>
36
+ <%- api.operations.each do |operation| -%>
37
+ # <%= operation.http_method %> <%= api.path %>
38
+ #
39
+ # <%= api.description %>
40
+ #<% operation.description.scan(/.{80}/).each do |comment_line| %>
41
+ # <%= comment_line -%>
42
+ <%- end if operation.description %>
43
+ #
44
+ <%- if operation.parameters.present? -%>
45
+ # Parameters:
46
+ #
47
+ <%- operation.parameters.each do |parameter| -%>
48
+ # <%= parameter.name %> <%= '(required)' if parameter.required? %> - <%= parameter.description %>
49
+ <%- end -%>
50
+ #
51
+ <%- end -%>
52
+ def self.<%= operation.method_name %>(options = {})
53
+ <%- operation.parameters.each do |parameter| -%>
54
+ <%- next unless parameter.required? -%>
55
+ raise ArgumentError.new("Parameter <%= parameter.name %> must be passed in options hash.") unless options[:<%= parameter.name %>]
56
+ <%- end -%>
57
+ <%- if operation.parameters.any? { |p| p.location == 'path' } -%>
58
+ path = '<%= api.path %>' % options
59
+ <%- else -%>
60
+ path = '<%= api.path %>'
61
+ <%- end -%>
62
+ response = client(options).<%= operation.http_method.downcase %>(path, options)
63
+ <%- if operation.returns_array? -%>
64
+ <%- if operation.return_klass -%>
65
+ response.map { |hash| <%= operation.return_klass %>.new(hash.merge(client: options[:client])) }
66
+ <%- else -%>
67
+ response.map { |hash| <%= operation.return_klass %>.new(hash) }
68
+ <%- end -%>
69
+ <%- elsif operation.return_klass -%>
70
+ <%= operation.return_klass %>.new(response.merge(client: options[:client]))
71
+ <%- end -%>
72
+ <%- if operation.http_method == 'DELETE' -%>
73
+ rescue Ari::RequestError => e
74
+ raise unless e.code == '404'
75
+ <%- end -%>
76
+ end
77
+ <%- unless operation.method_name == operation.original_method_name -%>
78
+ class << self; alias_method :<%= operation.original_method_name %>, :<%= operation.method_name %>; end
79
+ <%- end -%>
80
+ <%- if operation.parameters.any? { |p| p.location == 'path' } -%>
81
+
82
+ def <%= operation.method_name %>(options = {})
83
+ self.class.<%= operation.method_name %>(options.merge(<%= id_attribute_name %>: self.id, client: @client))
84
+ end
85
+ <%- end -%>
86
+
87
+ <%- end -%>
88
+ <%- end unless generate_only_models? -%>
89
+
90
+ end
91
+ end
92
+
93
+ <%- unless generate_only_models? -%>
94
+ Ari::Client.send :define_method, '<%= resource_plural_name %>' do
95
+ Ari::ListResource.new(self, Ari::<%= klass_name %>)
96
+ end
97
+ <%- end -%>
@@ -0,0 +1,23 @@
1
+ module Ari
2
+ class ListResource
3
+
4
+ def initialize(client, resource_klass)
5
+ @client = client
6
+ @resource_klass = resource_klass
7
+ end
8
+
9
+ private
10
+
11
+ def method_missing(method, *args, &block)
12
+ if @resource_klass.respond_to? method
13
+ options = args.first
14
+ options ||= {}
15
+ options.merge!(client: @client)
16
+ @resource_klass.send(method, options, &block)
17
+ else
18
+ super
19
+ end
20
+ end
21
+
22
+ end
23
+ end
data/lib/ari/model.rb ADDED
@@ -0,0 +1,26 @@
1
+ require 'time'
2
+
3
+ module Ari
4
+ class Model
5
+
6
+ def initialize(attributes)
7
+ if attributes
8
+ @client = attributes.delete(:client)
9
+ self.attributes = attributes
10
+ end
11
+ end
12
+
13
+ def attributes=(attributes)
14
+ attributes.each do |name, value|
15
+ setter = "#{name}="
16
+ if respond_to? setter
17
+ value.merge!(client: @client) if @client && value.is_a?(Hash)
18
+ __send__ setter, value
19
+ else
20
+ instance_variable_set "@#{name}", value
21
+ end
22
+ end
23
+ end
24
+
25
+ end
26
+ end
data/lib/ari/models.rb ADDED
@@ -0,0 +1,47 @@
1
+ require 'ari/models/build_info'
2
+ require 'ari/models/system_info'
3
+ require 'ari/models/set_id'
4
+ require 'ari/models/config_info'
5
+ require 'ari/models/status_info'
6
+ require 'ari/models/asterisk_info'
7
+ require 'ari/models/variable'
8
+ require 'ari/models/dialed'
9
+ require 'ari/models/dialplan_cep'
10
+ require 'ari/models/caller_id'
11
+ require 'ari/models/text_message_variable'
12
+ require 'ari/models/text_message'
13
+ require 'ari/models/message'
14
+ require 'ari/models/missing_params'
15
+ require 'ari/models/device_state_changed'
16
+ require 'ari/models/playback_started'
17
+ require 'ari/models/playback_finished'
18
+ require 'ari/models/recording_started'
19
+ require 'ari/models/recording_finished'
20
+ require 'ari/models/recording_failed'
21
+ require 'ari/models/application_replaced'
22
+ require 'ari/models/bridge_created'
23
+ require 'ari/models/bridge_destroyed'
24
+ require 'ari/models/bridge_merged'
25
+ require 'ari/models/bridge_blind_transfer'
26
+ require 'ari/models/bridge_attended_transfer'
27
+ require 'ari/models/channel_created'
28
+ require 'ari/models/channel_destroyed'
29
+ require 'ari/models/channel_entered_bridge'
30
+ require 'ari/models/channel_left_bridge'
31
+ require 'ari/models/channel_state_change'
32
+ require 'ari/models/channel_dtmf_received'
33
+ require 'ari/models/channel_dialplan'
34
+ require 'ari/models/channel_caller_id'
35
+ require 'ari/models/channel_userevent'
36
+ require 'ari/models/channel_hangup_request'
37
+ require 'ari/models/channel_varset'
38
+ require 'ari/models/channel_talking_started'
39
+ require 'ari/models/channel_talking_finished'
40
+ require 'ari/models/endpoint_state_change'
41
+ require 'ari/models/dial'
42
+ require 'ari/models/stasis_end'
43
+ require 'ari/models/stasis_start'
44
+ require 'ari/models/text_message_received'
45
+ require 'ari/models/stored_recording'
46
+ require 'ari/models/live_recording'
47
+ require 'ari/models/format_lang_pair'