ncco 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a216f5030e69381a0ca46d99de4a863b516db4fd66f22064355e67cdb046d54c
4
+ data.tar.gz: e1b6caab15abf000193c4f8710b9b50522ce00cd15dd7e576c41547f8e5e77e2
5
+ SHA512:
6
+ metadata.gz: 674f045c36ab386af75256ac35ada221181eb5e342cff6e136fa307c5d5e2d21e9f829d8bac21da767d24e1e43aeb2ea9a4ee63d9ed6282e0c140cf0d3e06aff
7
+ data.tar.gz: f6cc04a4edbf5de4a22fdee12a5950416cb450d9dfeb82b25a6dcbfc1a0d32d58b83788b453efed44cd6d0612b574f86bff214046fcc43c91354dccf3d8f2c53
@@ -0,0 +1,20 @@
1
+ ---
2
+ version: 2
3
+ jobs:
4
+ build:
5
+ working_directory: ~/ncco
6
+ docker:
7
+ - image: circleci/ruby:2.5.1
8
+ steps:
9
+ - checkout
10
+ - type: cache-restore
11
+ name: Restore Bundler cache
12
+ key: ncco-{{ checksum "Gemfile.lock" }}
13
+ - run: bundle install --path vendor/bundle
14
+ - type: cache-save
15
+ name: Save Bundler cache
16
+ key: ncco-{{ checksum "Gemfile.lock" }}
17
+ paths:
18
+ - vendor/bundle
19
+ - run: bundle exec rake
20
+ - run: bundle exec rake lint
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ inherit_gem:
2
+ gc_ruboconfig: rubocop.yml
3
+
4
+ AllCops:
5
+ TargetRubyVersion: 2.5
6
+
7
+ RSpec/NestedGroups:
8
+ Max: 5
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # v0.1.0 (25 November 2018)
2
+
3
+ * Initial release
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at me@timrogers.co.uk. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in ncco.gemspec
8
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,107 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ncco (0.1.0)
5
+ dry-validation (~> 0.12.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (5.2.1)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 0.7, < 2)
13
+ minitest (~> 5.1)
14
+ tzinfo (~> 1.1)
15
+ ast (2.4.0)
16
+ coderay (1.1.2)
17
+ concurrent-ruby (1.1.3)
18
+ diff-lcs (1.3)
19
+ dry-configurable (0.7.0)
20
+ concurrent-ruby (~> 1.0)
21
+ dry-container (0.6.0)
22
+ concurrent-ruby (~> 1.0)
23
+ dry-configurable (~> 0.1, >= 0.1.3)
24
+ dry-core (0.4.7)
25
+ concurrent-ruby (~> 1.0)
26
+ dry-equalizer (0.2.1)
27
+ dry-inflector (0.1.2)
28
+ dry-logic (0.4.2)
29
+ dry-container (~> 0.2, >= 0.2.6)
30
+ dry-core (~> 0.2)
31
+ dry-equalizer (~> 0.2)
32
+ dry-types (0.13.2)
33
+ concurrent-ruby (~> 1.0)
34
+ dry-container (~> 0.3)
35
+ dry-core (~> 0.4, >= 0.4.4)
36
+ dry-equalizer (~> 0.2)
37
+ dry-inflector (~> 0.1, >= 0.1.2)
38
+ dry-logic (~> 0.4, >= 0.4.2)
39
+ dry-validation (0.12.2)
40
+ concurrent-ruby (~> 1.0)
41
+ dry-configurable (~> 0.1, >= 0.1.3)
42
+ dry-core (~> 0.2, >= 0.2.1)
43
+ dry-equalizer (~> 0.2)
44
+ dry-logic (~> 0.4, >= 0.4.0)
45
+ dry-types (~> 0.13.1)
46
+ gc_ruboconfig (2.3.13)
47
+ rubocop (>= 0.57, < 0.61)
48
+ rubocop-rspec (~> 1.25)
49
+ i18n (1.1.1)
50
+ concurrent-ruby (~> 1.0)
51
+ jaro_winkler (1.5.1)
52
+ method_source (0.9.2)
53
+ minitest (5.11.3)
54
+ parallel (1.12.1)
55
+ parser (2.5.3.0)
56
+ ast (~> 2.4.0)
57
+ powerpack (0.1.2)
58
+ pry (0.12.2)
59
+ coderay (~> 1.1.0)
60
+ method_source (~> 0.9.0)
61
+ rainbow (3.0.0)
62
+ rake (10.5.0)
63
+ rspec (3.8.0)
64
+ rspec-core (~> 3.8.0)
65
+ rspec-expectations (~> 3.8.0)
66
+ rspec-mocks (~> 3.8.0)
67
+ rspec-core (3.8.0)
68
+ rspec-support (~> 3.8.0)
69
+ rspec-expectations (3.8.2)
70
+ diff-lcs (>= 1.2.0, < 2.0)
71
+ rspec-support (~> 3.8.0)
72
+ rspec-mocks (3.8.0)
73
+ diff-lcs (>= 1.2.0, < 2.0)
74
+ rspec-support (~> 3.8.0)
75
+ rspec-support (3.8.0)
76
+ rubocop (0.60.0)
77
+ jaro_winkler (~> 1.5.1)
78
+ parallel (~> 1.10)
79
+ parser (>= 2.5, != 2.5.1.1)
80
+ powerpack (~> 0.1)
81
+ rainbow (>= 2.2.2, < 4.0)
82
+ ruby-progressbar (~> 1.7)
83
+ unicode-display_width (~> 1.4.0)
84
+ rubocop-rspec (1.30.1)
85
+ rubocop (>= 0.60.0)
86
+ ruby-progressbar (1.10.0)
87
+ thread_safe (0.3.6)
88
+ tzinfo (1.2.5)
89
+ thread_safe (~> 0.1)
90
+ unicode-display_width (1.4.0)
91
+
92
+ PLATFORMS
93
+ ruby
94
+
95
+ DEPENDENCIES
96
+ activesupport (~> 5.2.1)
97
+ bundler (~> 1.17.a)
98
+ gc_ruboconfig (~> 2.3.13)
99
+ ncco!
100
+ pry (~> 0.12.2)
101
+ rake (~> 10.0)
102
+ rspec (~> 3.0)
103
+ rubocop (~> 0.60.0)
104
+ rubocop-rspec (~> 1.30.1)
105
+
106
+ BUNDLED WITH
107
+ 1.17.0.pre.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Tim Rogers
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,92 @@
1
+ # NCCO
2
+
3
+ [Nexmo Call Control Objects](https://developer.nexmo.com/voice/voice-api/ncco-reference) (NCCOs) allow you to decide with your code what happens in a phone call which you've placed or received using [Nexmo's Voice API](https://developer.nexmo.com/voice/voice-api/overview).
4
+
5
+ If you make a mistake in your NCCO (for example, you miss out a required parameter or misspell something), the call will fail.
6
+
7
+ This gem allows you to __validate__ your NCCOs before you send them to Nexmo, giving you a tighter feedback loop and helping you to catch errors earlier, before they hit production (for example when you run your tests).
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's `Gemfile`:
12
+
13
+ ```ruby
14
+ gem "ncco", "~> 0.1.0"
15
+ ```
16
+
17
+ Next, install the gem by running:
18
+
19
+ ```bash
20
+ bundle
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ Nexmo Call Control Objects (NCCOs) are JSON arrays of objects. In Ruby, you'll build a Ruby array of hashes, where each hash represents an "action" in your call, and then you'll send it to Nexmo as JSON:
26
+
27
+ ```ruby
28
+ say_hello = {
29
+ action: "talk",
30
+ text: "Hello there! You're through to Acme Widgets. Leave a message after the tone. Press star or hang up when you're done.",
31
+ }
32
+
33
+ record_message = {
34
+ action: "record",
35
+ endOnKey: "*",
36
+ beepStart: true,
37
+ eventUrl: "https://acmewidgets.com/nexmo/recordings",
38
+ eventMethod: "POST",
39
+ format: "mp4"
40
+ }
41
+
42
+ ncco = [say_hello, record_message]
43
+ render json: ncco
44
+ ```
45
+
46
+ The eagle-eyed among you will have noticed that there's a mistake in our example above: Nexmo doesn't support the `mp4` format for recordings! This would lead to an error, and a bad experience for our users 😭
47
+
48
+ But there's a better way:
49
+
50
+ ```ruby
51
+ require "ncco"
52
+
53
+ render json: NCCO.build(ncco)
54
+ ```
55
+
56
+ This will still fail - our NCCO is invalid! - but our application will let us know ahead of time:
57
+
58
+ ```ruby
59
+ NCCO.build(ncco)
60
+ # NCCO::InvalidActionError (The 2nd action is invalid: format must be one of: mp3, wav,
61
+ # ogg, eventUrl must be a valid HTTP or HTTPS URL)
62
+ ```
63
+
64
+ This allows you to catch errors earlier and easily handle them with your usual exception tracking tools (for example Sentry).
65
+
66
+ ## Development
67
+
68
+ After checking out the repo, run `bin/setup` to install dependencies.
69
+
70
+ Then, run `rake spec` to run the tests and `rake lint` to check the code style with Rubocop.
71
+
72
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment with the gem.
73
+
74
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
75
+
76
+ ## Contributing
77
+
78
+ NCCO "actions" are defined using schemas in `lib/ncco/schemas`, implemented using [dry-validation](https://github.com/dry-rb/dry-validation).
79
+
80
+ To tweak how we validate an action (for example to add a new attribute or change how an existing one is validated), just update the action's corresponding schema. Testing is easy thanks to our `allow_value`, `require_attribute` and `allow_blank_values` matchers - see the existing tests for an example.
81
+
82
+ If you want to add support for a new action, you'll need to crerate a new schema in `lib/ncco/schemas`, require it and add it to `SCHEMAS_BY_TYPE` in `lib/ncco.rb` and write some tests.
83
+
84
+ Bug reports and pull requests are welcome on GitHub at https://github.com/timrogers/ncco. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
85
+
86
+ ## License
87
+
88
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
89
+
90
+ ## Code of Conduct
91
+
92
+ Everyone interacting in the Ncco project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/timrogers/ncco/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+ require "rubocop/rake_task"
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+ RuboCop::RakeTask.new(:lint)
9
+
10
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ncco"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/examples/rails.rb ADDED
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ class IncomingCallsController < ApplicationController
4
+ def create
5
+ say_hello = {
6
+ action: "talk",
7
+ text: "Hello there! You're through to Acme Widgets. Leave a message.",
8
+ }
9
+
10
+ record_message = {
11
+ action: "record",
12
+ eventUrl: record_incoming_calls_url,
13
+ eventMethod: "POST",
14
+ }
15
+
16
+ render json: NCCO.build([say_hello, record_message])
17
+ end
18
+
19
+ def record
20
+ recording_url = params[:recording_url]
21
+ conversation_uuid = params[:conversation_uuid]
22
+
23
+ SaveRecordedMessage.call(recording_url: recording_url,
24
+ conversation_uuid: conversation_uuid)
25
+
26
+ render status: 204, nothing: true
27
+ end
28
+ end
data/lib/ncco.rb ADDED
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry-validation"
4
+ require "ncco/version"
5
+ require "ncco/predicates"
6
+ require "ncco/schemas/base_schema"
7
+ require "ncco/schemas/connect"
8
+ require "ncco/schemas/conversation"
9
+ require "ncco/schemas/input"
10
+ require "ncco/schemas/record"
11
+ require "ncco/schemas/stream"
12
+ require "ncco/schemas/talk"
13
+
14
+ module NCCO
15
+ class InvalidActionError < StandardError; end
16
+
17
+ SCHEMAS_BY_TYPE = {
18
+ "connect" => Schemas::Connect,
19
+ "conversation" => Schemas::Conversation,
20
+ "input" => Schemas::Input,
21
+ "record" => Schemas::Record,
22
+ "stream" => Schemas::Stream,
23
+ "talk" => Schemas::Talk,
24
+ }.freeze
25
+
26
+ # A Nexmo Call Control Object (NCCO) is a JSON array that you use to control the flow
27
+ # of a Nexmo call. This method validates an array, ensuring the "actions" inside are
28
+ # valid NCCO actions, either throwing an explanatory error if they're not, or returning
29
+ # back the array if the input is valid.
30
+ #
31
+ # This method can be used to pre-emptively ensure that an NCCO is valid *before* sending
32
+ # it to Nexmo, providing a kind of static analysis.
33
+ #
34
+ # @param actions [Array<Hash>] a Nexmo Call Control Object (NCCO) as a Ruby array, made
35
+ # up of a series of "actions", each of which is a Ruby hash
36
+ # @return [Array<Hash>] the Nexmo Call Control Object (NCCO), as a Ruby array, which
37
+ # was passed in
38
+ # @raise [InvalidActionError] if any of the actions within the Nexmo Call Control
39
+ # Object is invalid. The error message will include details on which action was
40
+ # invalid and why.
41
+ def self.build(actions)
42
+ actions.
43
+ map { |action| action.transform_keys(&:to_sym) }.
44
+ each_with_index { |action, index| validate_action!(action, index: index) }
45
+
46
+ actions
47
+ end
48
+
49
+ class << self
50
+ private
51
+
52
+ # Validates an NCCO action, raising an error if it is invalid
53
+ #
54
+ # @param action [Hash] the NCCO action
55
+ # @param index [Integer] the position of the action in the NCCO array
56
+ # @raise [InvalidActionError] if the action is invalid
57
+ def validate_action!(action, index:)
58
+ schema = SCHEMAS_BY_TYPE[action[:action]]
59
+
60
+ unless schema
61
+ raise_invalid_error("#{action[:action]} isn't a valid action type",
62
+ index: index)
63
+ end
64
+
65
+ error_messages = transform_error_messages(schema.call(action).messages(full: true))
66
+
67
+ raise_invalid_error(error_messages.join(", "), index: index) if error_messages.any?
68
+ end
69
+
70
+ # Raises an InvalidActionError, featuring the human-readable index of the action
71
+ # within the provided NCCO object, with a specified error message
72
+ #
73
+ # @param error_message [String] the specific error message
74
+ # @param index [Integer] the zero-indexed position of the action within the NCCO
75
+ # object
76
+ # @raise [InvalidActionError]
77
+ def raise_invalid_error(error_message, index:)
78
+ raise InvalidActionError, "The #{ordinalized_number(index + 1)} action is " \
79
+ "invalid: #{error_message}"
80
+ end
81
+
82
+ # Finds a number's "ordinal string", used to denote its position in an ordered
83
+ # sequence, and returns the number followed by that string. See `#ordinalize` for
84
+ # further information.
85
+ #
86
+ # @param number [Integer] the number to fetch the ordinal string for
87
+ # @return [String] the number, followed by the ordinal string corresponding to the
88
+ # number
89
+ def ordinalized_number(number)
90
+ "#{number}#{ordinalize(number)}"
91
+ end
92
+
93
+ # Turns a number into an "ordinal string" used to denote its position in an ordered
94
+ # sequence - for example passing in 1 returns "st" (1st), 2 returns "nd" (2nd), etc.
95
+ #
96
+ # @param number [Integer] the number to fetch the ordinal string for
97
+ # @return [String] the ordinal string corresponding to the number
98
+ def ordinalize(number)
99
+ case number.digits.last
100
+ when 0 then "th"
101
+ when 1 then "st"
102
+ when 2 then "nd"
103
+ when 3 then "rd"
104
+ when 4..9 then "th"
105
+ end
106
+ end
107
+
108
+ # Transforms error messages returned by dry-validations into a consistent format. For
109
+ # field-level errors, we get back a `Hash` mapping the attribute name to an array of
110
+ # `String` error messages, whereas for unrecognised attributes (which is a bit of a
111
+ # hack), we just get back an array. This handles either gracefully.
112
+ #
113
+ # @param error_messages [Array<String>, Hash] messages returned by dry-validations
114
+ # @return [Array<String>] a list of error messages which is guaranteed to be an array
115
+ def transform_error_messages(error_messages)
116
+ return error_messages if error_messages.is_a?(Array)
117
+
118
+ error_messages.values.flatten
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,10 @@
1
+ en:
2
+ errors:
3
+ strict_keys?: has attributes which aren't part of the NCCO specification
4
+ http_or_https_url?: must be a valid HTTP or HTTPS URL
5
+ supported_http_method?: must be a supported HTTP method
6
+ single_digit?: must be a single digit from a phone keypad, as a string
7
+ digits?: must be a series of one or more digits from a phone keypad, as a string
8
+ e164?: must be a valid E.164-formatted phone number
9
+ websocket_url?: must be a valid Websocket URL
10
+ hash_with_string_keys_and_values?: must be a hash with String keys and values
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NCCO
4
+ module Predicates
5
+ include ::Dry::Logic::Predicates
6
+
7
+ predicate(:http_or_https_url?) do |value|
8
+ uri = URI.parse(value)
9
+ %w[http https].include?(uri.scheme)
10
+ rescue URI::InvalidURIError
11
+ false
12
+ end
13
+
14
+ predicate(:websocket_url?) do |value|
15
+ uri = URI.parse(value)
16
+ uri.scheme == "ws"
17
+ rescue URI::InvalidURIError
18
+ false
19
+ end
20
+
21
+ # TODO: Check what HTTP methods are supported by Nexmo - presumably not the full set?
22
+ # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods>
23
+ predicate(:supported_http_method?) do |value|
24
+ %w[GET POST].include?(value)
25
+ end
26
+
27
+ NUMBERS = (0..9).map(&:to_s).freeze
28
+ DIGITS = [*NUMBERS, "*", "#"].freeze
29
+
30
+ predicate(:single_digit?) do |value|
31
+ DIGITS.include?(value)
32
+ end
33
+
34
+ predicate(:digits?) do |value|
35
+ value.is_a?(String) &&
36
+ !value.empty? &&
37
+ (value.chars - DIGITS).none?
38
+ end
39
+
40
+ predicate(:e164?) do |value|
41
+ # Regular expression taken from https://www.twilio.com/docs/glossary/what-e164
42
+ value =~ /^\+?[1-9]\d{1,14}$/
43
+ end
44
+
45
+ predicate(:hash_with_string_keys_and_values?) do |value|
46
+ value.is_a?(Hash) &&
47
+ value.keys.map(&:class).uniq == [String] &&
48
+ value.values.map(&:class).uniq == [String]
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NCCO
4
+ module Schemas
5
+ BaseSchema = Dry::Validation.Schema do
6
+ input :hash?, :strict_keys?
7
+
8
+ configure do
9
+ predicates(Predicates)
10
+
11
+ def strict_keys?(input)
12
+ (input.keys - rules.keys).empty?
13
+ end
14
+
15
+ config.messages_file = File.join(File.dirname(__FILE__), "../data/errors.yml")
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NCCO
4
+ module Schemas
5
+ # TODO: Validate these attributes depending on the specified endpoint type
6
+ ConnectEndpoint = Dry::Validation.Schema(BaseSchema) do
7
+ required(:type).value(included_in?: %w[phone websocket sip])
8
+
9
+ # Phone endpoint attributes
10
+ optional(:number).value(:e164?)
11
+ optional(:onAnswer).value(:http_or_https_url?)
12
+ optional(:dtmfAnswer).value(:digits?)
13
+
14
+ # Websocket endpoint attributes
15
+ optional(:uri).value(:websocket_url?) # Also used for SIP endpoints
16
+ optional("content-type").value(eql?: "audio/l16;rate=16000")
17
+ optional(:headers).value(:hash_with_string_keys_and_values?)
18
+ end
19
+
20
+ Connect = Dry::Validation.Schema(BaseSchema) do
21
+ required(:action).value(eql?: "connect")
22
+ required(:endpoint).schema(ConnectEndpoint)
23
+ optional(:from).value(:e164?)
24
+ # TODO: What are the other options? (e.g. what is the default?)
25
+ optional(:eventType).value(included_in?: ["synchronous"])
26
+ # TODO: Are there any limitations on timeout?
27
+ optional(:timeout).value(type?: Integer, gteq?: 1)
28
+ optional(:limit).value(type?: Integer, gteq?: 1, lteq?: 7200)
29
+ optional(:machineDetection).value(included_in?: %w[continue hangup])
30
+ optional(:eventUrl).value(:http_or_https_url?)
31
+ optional(:eventMethod).value(:supported_http_method?)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NCCO
4
+ module Schemas
5
+ Conversation = Dry::Validation.Schema(BaseSchema) do
6
+ required(:action).value(eql?: "conversation")
7
+ required(:name).value(:filled?, type?: String)
8
+ optional(:musicOnHoldUrl).value(:http_or_https_url?)
9
+ optional(:startOnEnter).value(:bool?)
10
+ optional(:endOnExit).value(:bool?)
11
+ optional(:record).value(:bool?)
12
+ optional(:eventUrl).value(:http_or_https_url?)
13
+ optional(:eventMethod).value(:supported_http_method?)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NCCO
4
+ module Schemas
5
+ Input = Dry::Validation.Schema(BaseSchema) do
6
+ required(:action).value(eql?: "input")
7
+ optional(:timeOut).value(type?: Integer, gteq?: 1, lteq?: 10)
8
+ optional(:maxDigits).value(type?: Integer, gteq?: 1, lteq?: 20)
9
+ optional(:submitOnHash).value(:bool?)
10
+ optional(:eventUrl).value(:http_or_https_url?)
11
+ optional(:eventMethod).value(:supported_http_method?)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NCCO
4
+ module Schemas
5
+ DIGITS = (0..9).map(&:to_s).freeze
6
+
7
+ Record = Dry::Validation.Schema(BaseSchema) do
8
+ required(:action).value(eql?: "record")
9
+ optional(:format).value(included_in?: %w[mp3 wav ogg])
10
+ # TODO: Is nil allowed? What are the other options?
11
+ optional(:split).value(included_in?: [nil, "conversation"])
12
+ optional(:channels).value(type?: Integer, gteq?: 1, lteq?: 32)
13
+ optional(:endOnSilence).value(type?: Integer, gteq?: 3, lteq?: 10)
14
+ optional(:endOnKey).value(:single_digit?)
15
+ optional(:beepStart).value(:bool?)
16
+ optional(:timeout).value(type?: Integer, gteq?: 3, lteq?: 7200)
17
+ optional(:eventUrl).value(:http_or_https_url?)
18
+ optional(:eventMethod).value(:supported_http_method?)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NCCO
4
+ module Schemas
5
+ Stream = Dry::Validation.Schema(BaseSchema) do
6
+ required(:action).value(eql?: "stream")
7
+ required(:streamUrl).value(:http_or_https_url?)
8
+ optional(:bargeIn).value(:bool?)
9
+ optional(:loop).value(type?: Integer)
10
+ optional(:level).value(included_in?: [-1, 0, 1])
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NCCO
4
+ module Schemas
5
+ Talk = Dry::Validation.Schema(BaseSchema) do
6
+ required(:action).value(eql?: "talk")
7
+ required(:text).value(:filled?, type?: String)
8
+ optional(:bargeIn).value(:bool?)
9
+ optional(:loop).value(type?: Integer)
10
+ optional(:level).value(included_in?: [-1, 0, 1])
11
+ optional(:voiceName).value(:filled?, type?: String)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NCCO
4
+ VERSION = "0.1.0"
5
+ end
data/ncco.gemspec ADDED
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("lib", __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "ncco/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "ncco"
9
+ spec.version = NCCO::VERSION
10
+ spec.authors = ["Tim Rogers"]
11
+ spec.email = ["me@timrogers.co.uk"]
12
+
13
+ spec.summary = "Build and validate Nexmo Call Control Objects (NCCOs) with a " \
14
+ "simple API"
15
+ spec.homepage = "https://github.com/timrogers/ncco-ruby"
16
+ spec.license = "MIT"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = spec.homepage
20
+ spec.metadata["changelog_uri"] = "https://github.com/timrogers/ncco-ruby/blob/master/CHANGELOG.md"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_dependency "dry-validation", "~> 0.12.2"
32
+
33
+ spec.add_development_dependency "activesupport", "~> 5.2.1"
34
+ spec.add_development_dependency "bundler", "~> 1.17.a"
35
+ spec.add_development_dependency "gc_ruboconfig", "~> 2.3.13"
36
+ spec.add_development_dependency "pry", "~> 0.12.2"
37
+ spec.add_development_dependency "rake", "~> 10.0"
38
+ spec.add_development_dependency "rspec", "~> 3.0"
39
+ spec.add_development_dependency "rubocop", "~> 0.60.0"
40
+ spec.add_development_dependency "rubocop-rspec", "~> 1.30.1"
41
+ end
metadata ADDED
@@ -0,0 +1,199 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ncco
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tim Rogers
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-11-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-validation
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.12.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.12.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 5.2.1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 5.2.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.17.a
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.17.a
55
+ - !ruby/object:Gem::Dependency
56
+ name: gc_ruboconfig
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.3.13
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 2.3.13
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.12.2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.12.2
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.60.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.60.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop-rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 1.30.1
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 1.30.1
139
+ description:
140
+ email:
141
+ - me@timrogers.co.uk
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".circleci/config.yml"
147
+ - ".gitignore"
148
+ - ".rspec"
149
+ - ".rubocop.yml"
150
+ - CHANGELOG.md
151
+ - CODE_OF_CONDUCT.md
152
+ - Gemfile
153
+ - Gemfile.lock
154
+ - LICENSE.txt
155
+ - README.md
156
+ - Rakefile
157
+ - bin/console
158
+ - bin/setup
159
+ - examples/rails.rb
160
+ - lib/ncco.rb
161
+ - lib/ncco/data/errors.yml
162
+ - lib/ncco/predicates.rb
163
+ - lib/ncco/schemas/base_schema.rb
164
+ - lib/ncco/schemas/connect.rb
165
+ - lib/ncco/schemas/conversation.rb
166
+ - lib/ncco/schemas/input.rb
167
+ - lib/ncco/schemas/record.rb
168
+ - lib/ncco/schemas/stream.rb
169
+ - lib/ncco/schemas/talk.rb
170
+ - lib/ncco/version.rb
171
+ - ncco.gemspec
172
+ homepage: https://github.com/timrogers/ncco-ruby
173
+ licenses:
174
+ - MIT
175
+ metadata:
176
+ homepage_uri: https://github.com/timrogers/ncco-ruby
177
+ source_code_uri: https://github.com/timrogers/ncco-ruby
178
+ changelog_uri: https://github.com/timrogers/ncco-ruby/blob/master/CHANGELOG.md
179
+ post_install_message:
180
+ rdoc_options: []
181
+ require_paths:
182
+ - lib
183
+ required_ruby_version: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ required_rubygems_version: !ruby/object:Gem::Requirement
189
+ requirements:
190
+ - - ">="
191
+ - !ruby/object:Gem::Version
192
+ version: '0'
193
+ requirements: []
194
+ rubyforge_project:
195
+ rubygems_version: 2.7.6
196
+ signing_key:
197
+ specification_version: 4
198
+ summary: Build and validate Nexmo Call Control Objects (NCCOs) with a simple API
199
+ test_files: []