verboice 0.1.1 → 0.2.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.
Files changed (2) hide show
  1. data/lib/verboice.rb +96 -9
  2. metadata +6 -6
data/lib/verboice.rb CHANGED
@@ -8,28 +8,115 @@
8
8
  #
9
9
  # api = Verboice.new 'service_url', 'account_name', 'account_password', 'channel'
10
10
  require 'rubygems'
11
- require 'httparty'
11
+ require 'net/http'
12
12
  require 'json'
13
+ require 'rest_client'
14
+ require 'cgi'
15
+ require File.expand_path('../verboice/exception', __FILE__)
13
16
 
14
17
  # Provides access to the Verboice Public API.
15
18
  class Verboice
16
- include HTTParty
17
-
18
- # Creates an application-authenticated Verboice api access.
19
+ # Creates an account-authenticated Verboice api access.
19
20
  def initialize(url, account, password, default_channel = nil)
20
21
  @url = url
21
22
  @account = account
22
23
  @password = password
23
24
  @default_channel = default_channel
24
- @auth = {:username => account, :password => password}
25
+ @options = {
26
+ :user => account,
27
+ :password => password,
28
+ :headers => {:content_type => 'application/json'},
29
+ }
25
30
  end
26
-
31
+
27
32
  def call address, channel = @default_channel
28
- self.class.get "#{@url}/api/call", :query => {:channel => channel, :address => address}, :basic_auth => @auth
33
+ get_json "/api/call?#{self.class.to_query :channel => channel, :address => address}"
29
34
  end
30
-
35
+
31
36
  def call_state id
32
- self.class.get "#{@url}/api/call/#{id}/state", :basic_auth => @auth
37
+ get_json "/api/call/#{id}/state"
38
+ end
39
+
40
+ def create_channel(channel)
41
+ post "/api/channels.json", channel.to_json do |response, error|
42
+ handle_channel_error error if error
43
+
44
+ channel = JSON.parse response.body
45
+ with_indifferent_access channel
46
+ end
47
+ end
48
+
49
+ # Deletes a chnanel given its name.
50
+ #
51
+ # Raises Verboice::Exception if something goes wrong.
52
+ def delete_channel(name)
53
+ delete "/api/channels/#{name}" do |response, error|
54
+ raise Verboice::Exception.new error.message if error
55
+
56
+ response
57
+ end
58
+ end
59
+
60
+ private
61
+
62
+ def get(path)
63
+ resource = RestClient::Resource.new @url, @options
64
+ resource = resource[path].get
65
+ yield resource, nil
66
+ rescue => ex
67
+ yield nil, ex
33
68
  end
34
69
 
70
+ def get_json(path)
71
+ get(path) do |response, error|
72
+ raise Verboice::Exception.new error.message if error
73
+
74
+ elem = JSON.parse response.body
75
+ elem.map! { |x| with_indifferent_access x } if elem.is_a? Array
76
+ elem
77
+ end
78
+ end
79
+
80
+ def post(path, data)
81
+ resource = RestClient::Resource.new @url, @options
82
+ resource = resource[path].post(data)
83
+ yield resource, nil
84
+ rescue => ex
85
+ yield nil, ex
86
+ end
87
+
88
+ def delete(path)
89
+ resource = RestClient::Resource.new @url, @options
90
+ resource = resource[path].delete
91
+ yield resource, nil
92
+ rescue => ex
93
+ yield nil, ex
94
+ end
95
+
96
+ def self.to_query(hash)
97
+ query = ''
98
+ first = true
99
+ hash.each do |key, value|
100
+ query << '&' unless first
101
+ query << key.to_s
102
+ query << '='
103
+ query << CGI.escape(value.to_s)
104
+ first = false
105
+ end
106
+ query
107
+ end
108
+
109
+ def handle_channel_error(error)
110
+ if error.is_a? RestClient::BadRequest
111
+ response = JSON.parse error.response.body
112
+ raise Verboice::Exception.new response['summary'], Hash[response['properties'].map {|e| [e.keys[0], e.values[0]]}]
113
+ else
114
+ raise Verboice::Exception.new error.message
115
+ end
116
+ end
117
+
118
+ def with_indifferent_access(hash)
119
+ hash = hash.with_indifferent_access if hash.respond_to? :with_indifferent_access
120
+ hash
121
+ end
35
122
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: verboice
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - InSTEDD
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-08-10 00:00:00 Z
18
+ date: 2011-11-24 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: httparty
@@ -84,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
84
  requirements: []
85
85
 
86
86
  rubyforge_project:
87
- rubygems_version: 1.8.6
87
+ rubygems_version: 1.8.8
88
88
  signing_key:
89
89
  specification_version: 3
90
90
  summary: Access the Verboice API in ruby