tropo-rest 0.0.1 → 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.
- data/LICENSE +19 -0
- data/README.md +39 -0
- data/lib/tropo-rest.rb +69 -27
- data/lib/tropo-rest/cli.rb +22 -4
- data/lib/tropo-rest/version.rb +1 -1
- data/tropo-rest.gemspec +1 -0
- metadata +21 -6
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (C) 2011 by Ben Langfeld
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
TropoREST
|
2
|
+
--------
|
3
|
+
|
4
|
+
TropoREST provides a wrapper for the Tropo REST API and also for Troplets.
|
5
|
+
|
6
|
+
Installation
|
7
|
+
============
|
8
|
+
gem install tropo-rest
|
9
|
+
|
10
|
+
Library
|
11
|
+
=======
|
12
|
+
|
13
|
+
require 'tropo-rest'
|
14
|
+
TropoREST.tokens = {tropo: {voice: 'your_voice_token', messaging: 'your_messaging_token'}, troplets: {voice: 'your_troplets_voice_token', messaging: 'your_troplets_messaging_token'}}
|
15
|
+
|
16
|
+
TropoREST.originate some_options_hash
|
17
|
+
|
18
|
+
Check out the [YARD documentation](http://rdoc.info/github/benlangfeld/tropo-rest/master/frames) for more
|
19
|
+
|
20
|
+
Console
|
21
|
+
=======
|
22
|
+
|
23
|
+
TropoREST comes with a basic console for testing purposes. You can launch it via the `tropo-rest` binary and you will be asked for your application tokens. You can alternatively provide them as arguments in the order tropo\_voice, tropo\_messaging, troplets\_voice, troplets\_messaging.
|
24
|
+
|
25
|
+
|
26
|
+
Note on Patches/Pull Requests
|
27
|
+
-----------------------------
|
28
|
+
|
29
|
+
* Fork the project.
|
30
|
+
* Make your feature addition or bug fix.
|
31
|
+
* Add tests for it. This is important so I don't break it in a future version unintentionally.
|
32
|
+
* Commit, do not mess with rakefile, version, or history.
|
33
|
+
* If you want to have your own version, that is fine but bump version in a commit by itself so I can ignore when I pull
|
34
|
+
* Send me a pull request. Bonus points for topic branches.
|
35
|
+
|
36
|
+
Copyright
|
37
|
+
---------
|
38
|
+
|
39
|
+
Copyright (c) 2011 Ben Langfeld. MIT licence (see LICENSE for details).
|
data/lib/tropo-rest.rb
CHANGED
@@ -1,23 +1,35 @@
|
|
1
|
-
require
|
2
|
-
require 'httparty'
|
3
|
-
require 'logger'
|
4
|
-
require 'singleton'
|
1
|
+
%w{active_support/core_ext/class/attribute_accessors active_support/core_ext/object/blank json httparty logger}.each { |l| require l }
|
5
2
|
|
6
3
|
module TropoREST
|
7
|
-
class
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
4
|
+
class << self
|
5
|
+
def logger=(logger)
|
6
|
+
[API, Troplets].each do |endpoint|
|
7
|
+
endpoint.send :logger=, logger
|
8
|
+
end
|
9
|
+
end
|
12
10
|
|
13
|
-
def
|
14
|
-
|
15
|
-
|
11
|
+
def tokens=(tokens)
|
12
|
+
[[:tropo, API], [:troplets, Troplets]].each do |type, target|
|
13
|
+
if token = tokens[type]
|
14
|
+
if token.is_a? String
|
15
|
+
target.voice_token = token
|
16
|
+
elsif token.is_a? Hash
|
17
|
+
target.voice_token = token[:voice]
|
18
|
+
target.messaging_token = token[:messaging]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
16
22
|
end
|
17
23
|
|
18
|
-
def
|
19
|
-
|
24
|
+
def method_missing(method_name, *args, &block)
|
25
|
+
API.send method_name, *args, &block
|
20
26
|
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class API
|
30
|
+
include HTTParty
|
31
|
+
|
32
|
+
cattr_accessor :logger, :voice_token, :messaging_token
|
21
33
|
|
22
34
|
base_uri 'https://api.tropo.com'
|
23
35
|
SESSION_URI = '/1.0/sessions'
|
@@ -25,22 +37,52 @@ module TropoREST
|
|
25
37
|
format :json
|
26
38
|
headers 'Accept' => 'application/json', 'content-type' => 'application/json'
|
27
39
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
40
|
+
self.logger = Logger.new STDOUT
|
41
|
+
self.logger.level = Logger::DEBUG
|
42
|
+
debug_output self.logger
|
43
|
+
|
44
|
+
class << self
|
45
|
+
def originate(options = {})
|
46
|
+
logger.info "Originating Tropo call with parameters #{options}"
|
47
|
+
resp = request SESSION_URI, options
|
48
|
+
logger.info "Tropo origination responded with: #{resp.inspect}"
|
49
|
+
resp
|
50
|
+
end
|
51
|
+
|
52
|
+
def fire_event(event_name, session_id)
|
53
|
+
logger.info "Firing event #{event_name} into Tropo session #{session_id}"
|
54
|
+
resp = request "#{SESSION_URI}/#{session_id}/signals", value: event_name
|
55
|
+
logger.info "Tropo event trigger responded with: #{resp.inspect}"
|
56
|
+
resp
|
57
|
+
end
|
34
58
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
59
|
+
def request(target, body, type = nil)
|
60
|
+
options = options_with_token body, type
|
61
|
+
logger.debug "Making request to #{target} with #{options.inspect}"
|
62
|
+
post target, body: options.to_json
|
63
|
+
end
|
64
|
+
|
65
|
+
def options_with_token(options, type = :voice)
|
66
|
+
token = case type
|
67
|
+
when :messaging then messaging_token
|
68
|
+
else voice_token
|
69
|
+
end
|
70
|
+
options.merge! token: token
|
71
|
+
end
|
40
72
|
end
|
41
73
|
end
|
42
74
|
|
43
|
-
|
44
|
-
|
75
|
+
class Troplets < API
|
76
|
+
base_uri 'http://troplets.tropo.com'
|
77
|
+
|
78
|
+
class << self
|
79
|
+
def say(to, message, options = {})
|
80
|
+
logger.debug "Using Troplets to say #{message} to #{to}"
|
81
|
+
options.merge! to: to, message: message
|
82
|
+
resp = request '/say', options, options[:channel] == :voice ? :voice : :messaging
|
83
|
+
logger.debug "Troplet#say responded with: #{resp.inspect}"
|
84
|
+
resp
|
85
|
+
end
|
86
|
+
end
|
45
87
|
end
|
46
88
|
end
|
data/lib/tropo-rest/cli.rb
CHANGED
@@ -1,11 +1,29 @@
|
|
1
|
-
require
|
2
|
-
require 'tropo-rest'
|
1
|
+
%w{pry tropo-rest}.each { |l| require l }
|
3
2
|
|
4
3
|
module TropoREST
|
5
4
|
class CLI
|
6
5
|
def self.start
|
7
|
-
|
8
|
-
|
6
|
+
puts "Setting up your session..."
|
7
|
+
|
8
|
+
tokens = {tropo: {voice: ARGV[0], messaging: ARGV[1]}, troplets: {voice: ARGV[2], messaging: ARGV[3]}}
|
9
|
+
unless tokens[:tropo][:voice].present?
|
10
|
+
print "Enter your Tropo voice token: "
|
11
|
+
tokens[:tropo][:voice] = gets
|
12
|
+
end
|
13
|
+
unless tokens[:tropo][:messaging].present?
|
14
|
+
print "Enter your Tropo messaging token: "
|
15
|
+
tokens[:tropo][:messaging] = gets
|
16
|
+
end
|
17
|
+
unless tokens[:troplets][:voice].present?
|
18
|
+
print "Enter your Troplets voice token: "
|
19
|
+
tokens[:troplets][:voice] = gets
|
20
|
+
end
|
21
|
+
unless tokens[:troplets][:messaging].present?
|
22
|
+
print "Enter your Troplets messaging token: "
|
23
|
+
tokens[:troplets][:messaging] = gets
|
24
|
+
end
|
25
|
+
|
26
|
+
TropoREST.tokens = tokens
|
9
27
|
pry
|
10
28
|
end
|
11
29
|
end
|
data/lib/tropo-rest/version.rb
CHANGED
data/tropo-rest.gemspec
CHANGED
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
- 0
|
8
7
|
- 1
|
9
|
-
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Ben Langfeld
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-05-
|
17
|
+
date: 2011-05-12 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -44,7 +44,7 @@ dependencies:
|
|
44
44
|
type: :runtime
|
45
45
|
version_requirements: *id002
|
46
46
|
- !ruby/object:Gem::Dependency
|
47
|
-
name:
|
47
|
+
name: activesupport
|
48
48
|
prerelease: false
|
49
49
|
requirement: &id003 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
segments:
|
55
55
|
- 0
|
56
56
|
version: "0"
|
57
|
-
type: :
|
57
|
+
type: :runtime
|
58
58
|
version_requirements: *id003
|
59
59
|
- !ruby/object:Gem::Dependency
|
60
|
-
name:
|
60
|
+
name: rspec
|
61
61
|
prerelease: false
|
62
62
|
requirement: &id004 !ruby/object:Gem::Requirement
|
63
63
|
none: false
|
@@ -69,6 +69,19 @@ dependencies:
|
|
69
69
|
version: "0"
|
70
70
|
type: :development
|
71
71
|
version_requirements: *id004
|
72
|
+
- !ruby/object:Gem::Dependency
|
73
|
+
name: yard
|
74
|
+
prerelease: false
|
75
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
83
|
+
type: :development
|
84
|
+
version_requirements: *id005
|
72
85
|
description:
|
73
86
|
email:
|
74
87
|
- ben@langfeld.me
|
@@ -82,6 +95,8 @@ files:
|
|
82
95
|
- .gitignore
|
83
96
|
- .rspec
|
84
97
|
- Gemfile
|
98
|
+
- LICENSE
|
99
|
+
- README.md
|
85
100
|
- Rakefile
|
86
101
|
- bin/tropo-rest
|
87
102
|
- lib/tropo-rest.rb
|