exotel_api 0.5.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
+ SHA1:
3
+ metadata.gz: 13d423efa38ef4e16ec11cfc99f08f50074729e6
4
+ data.tar.gz: 58a898623c86440fb283ed9349347ef7382ada99
5
+ SHA512:
6
+ metadata.gz: cd2b8ca7059d3f6c7301fd96ee0cd70b967721ce27797f9e7edd7ca809eab1dfbf83408d183ca90c508158aeec77577b11440dc87b9bdd96ac44b79b6efc73b3
7
+ data.tar.gz: 2d2da0337a928c79ce1c00c2113f96b54fc8815290603a68b9071f58cbc4d2939184c05ff0e86b055d898bce7176c198fc24686533b6a1fc495e24e3ec36ff87
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /nbproject/private/
11
+ nbproject/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in exotel_api.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Aashutosh Chaudhary
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,41 @@
1
+ # ExotelApi
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/exotel_api`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'exotel_api'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install exotel_api
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/exotel_api.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,38 @@
1
+ module ExotelApi
2
+ module Call
3
+ module AfterDtmf
4
+ class ConnectController < ApplicationController
5
+ #response - nil
6
+ def status
7
+ begin
8
+ _call = direction
9
+ if _call.present?
10
+ _call.status(params) if defined?(_call.status)
11
+ end
12
+ rescue => e
13
+ logger.error e.message
14
+ logger.error e.backtrace.join("\n")
15
+ end
16
+ render :plain => '', content_type: "text/plain", :status => 200
17
+ end
18
+
19
+ private
20
+ def find_call
21
+ if params[:CustomField].present?
22
+ params[:CustomField].titleize.split.join.constantize::Call::AfterDtmf::Connect.find_by_call_sid(params[:CallSid])
23
+ else
24
+ Call.find_by_call_sid(params[:CallSid])
25
+ end
26
+ end
27
+
28
+ def direction
29
+ if params[:Direction] == 'incoming'
30
+ eval(ExotelApi.inbound_query)
31
+ else
32
+ find_call
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,62 @@
1
+ module ExotelApi
2
+ module Call
3
+ class AfterDtmfController < ApplicationController
4
+ #response - list of URLs
5
+ def greeting
6
+ begin
7
+ _call = direction
8
+ if _call.present?
9
+ urls = _call.greeting(params) if defined?(_call.greeting)
10
+ end
11
+ rescue => e
12
+ logger.error e.message
13
+ logger.error e.backtrace.join("\n")
14
+ end
15
+ render :plain => urls, content_type: "text/plain", :status => 200
16
+ end
17
+ #response - menu
18
+ def after_greeting
19
+ begin
20
+ _call = direction
21
+ if _call.present?
22
+ applet = "{\"select\":\"#{_call.after_greeting(params)}\"}" if defined?(_call.after_greeting)
23
+ end
24
+ rescue => e
25
+ logger.error e.message
26
+ logger.error e.backtrace.join("\n")
27
+ end
28
+ render :plain => applet, content_type: "text/html", status: 200
29
+ end
30
+ #response - phones
31
+ def connect
32
+ begin
33
+ _call = direction
34
+ if _call.present?
35
+ phones = _call.connect if defined?(_call.connect)
36
+ end
37
+ rescue => e
38
+ logger.error e.message
39
+ logger.error e.backtrace.join("\n")
40
+ end
41
+ render :plain => phones, content_type: "text/plain", :status => 200
42
+ end
43
+
44
+ private
45
+ def find_call
46
+ if params[:CustomField].present?
47
+ params[:CustomField].titleize.split.join.constantize::Call::AfterDtmf.find_by_call_sid(params[:CallSid])
48
+ else
49
+ Call.find_by_call_sid(params[:CallSid])
50
+ end
51
+ end
52
+
53
+ def direction
54
+ if params[:Direction] == 'incoming'
55
+ eval(ExotelApi.inbound_query)
56
+ else
57
+ find_call
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,38 @@
1
+ module ExotelApi
2
+ module Call
3
+ module AfterGreeting
4
+ class ConnectController < ApplicationController
5
+ #response - nil
6
+ def status
7
+ begin
8
+ _call = direction
9
+ if _call.present?
10
+ _call.status(params) if defined?(_call.status)
11
+ end
12
+ rescue => e
13
+ logger.error e.message
14
+ logger.error e.backtrace.join("\n")
15
+ end
16
+ render :plain => '', content_type: "text/plain", :status => 200
17
+ end
18
+
19
+ private
20
+ def find_call
21
+ if params[:CustomField].present?
22
+ params[:CustomField].titleize.split.join.constantize::Call::AfterGreeting::Connect.find_by_call_sid(params[:CallSid])
23
+ else
24
+ Call.find_by_call_sid(params[:CallSid])
25
+ end
26
+ end
27
+
28
+ def direction
29
+ if params[:Direction] == 'incoming'
30
+ eval(ExotelApi.inbound_query)
31
+ else
32
+ find_call
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,36 @@
1
+ module ExotelApi
2
+ module Call
3
+ class AfterGreetingController < ApplicationController
4
+ #response - phone
5
+ def connect
6
+ begin
7
+ _call = direction
8
+ if _call.present?
9
+ phones = _call.connect if defined?(_call.connect)
10
+ end
11
+ rescue => e
12
+ logger.error e.message
13
+ logger.error e.backtrace.join("\n")
14
+ end
15
+ render :plain => phones, content_type: "text/plain", :status => 200
16
+ end
17
+
18
+ private
19
+ def find_call
20
+ if params[:CustomField].present?
21
+ params[:CustomField].titleize.split.join.constantize::Call::AfterGreeting.find_by_call_sid(params[:CallSid])
22
+ else
23
+ Call.find_by_call_sid(params[:CallSid])
24
+ end
25
+ end
26
+
27
+ def direction
28
+ if params[:Direction] == 'incoming'
29
+ eval(ExotelApi.inbound_query)
30
+ else
31
+ find_call
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,36 @@
1
+ module ExotelApi
2
+ module Call
3
+ class ConnectController < CallController
4
+ #response - nil
5
+ def status
6
+ begin
7
+ _call = direction
8
+ if _call.present?
9
+ _call.status(params) if defined?(_call.status)
10
+ end
11
+ rescue => e
12
+ logger.error e.message
13
+ logger.error e.backtrace.join("\n")
14
+ end
15
+ render :plain => '', content_type: "text/plain", :status => 200
16
+ end
17
+
18
+ private
19
+ def find_call
20
+ if params[:CustomField].present?
21
+ params[:CustomField].titleize.split.join.constantize::Call::Connect.find_by_call_sid(params[:CallSid])
22
+ else
23
+ Call.find_by_call_sid(params[:CallSid])
24
+ end
25
+ end
26
+
27
+ def direction
28
+ if params[:Direction] == 'incoming'
29
+ eval(ExotelApi.inbound_query)
30
+ else
31
+ find_call
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,127 @@
1
+ module ExotelApi
2
+ class CallController < ApplicationController
3
+ skip_before_filter :verify_authenticity_token
4
+ #response - greeting,menu,connect
5
+ def start
6
+ begin
7
+ _call = direction
8
+ if _call.present?
9
+ applet = "{\"select\":\"#{_call.start(params)}\"}" if defined?(_call.start)
10
+ end
11
+ rescue => e
12
+ logger.error e.message
13
+ logger.error e.backtrace.join("\n")
14
+ end
15
+ render :plain => applet, content_type: "text/html", status: 200
16
+ end
17
+ #response - list of URLS
18
+ def greeting
19
+ begin
20
+ _call = direction
21
+ if _call.present?
22
+ urls = _call.greeting(params) if defined?(_call.greeting)
23
+ end
24
+ rescue => e
25
+ logger.error e.message
26
+ logger.error e.backtrace.join("\n")
27
+ end
28
+ render :plain => urls, content_type: "text/plain", :status => 200
29
+ end
30
+ #response - menu,connect
31
+ def after_greeting
32
+ begin
33
+ _call = direction
34
+ if _call.present?
35
+ applet = "{\"select\":\"#{_call.after_greeting(params)}\"}" if defined?(_call.after_greeting)
36
+ end
37
+ rescue => e
38
+ logger.error e.message
39
+ logger.error e.backtrace.join("\n")
40
+ end
41
+ render :plain => applet, content_type: "text/html", status: 200
42
+ end
43
+ #response - phone
44
+ def connect
45
+ begin
46
+ _call = direction
47
+ if _call.present?
48
+ phones = _call.connect if defined?(_call.connect)
49
+ end
50
+ rescue => e
51
+ logger.error e.message
52
+ logger.error e.backtrace.join("\n")
53
+ end
54
+ render :plain => phones, content_type: "text/plain", :status => 200
55
+ end
56
+ #response - nil
57
+ def finish
58
+ begin
59
+ _call = direction
60
+ if _call.present?
61
+ _call.finish(params) if defined?(_call.finish)
62
+ end
63
+ rescue => e
64
+ logger.error e.message
65
+ logger.error e.backtrace.join("\n")
66
+ end
67
+ render :plain => '', content_type: "text/plain", :status => 200
68
+ end
69
+ #response - list of URLS
70
+ def menu
71
+ begin
72
+ _call = direction
73
+ if _call.present?
74
+ urls = _call.menu(params) if defined?(_call.menu)
75
+ end
76
+ rescue => e
77
+ logger.error e.message
78
+ logger.error e.backtrace.join("\n")
79
+ end
80
+ render :plain => urls, content_type: "text/plain", :status => 200
81
+ end
82
+ #response - nil
83
+ def dtmf
84
+ begin
85
+ _call = direction
86
+ if _call.present?
87
+ _call.dtmf_key(params) if defined?(_call.dtmf_key)
88
+ end
89
+ rescue => e
90
+ logger.error e.message
91
+ logger.error e.backtrace.join("\n")
92
+ end
93
+ render :plain => '', content_type: "text/plain", :status => 200
94
+ end
95
+ #response - greeting,connect
96
+ def after_dtmf
97
+ begin
98
+ _call = direction
99
+ if _call.present?
100
+ applet = "{\"select\":\"#{_call.after_dtmf(params)}\"}" if defined?(_call.after_dtmf)
101
+ end
102
+ rescue => e
103
+ logger.error e.message
104
+ logger.error e.backtrace.join("\n")
105
+ end
106
+ render :plain => applet, content_type: "text/html", status: 200
107
+ end
108
+
109
+
110
+ private
111
+ def find_call
112
+ if params[:CustomField].present?
113
+ params[:CustomField].titleize.split.join.constantize::Call.find_by_call_sid(params[:CallSid])
114
+ else
115
+ Call.find_by_call_sid(params[:CallSid])
116
+ end
117
+ end
118
+
119
+ def direction
120
+ if params[:Direction] == 'incoming'
121
+ eval(ExotelApi.inbound_query)
122
+ else
123
+ find_call
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,88 @@
1
+ module ExotelApi
2
+ class Ivr::CallController < ApplicationController
3
+ skip_before_filter :verify_authenticity_token
4
+ def greeting_and_menu
5
+ begin
6
+ _call = direction
7
+ if _call.present?
8
+ _call.update(status: 'completed')
9
+ urls = _call.greeting_and_menu if defined?(_call.greeting_and_menu)
10
+ end
11
+ rescue => e
12
+ logger.error e.message
13
+ logger.error e.backtrace.join("\n")
14
+ end
15
+ render :plain => urls, content_type: "text/plain", :status => 200
16
+ end
17
+
18
+ def dtmf
19
+ begin
20
+ _call = direction
21
+ if _call.present?
22
+ _call.update(dtmf: params[:digits].parameterize.to_i)
23
+ _call.dtmf_callback(params) if defined?(_call.dtmf_callback)
24
+ end
25
+ rescue => e
26
+ logger.error e.message
27
+ logger.error e.backtrace.join("\n")
28
+ end
29
+ render :plain => '', content_type: "text/plain", :status => 200
30
+ end
31
+
32
+ def closing
33
+ begin
34
+ _call = direction
35
+ urls = _call.closing if _call.present? and defined?(_call.closing)
36
+ rescue => e
37
+ logger.error e.message
38
+ logger.error e.backtrace.join("\n")
39
+ end
40
+ render :plain => urls, content_type: "text/plain", :status => 200
41
+ end
42
+
43
+ def status
44
+ begin
45
+ _call = direction
46
+ if _call.present?
47
+ _call.update(status: params[:Status])
48
+ _call.status_callback(params) if defined?(_call.status_callback)
49
+ end
50
+ rescue => e
51
+ logger.error e.message
52
+ logger.error e.backtrace.join("\n")
53
+ end
54
+ render :plain => '', content_type: "text/plain", :status => 200
55
+ end
56
+
57
+ def repeat
58
+ begin
59
+ _call = direction
60
+ if _call.present?
61
+ _call.repeat += 1
62
+ _call.save
63
+ status = _call.repeat_callback(params) if defined?(_call.repeat_callback)
64
+ end
65
+ rescue => e
66
+ logger.error e.message
67
+ logger.error e.backtrace.join("\n")
68
+ end
69
+ render :plain => '', content_type: "text/plain", :status => status
70
+ end
71
+
72
+ private
73
+ def find_call
74
+ if params[:CustomField].present?
75
+ params[:CustomField].titleize.split.join.constantize::Call.find_by_call_sid(params[:CallSid])
76
+ else
77
+ eval(ExotelApi.status_callback_query)
78
+ end
79
+ end
80
+ def direction
81
+ if params[:Direction] == 'incoming'
82
+ eval(ExotelApi.inbound_query)
83
+ else
84
+ find_call
85
+ end
86
+ end
87
+ end
88
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "exotel_api"
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/config/routes.rb ADDED
@@ -0,0 +1,40 @@
1
+ Rails.application.routes.draw do
2
+ namespace :exotel_api do
3
+ namespace :ivr do
4
+ namespace :call do
5
+ get 'greeting_and_menu'
6
+ get 'dtmf'
7
+ get 'closing'
8
+ get 'repeat'
9
+ post 'status'
10
+ end
11
+ end
12
+
13
+ namespace :call do
14
+ get 'start'
15
+ get 'greeting'
16
+ get 'after_greeting'
17
+ namespace :after_greeting do
18
+ get 'connect'
19
+ namespace :connect do
20
+ get 'status'
21
+ end
22
+ end
23
+ get 'connect'
24
+ namespace :connect do
25
+ get 'status'
26
+ end
27
+ post 'finish'
28
+ get 'menu'
29
+ get 'dtmf'
30
+ get 'after_dtmf'
31
+ namespace :after_dtmf do
32
+ get 'greeting'
33
+ get 'after_greeting'
34
+ namespace :connect do
35
+ get 'status'
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'exotel_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "exotel_api"
8
+ spec.version = ExotelApi::VERSION
9
+ spec.authors = ["Aashutosh Chaudhary"]
10
+ spec.email = ["ashugodia@gmail.com"]
11
+
12
+ spec.summary = %q{Exotel Call API}
13
+ spec.description = %q{To use exotel call api}
14
+ spec.homepage = "https://github.com/ashugodia/exotel-api.git"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.14"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_dependency "httparty", "~> 0.14.0"
36
+ spec.add_runtime_dependency 'rails', '~> 4.1', '>= 4.1.0'
37
+ spec.add_runtime_dependency 'activesupport', '~> 4.1', '>= 4.1.0'
38
+ end
@@ -0,0 +1,15 @@
1
+ module ExotelApi
2
+ class << self
3
+ attr_accessor :exotel_sid, :exotel_token, :inbound_query, :status_callback_query
4
+
5
+ def configure
6
+ yield self
7
+ end
8
+ end
9
+
10
+ class AuthenticationError < StandardError; end
11
+
12
+ class UnexpectedError < StandardError; end
13
+
14
+ class ParamsError < StandardError; end
15
+ end
@@ -0,0 +1,4 @@
1
+ module ExotelApi
2
+ class Engine < Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,111 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'httparty'
3
+ module ExotelApi
4
+ module Ivr
5
+ class Call
6
+ include HTTParty
7
+ base_uri "https://twilix.exotel.in/v1/Accounts"
8
+ default_timeout 10
9
+
10
+ def initialize; end
11
+
12
+ #TODO check if this is a good decision to provide a wrapper.
13
+ def self.shoot(params={})
14
+ self.new.shoot(params)
15
+ end
16
+
17
+ def self.connect_to_flow(params={})
18
+ self.new.connect_to_flow(params)
19
+ end
20
+
21
+ def self.connect_to_agent(params={})
22
+ self.new.connect_to_agent(params)
23
+ end
24
+
25
+ def self.details(params={})
26
+ self.new.details(params)
27
+ end
28
+
29
+ def shoot(params={})
30
+ if valid?(params, {:type => 'flow'})
31
+ params = transfrom_params(params, {:type => 'flow'})
32
+ make_call(params)
33
+ end
34
+ end
35
+
36
+ def connect_to_flow(params={})
37
+ if valid?(params, {:type => 'flow'})
38
+ params = transfrom_params(params, {:type => 'flow'})
39
+ make_call(params)
40
+ end
41
+ end
42
+
43
+ def connect_to_agent(params={})
44
+ if valid?(params, {:type => 'agent'})
45
+ params = transfrom_params(params, {:type => 'agent'})
46
+ make_call(params)
47
+ end
48
+ end
49
+
50
+ def details(sid)
51
+ response = self.class.get("/#{ExotelApi.exotel_sid}/Calls/#{sid}", :basic_auth => auth)
52
+ handle_response(response)
53
+ end
54
+
55
+ protected
56
+
57
+ def make_call(params)
58
+ response = self.class.post(URI.escape("/#{ExotelApi.exotel_sid}/Calls/connect"), {:body => params, :basic_auth => auth })
59
+ handle_response(response)
60
+ end
61
+
62
+ def valid?(params, options)
63
+ mandatory_keys = [:from, :to, :caller_id, :call_type]
64
+ mandatory_keys << :flow_id if options[:type] == 'flow'
65
+
66
+ unless mandatory_keys.all?{|key| params.keys.include?(key)}
67
+ raise ExotelApi::ParamsError, "Missing one or many required parameters."
68
+ end
69
+ valid_call_type?(params)
70
+ return true
71
+ end
72
+
73
+ def valid_call_type?(params)
74
+ raise ExotelApi::ParamsError, "Call Type is not valid" unless ['trans', 'promo'].include?(params[:call_type])
75
+ end
76
+
77
+ def auth
78
+ {:username => ExotelApi.exotel_sid, :password => ExotelApi.exotel_token}
79
+ end
80
+
81
+ def flow_url(flow_id)
82
+ "http://my.exotel.in/exoml/start/#{flow_id}"
83
+ end
84
+
85
+ def transfrom_params(params, options)
86
+ if options[:type] == 'flow'
87
+ #Construct flow url and delete flow_id.
88
+ params = params.merge(:URL => flow_url(params[:flow_id]))
89
+ params.delete(:flow_id)
90
+ end
91
+
92
+ #Keys are converted to camelcase
93
+ params.inject({}){ |h, (key, value)| h[camelcase_key(key)] = value; h }
94
+ end
95
+
96
+ def camelcase_key(key)
97
+ key.to_s.split('_').map(&:capitalize).join.to_sym #Input: call_type, Output: :CallType
98
+ end
99
+
100
+ def handle_response(response)
101
+ case response.code.to_i
102
+ when 200...300 then ExotelApi::Response.new(response)
103
+ when 401 then raise ExotelApi::AuthenticationError, "#{response.body} Verify your sid and token."
104
+ when 403 then ExotelApi::Response.new(response)
105
+ else
106
+ raise ExotelApi::UnexpectedError, response.body
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,37 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'httparty'
3
+ module ExotelApi
4
+ class Metadata
5
+ include HTTParty
6
+ base_uri "https://twilix.exotel.in/v1/Accounts"
7
+
8
+ def initialize; end
9
+
10
+ def self.details(params={})
11
+ self.new.details(params)
12
+ end
13
+
14
+ def details(number)
15
+ response = self.class.get("/#{ExotelApi.exotel_sid}/Numbers/#{number}", :basic_auth => auth)
16
+ handle_response(response)
17
+ end
18
+
19
+ protected
20
+
21
+ def auth
22
+ {:username => ExotelApi.exotel_sid, :password => ExotelApi.exotel_token}
23
+ end
24
+
25
+ def handle_response(response)
26
+ case response.code.to_i
27
+ when 200...300 then ExotelApi::Response.new(response)
28
+ when 401 then raise ExotelApi::AuthenticationError, "#{response.body} Verify your sid and token."
29
+ else
30
+ raise ExotelApi::UnexpectedError, response.body
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+
37
+
@@ -0,0 +1,45 @@
1
+ # -*- encoding: utf-8 -*-
2
+ module ExotelApi
3
+ class Response
4
+
5
+ def initialize(response)
6
+ #To handle unexpected parsing from httparty
7
+ response = response.parsed_response
8
+ response_base = response['TwilioResponse']
9
+ unless response_base.include?('RestException')
10
+ set_response_data(response_base)
11
+ else
12
+ set_response_error(response_base)
13
+ end
14
+ end
15
+
16
+ def set_response_data(response_base)
17
+ (response_base['Call'] or response_base['SMSMessage'] or response_base['Numbers'] or response_base['Result']).each do |key, value|
18
+ set_variable(key, value)
19
+ end
20
+ end
21
+
22
+ def set_response_error(response_base)
23
+ response_base['RestException'].each do |key, value|
24
+ set_variable(key, value)
25
+ instance_variable_set('@status', 'DND') #Override
26
+ end
27
+ end
28
+
29
+ def set_variable(key, value)
30
+ attr_name = underscore_format(key)
31
+ self.class.send(:attr_accessor, attr_name) #Set accessors dynamically
32
+ instance_variable_set("@#{attr_name}", value)
33
+ end
34
+
35
+ protected
36
+
37
+ #TODO: CamelCase to underscore: check if we have to add this to string class.
38
+ def underscore_format(string)
39
+ string.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
40
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
41
+ tr("-", "_").
42
+ downcase
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module ExotelApi
2
+ VERSION = "0.5.0"
3
+ end
data/lib/exotel_api.rb ADDED
@@ -0,0 +1,13 @@
1
+ require "rails"
2
+ require "active_support/dependencies"
3
+ require 'httparty'
4
+ require 'exotel_api/version'
5
+ require 'exotel_api/engine'
6
+ require 'exotel_api/config'
7
+ require 'exotel_api/response'
8
+ require 'exotel_api/metadata'
9
+ require 'exotel_api/ivr/call'
10
+
11
+ module ExotelApi
12
+ # Your code goes here...
13
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: exotel_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Aashutosh Chaudhary
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-09-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: httparty
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.14.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.14.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.1'
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 4.1.0
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '4.1'
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 4.1.0
75
+ - !ruby/object:Gem::Dependency
76
+ name: activesupport
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '4.1'
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 4.1.0
85
+ type: :runtime
86
+ prerelease: false
87
+ version_requirements: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - "~>"
90
+ - !ruby/object:Gem::Version
91
+ version: '4.1'
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 4.1.0
95
+ description: To use exotel call api
96
+ email:
97
+ - ashugodia@gmail.com
98
+ executables: []
99
+ extensions: []
100
+ extra_rdoc_files: []
101
+ files:
102
+ - ".gitignore"
103
+ - Gemfile
104
+ - LICENSE.txt
105
+ - README.md
106
+ - Rakefile
107
+ - app/controllers/exotel_api/call/after_dtmf/connect_controller.rb
108
+ - app/controllers/exotel_api/call/after_dtmf_controller.rb
109
+ - app/controllers/exotel_api/call/after_greeting/connect_controller.rb
110
+ - app/controllers/exotel_api/call/after_greeting_controller.rb
111
+ - app/controllers/exotel_api/call/connect_controller.rb
112
+ - app/controllers/exotel_api/call_controller.rb
113
+ - app/controllers/exotel_api/ivr/call_controller.rb
114
+ - bin/console
115
+ - bin/setup
116
+ - config/routes.rb
117
+ - exotel_api.gemspec
118
+ - lib/exotel_api.rb
119
+ - lib/exotel_api/config.rb
120
+ - lib/exotel_api/engine.rb
121
+ - lib/exotel_api/ivr/call.rb
122
+ - lib/exotel_api/metadata.rb
123
+ - lib/exotel_api/response.rb
124
+ - lib/exotel_api/version.rb
125
+ homepage: https://github.com/ashugodia/exotel-api.git
126
+ licenses:
127
+ - MIT
128
+ metadata:
129
+ allowed_push_host: https://rubygems.org
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubyforge_project:
146
+ rubygems_version: 2.4.8
147
+ signing_key:
148
+ specification_version: 4
149
+ summary: Exotel Call API
150
+ test_files: []