onsip 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e86219349aaabe949b5c8d61fa83a7532fec8049
4
- data.tar.gz: 5e2a2ce635807c8e79db668e63cd5ab3f9e54346
3
+ metadata.gz: d756dbee6749a9399eefa71653ff605ce1181167
4
+ data.tar.gz: 777b636d24fcf319c711edf35a79f2b0cf7650f2
5
5
  SHA512:
6
- metadata.gz: 775131b7aad62549de28d8d0d600d443faa312cc38dd88f83a4f8c5dbc799c2cb9cbdd335e5f4048b5fa3b01f8249a6dbed5528949c0fa5bdd587c979da10c59
7
- data.tar.gz: 29ccf0628368492cfd8ce9c97c68b4b7eb00d25bec4d01a6dd8b8ba1fd3d2b5bc8f11edbac0cccd5500336feb8491acd8ac5666f2ff9b85d16fc66db78d3e017
6
+ metadata.gz: e5358759f94c71ac2e61f02738d613e5c6d7d2988f81a6b54f778085425202beec664018d9b1cc301519c905af2fa40c1e5781aa71b2548c322e598ec14725ec
7
+ data.tar.gz: c2be2aead48b8afc2ad3378543f0f8ec74bd7016a11784bc4d3354d90b4e5cddd5c8ea927fff17ea93ee1c3a75f48c9138660372b85eaf7ef25dad8ce8b41702
data/.rspec CHANGED
@@ -1,2 +1,3 @@
1
1
  --color
2
+ --format documentation
2
3
  --require spec_helper
@@ -1,17 +1,17 @@
1
1
  module OnSIP
2
- class OnSIPException < StandardError
3
- attr_accessor :options, :exception
2
+ module Exceptions
3
+ class OnSIPError < StandardError
4
+ attr_reader :object
4
5
 
5
- def initialize(options = {})
6
- @options = options
7
- @message = @options[:message]
8
- @exception = @options[:exception]
6
+ def initialize(object)
7
+ @object = object
8
+ end
9
9
  end
10
- end
11
10
 
12
- class OnSIPRequestException < OnSIPException
13
- def response
14
- @options[:response]
11
+ class OnSIPRequestError < OnSIPError
12
+ def response
13
+ @object[:response]
14
+ end
15
15
  end
16
16
  end
17
17
  end
@@ -2,21 +2,44 @@ module OnSIP
2
2
  module ResponseParser
3
3
 
4
4
  module ClassMethods
5
+ include ::OnSIP::Exceptions
6
+
5
7
  def parse_response(response, key_path = [], ignore_result_keys = [])
6
- result = []
8
+ begin
9
+
10
+ if valid_response?(response)
11
+ value_at_key_path(response.env.body, key_path, ignore_result_keys)
12
+ else
13
+ raise StandardError, 'Problem validating OnSIP response'
14
+ end
15
+
16
+ rescue StandardError => e
17
+ raise OnSIPRequestError.new(:message => 'Problem with parsing response',
18
+ :exception => e,
19
+ :response => response)
20
+ end
21
+ end
7
22
 
8
- return result unless response && !response.env.body.blank?
23
+ def valid_response?(response)
24
+ if response && response.env[:status] == 200 && !response.env.body.blank?
25
+ r = response.env.body
26
+ r['Response']['Context']['Action']['IsCompleted'] == 'false' ? false : true
27
+ else
28
+ false
29
+ end
30
+ end
9
31
 
10
- r = response.env.body
32
+ def value_at_key_path(body, key_path = [], ignore_result_keys = [])
33
+ result = []
11
34
 
12
35
  key_path.each_with_index do |key, i|
13
- if i < (key_path.length - 1) && r[key]
14
- r = r[key]
15
- elsif i == (key_path.length - 1) && r[key] && r[key].kind_of?(Array)
16
- a = r[key]
36
+ if i < (key_path.length - 1) && body[key]
37
+ body = body[key]
38
+ elsif i == (key_path.length - 1) && body[key] && body[key].kind_of?(Array)
39
+ a = body[key]
17
40
  result = a.flatten.map { |h| h.delete_if { |k| ignore_result_keys.include?(k) }; h }
18
- elsif i == (key_path.length - 1) && r[key] && r[key].kind_of?(Hash)
19
- h = r[key]
41
+ elsif i == (key_path.length - 1) && body[key] && body[key].kind_of?(Hash)
42
+ h = body[key]
20
43
  h.delete_if { |k| ignore_result_keys.include?(k) }
21
44
  result = [h]
22
45
  else
@@ -25,11 +48,8 @@ module OnSIP
25
48
  end
26
49
 
27
50
  result
28
- rescue StandardError => e
29
- raise OnSIPRequestException.new(:message => 'Problem with parsing response',
30
- :exception => e,
31
- :response => response)
32
51
  end
52
+
33
53
  end
34
54
 
35
55
  extend ClassMethods
data/lib/onsip/session.rb CHANGED
@@ -36,6 +36,7 @@ module OnSIP
36
36
  module ClassMethods
37
37
  def create(username, password)
38
38
  response = OnSIP.connection.get('/api', {'Action' => 'SessionCreate', 'Username' => username, 'Password' => password, 'Output' => 'json'}, {})
39
+ yield response if block_given?
39
40
  process_create_session_response response
40
41
  end
41
42
 
@@ -51,6 +52,7 @@ module OnSIP
51
52
 
52
53
  def destroy!(session_id)
53
54
  response = OnSIP.connection.get('/api', {'Action' => 'SessionDestroy', 'SessionId' => session_id, 'Output' => 'json'}, {})
55
+ yield response if block_given?
54
56
  process_destroy_session_response response
55
57
  end
56
58
 
data/lib/onsip/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module OnSIP
2
- VERSION = '0.0.9'
2
+ VERSION = '0.1.0'
3
3
  end
data/lib/onsip.rb CHANGED
@@ -22,7 +22,7 @@ module OnSIP
22
22
  autoload :CDR, 'onsip/models/cdr'
23
23
 
24
24
  module ClassMethods
25
- attr_writer :logger
25
+ attr_writer :logger, :session
26
26
 
27
27
  def logger
28
28
  @logger ||= init_logger
data/onsip.gemspec CHANGED
@@ -28,7 +28,7 @@ Gem::Specification.new do |spec|
28
28
  spec.add_development_dependency 'rb-fsevent', '~> 0.9.3'
29
29
  spec.add_development_dependency 'simplecov', '~> 0.8.2'
30
30
  spec.add_development_dependency 'rspec', '>= 3.1.0'
31
- spec.add_runtime_dependency 'activesupport', '>= 3.0.0'
31
+ spec.add_runtime_dependency 'activesupport', ['>= 3.0.0', '<= 4.0.0']
32
32
  spec.add_runtime_dependency 'trollop', '~> 2.0'
33
33
  spec.add_runtime_dependency 'multi_json', '~> 1.10.1'
34
34
  spec.add_runtime_dependency 'nokogiri', '~> 1.6.1'
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe OnSIP::Account do
4
+ before :each do
5
+
6
+ success_body = {"Response"=>{"Context"=>{"Action"=>{"IsCompleted"=>"true"}, "Request"=>{"IsValid"=>"true", "DateTime"=>"2014-12-04T20:34:18+00:00", "Duration"=>"50", "Parameters"=>{"Parameter"=>[{"Name"=>"AccountId", "Value"=>"27370"}, {"Name"=>"Action", "Value"=>"AccountRead"}, {"Name"=>"Output", "Value"=>"json"}, {"Name"=>"SessionId", "Value"=>"1vflpm8sa25vt88o6nsl60lgi5"}]}}, "Session"=>{"IsEstablished"=>"true", "SessionId"=>"1vflpm8sa25vt88o6nsl60lgi5", "UserId"=>"212762", "Roles"=>{"Role"=>{"Name"=>"Account Admin"}}}}, "Result"=>{"AccountRead"=>{"Account"=>{"AccountId"=>"27370", "Receivable"=>"0.00", "Promocode"=>{}, "Productcode"=>"platform", "PerUserPricing"=>"false", "ResellerBilling"=>"false", "BalanceSeconds"=>"0", "EscrowSecondsUpdate"=>"0", "EscrowSecondsCredit"=>"0", "Balance"=>"00.00", "CreditLimit"=>"0.00", "Recharge"=>"false", "RechargeLevel"=>"0.00", "RechargeThreshold"=>"0.00", "Modified"=>"2014-12-03T19:04:34+00:00", "Created"=>"2014-03-24T14:15:44+00:00", "Contact"=>{"Name"=>"Bar Foo", "Organization"=>"IceHook Systems", "Address"=>"123 Main St", "City"=>"New York", "State"=>"NY", "Zipcode"=>"100000", "Country"=>"United States of America", "CountryId"=>"207", "Phone"=>"1-212-555-1212", "Email"=>"test@icehook.com", "Modified"=>"2014-03-24T14:15:44+00:00"}, "CreditCard"=>{"CreditCardId"=>"12345", "AccountId"=>"54321", "CreditCardType"=>"3", "Name"=>"Foo Bar", "LastFour"=>"****", "Expiration"=>"01/2017", "Modified"=>"2014-10-15T18:07:36+00:00", "FirstName"=>"Foo", "LastName"=>"Bar", "Address"=>"123 Main St", "City"=>"New York", "State"=>"NY", "Zipcode"=>"10000", "CountryId"=>"207", "Phone"=>"1-212-555-1212", "IPAddress"=>"127.0.0.1"}}}}}}.to_json
7
+ stub_request(:get, Regexp.new('http://success.onsip.com/api?.*AccountRead.*'))
8
+ .to_return({
9
+ :body => success_body,
10
+ :status => 200,
11
+ :headers => { 'Content-Type' => 'application/json; charset=utf-8' }
12
+ })
13
+
14
+ failed_body = {"Response"=>{"Context"=>{"Action"=>{"IsCompleted"=>"false", "Errors"=>{"Error"=>{"Code"=>"AccountAuthorization.Required", "Message"=>"Permission to perform the requested action has not been granted."}}}, "Request"=>{"IsValid"=>"true", "DateTime"=>"2014-12-04T21:36:21+00:00", "Duration"=>"41", "Parameters"=>{"Parameter"=>[{"Name"=>"AccountId", "Value"=>"12345"}, {"Name"=>"Action", "Value"=>"AccountRead"}, {"Name"=>"Output", "Value"=>"json"}, {"Name"=>"SessionId", "Value"=>"j3kmb4boemqnl6u1vsip210rp7"}]}}, "Session"=>{"IsEstablished"=>"true", "SessionId"=>"j3kmb4boemqnl6u1vsip210rp7", "UserId"=>"212762", "Roles"=>{"Role"=>{"Name"=>"Account Admin"}}}}}}.to_json
15
+ stub_request(:get, Regexp.new('http://fail.onsip.com/api?.*AccountRead.*'))
16
+ .to_return({
17
+ :body => failed_body,
18
+ :status => 200,
19
+ :headers => { 'Content-Type' => 'application/json; charset=utf-8' }
20
+ })
21
+
22
+ OnSIP.session = OnSIP::Session.new("IsEstablished" => "true")
23
+ end
24
+
25
+ describe "Onsip::Account.read" do
26
+ it "has a valid id" do
27
+ OnSIP.connect('http://success.onsip.com')
28
+ account = OnSIP::Account.read('27370')
29
+ expect(account.class).to eq OnSIP::Account
30
+ end
31
+
32
+ it "has an invalid id" do
33
+ OnSIP.connect('http://fail.onsip.com')
34
+ account = nil
35
+ expect { account = OnSIP::Account.read('12345') }.to raise_error(OnSIP::Exceptions::OnSIPRequestError)
36
+ expect(account).to be(nil)
37
+ end
38
+ end
39
+
40
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe OnSIP::CDR do
4
+ before :each do
5
+
6
+ body = {"Response"=>{"Context"=>{"Action"=>{"IsCompleted"=>"true"}, "Request"=>{"IsValid"=>"true", "DateTime"=>"2014-12-04T17:39:53+00:00", "Duration"=>"30", "Parameters"=>{"Parameter"=>[{"Name"=>"Action", "Value"=>"SessionCreate"}, {"Name"=>"Output", "Value"=>"json"}, {"Name"=>"Password", "Value"=>"******"}, {"Name"=>"Username", "Value"=>"icehook"}]}}, "Session"=>{"IsEstablished"=>"true", "SessionId"=>"j2vtu1bk7rsdpklofplu07ocq1", "UserId"=>"212762", "Roles"=>{"Role"=>{"Name"=>"Account Admin"}}}}, "Result"=>{"SessionCreate"=>{}}}}.to_json
7
+ stub_request(:get, Regexp.new('http://api.onsip.com/api?.*CdrBrowse.*'))
8
+ .to_return({
9
+ :body => body,
10
+ :status => 200,
11
+ :headers => { 'Content-Type' => 'application/json; charset=utf-8' }
12
+ })
13
+ OnSIP.connect('http://api.onsip.com')
14
+ end
15
+
16
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe OnSIP::ExternalAddress do
4
+ before :each do
5
+
6
+ body = {"Response"=>{"Context"=>{"Action"=>{"IsCompleted"=>"true"}, "Request"=>{"IsValid"=>"true", "DateTime"=>"2014-12-04T17:39:53+00:00", "Duration"=>"30", "Parameters"=>{"Parameter"=>[{"Name"=>"Action", "Value"=>"SessionCreate"}, {"Name"=>"Output", "Value"=>"json"}, {"Name"=>"Password", "Value"=>"******"}, {"Name"=>"Username", "Value"=>"icehook"}]}}, "Session"=>{"IsEstablished"=>"true", "SessionId"=>"j2vtu1bk7rsdpklofplu07ocq1", "UserId"=>"212762", "Roles"=>{"Role"=>{"Name"=>"Account Admin"}}}}, "Result"=>{"SessionCreate"=>{}}}}.to_json
7
+ stub_request(:get, Regexp.new('http://api.onsip.com/api?.*ExternalAddressAdd.*'))
8
+ .to_return({
9
+ :body => body,
10
+ :status => 200,
11
+ :headers => { 'Content-Type' => 'application/json; charset=utf-8' }
12
+ })
13
+ OnSIP.connect('http://api.onsip.com')
14
+ end
15
+
16
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe OnSIP::Organization do
4
+ before :each do
5
+
6
+ body = {"Response"=>{"Context"=>{"Action"=>{"IsCompleted"=>"true"}, "Request"=>{"IsValid"=>"true", "DateTime"=>"2014-12-04T17:39:53+00:00", "Duration"=>"30", "Parameters"=>{"Parameter"=>[{"Name"=>"Action", "Value"=>"SessionCreate"}, {"Name"=>"Output", "Value"=>"json"}, {"Name"=>"Password", "Value"=>"******"}, {"Name"=>"Username", "Value"=>"icehook"}]}}, "Session"=>{"IsEstablished"=>"true", "SessionId"=>"j2vtu1bk7rsdpklofplu07ocq1", "UserId"=>"212762", "Roles"=>{"Role"=>{"Name"=>"Account Admin"}}}}, "Result"=>{"SessionCreate"=>{}}}}.to_json
7
+ stub_request(:get, Regexp.new('http://api.onsip.com/api?.*OrganizationAdd.*'))
8
+ .to_return({
9
+ :body => body,
10
+ :status => 200,
11
+ :headers => { 'Content-Type' => 'application/json; charset=utf-8' }
12
+ })
13
+ OnSIP.connect('http://api.onsip.com')
14
+ end
15
+
16
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe OnSIP::UserAddress do
4
+ before :each do
5
+
6
+ body = {"Response"=>{"Context"=>{"Action"=>{"IsCompleted"=>"true"}, "Request"=>{"IsValid"=>"true", "DateTime"=>"2014-12-04T17:39:53+00:00", "Duration"=>"30", "Parameters"=>{"Parameter"=>[{"Name"=>"Action", "Value"=>"SessionCreate"}, {"Name"=>"Output", "Value"=>"json"}, {"Name"=>"Password", "Value"=>"******"}, {"Name"=>"Username", "Value"=>"icehook"}]}}, "Session"=>{"IsEstablished"=>"true", "SessionId"=>"j2vtu1bk7rsdpklofplu07ocq1", "UserId"=>"212762", "Roles"=>{"Role"=>{"Name"=>"Account Admin"}}}}, "Result"=>{"SessionCreate"=>{}}}}.to_json
7
+ stub_request(:get, Regexp.new('http://api.onsip.com/api?.*UserAddressAdd.*'))
8
+ .to_return({
9
+ :body => body,
10
+ :status => 200,
11
+ :headers => { 'Content-Type' => 'application/json; charset=utf-8' }
12
+ })
13
+ OnSIP.connect('http://api.onsip.com')
14
+ end
15
+
16
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe OnSIP::User do
4
+ before :each do
5
+
6
+ body = {"Response"=>{"Context"=>{"Action"=>{"IsCompleted"=>"true"}, "Request"=>{"IsValid"=>"true", "DateTime"=>"2014-12-04T20:17:17+00:00", "Duration"=>"46", "Parameters"=>{"Parameter"=>[{"Name"=>"Action", "Value"=>"UserRead"}, {"Name"=>"Output", "Value"=>"json"}, {"Name"=>"SessionId", "Value"=>"bvb4thhuk1d7tb2bddf09teqr4"}, {"Name"=>"UserId", "Value"=>"260161"}]}}, "Session"=>{"IsEstablished"=>"true", "SessionId"=>"bvb4thhuk1d7tb2bddf09teqr4", "UserId"=>"212762", "Roles"=>{"Role"=>{"Name"=>"Account Admin"}}}}, "Result"=>{"UserRead"=>{"User"=>{"UserId"=>"260161", "OrganizationId"=>"30096", "AccountId"=>"27370", "Flags"=>"32", "Status"=>"disabled", "Domain"=>"sip.firertc.com", "Username"=>"test3_firertc", "Password"=>"**********", "AuthUsername"=>"test3_firertc", "Modified"=>"2014-11-24T19:24:03+00:00", "Created"=>"2014-11-03T19:30:08+00:00", "Ack911"=>"1970-01-01T00:00:00+00:00", "SurveyCompleted"=>"1970-01-01T00:00:00+00:00", "AckTrunkingTerms"=>"1970-01-01T00:00:00+00:00", "AckTrunkingE911"=>"1970-01-01T00:00:00+00:00", "AckHostedTerms"=>"1970-01-01T00:00:00+00:00", "AckHostedE911"=>"1970-01-01T00:00:00+00:00", "ExtendedDialing"=>"false", "PSTNForwarding"=>"false", "E911Provisioning"=>"false", "PSTNTrunking"=>"false", "FreePSTNTrunking"=>"false", "BusyLampField"=>"true", "Contact"=>{"Name"=>"test", "Organization"=>"IceHook Systems", "Address"=>"123 Main St", "City"=>"New York", "State"=>"NY", "Zipcode"=>"10000", "Country"=>"United States of America", "CountryId"=>"207", "Phone"=>"1-212-555-1212", "Email"=>"test@icehook.com", "Modified"=>"2014-11-03T19:30:08+00:00"}, "Roles"=>{"Role"=>{"Name"=>"User"}}}}}}}.to_json
7
+ stub_request(:get, Regexp.new('http://api.onsip.com/api?.*UserRead.*'))
8
+ .to_return({
9
+ :body => body,
10
+ :status => 200,
11
+ :headers => { 'Content-Type' => 'application/json; charset=utf-8' }
12
+ })
13
+ OnSIP.connect('http://api.onsip.com')
14
+ OnSIP.session = OnSIP::Session.new("IsEstablished" => "true")
15
+ end
16
+
17
+ describe "with valid id" do
18
+ it "can be read" do
19
+ user = OnSIP::User.read('260161')
20
+ expect(user.class).to eq OnSIP::User
21
+ end
22
+
23
+ end
24
+
25
+ end
File without changes
@@ -27,3 +27,5 @@ describe OnSIP::Session do
27
27
  end
28
28
 
29
29
  end
30
+
31
+ # {"Response"=>{"Context"=>{"Action"=>{"IsCompleted"=>"false"}, "Request"=>{"IsValid"=>"true", "DateTime"=>"2014-12-04T20:07:08+00:00", "Duration"=>"8", "Parameters"=>{"Parameter"=>[{"Name"=>"Action", "Value"=>"SessionCreate"}, {"Name"=>"Output", "Value"=>"json"}, {"Name"=>"Password", "Value"=>"7onsip6"}, {"Name"=>"Username", "Value"=>"klarrimore@icehook.com"}]}, "Errors"=>{"Error"=>{"Parameter"=>"Action", "Code"=>"Accessor.LoginLocked", "Message"=>"You have failed to log in too many times. Please wait 10 minutes then try again."}}}, "Session"=>{"IsEstablished"=>"false"}}}}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: onsip
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keith Larrimore
@@ -157,6 +157,9 @@ dependencies:
157
157
  - - ">="
158
158
  - !ruby/object:Gem::Version
159
159
  version: 3.0.0
160
+ - - "<="
161
+ - !ruby/object:Gem::Version
162
+ version: 4.0.0
160
163
  type: :runtime
161
164
  prerelease: false
162
165
  version_requirements: !ruby/object:Gem::Requirement
@@ -164,6 +167,9 @@ dependencies:
164
167
  - - ">="
165
168
  - !ruby/object:Gem::Version
166
169
  version: 3.0.0
170
+ - - "<="
171
+ - !ruby/object:Gem::Version
172
+ version: 4.0.0
167
173
  - !ruby/object:Gem::Dependency
168
174
  name: trollop
169
175
  requirement: !ruby/object:Gem::Requirement
@@ -305,6 +311,13 @@ files:
305
311
  - lib/onsip/session.rb
306
312
  - lib/onsip/version.rb
307
313
  - onsip.gemspec
314
+ - spec/lib/onsip/models/account_spec.rb
315
+ - spec/lib/onsip/models/cdr_spec.rb
316
+ - spec/lib/onsip/models/external_address_spec.rb
317
+ - spec/lib/onsip/models/organization_spec.rb
318
+ - spec/lib/onsip/models/user_address_spec.rb
319
+ - spec/lib/onsip/models/user_spec.rb
320
+ - spec/lib/onsip/response_parser_spec.rb
308
321
  - spec/lib/onsip/session_spec.rb
309
322
  - spec/spec_helper.rb
310
323
  homepage: http://icehook.com
@@ -332,5 +345,12 @@ signing_key:
332
345
  specification_version: 4
333
346
  summary: OnSIP ruby client.
334
347
  test_files:
348
+ - spec/lib/onsip/models/account_spec.rb
349
+ - spec/lib/onsip/models/cdr_spec.rb
350
+ - spec/lib/onsip/models/external_address_spec.rb
351
+ - spec/lib/onsip/models/organization_spec.rb
352
+ - spec/lib/onsip/models/user_address_spec.rb
353
+ - spec/lib/onsip/models/user_spec.rb
354
+ - spec/lib/onsip/response_parser_spec.rb
335
355
  - spec/lib/onsip/session_spec.rb
336
356
  - spec/spec_helper.rb