zadarma 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0f879aece24dd3ff14b74b194c08d58aafe42d6c
4
+ data.tar.gz: 20a8af9e3a0af119d8f2ba1ed8e66af9972fd3c8
5
+ SHA512:
6
+ metadata.gz: 3611229d9bfa0b47b01f2046dcad1ff2d8907dfd1b71802773d703598bead60ed729226178872e5865da5025be87bb708457f1227105447cb39dffff441aac37
7
+ data.tar.gz: ba9b991b6d697ddc2f853ac944603f352ae5b0792f3101708b30b269db8c9542ad809889aa24e37a093ab3ba5763b59c9dafc549b622504e16e98fffd5f20628
@@ -0,0 +1,4 @@
1
+ Gemfile.lock
2
+ *.sublime-*
3
+ .DS_Store
4
+ pkg
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Eugeniy Belyaev
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,46 @@
1
+ # Zadarma API
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ gem 'zadarma'
8
+
9
+ And then execute:
10
+
11
+ $ bundle
12
+
13
+ Or install it yourself as:
14
+
15
+ $ gem install zadarma
16
+
17
+ ## Usage
18
+
19
+ Zadarma.api_key = "YOUR_API_KEY"
20
+ Zadarma.api_secret = "YOUR_API_SECRET"
21
+ Zadarma.log_requests = false # default
22
+
23
+ Zadarma::Client.balance
24
+
25
+ ## Available methods:
26
+
27
+ * `balance` - user balance
28
+ * `price(number)` - call price
29
+ * `callback(from, to, params = {})` - request callback
30
+ * `sip` - list user’s SIP-numbers
31
+ * `set_sip_caller(id, number)` - change of CallerID
32
+ * `redirection(params = {})` - get call forwarding status on SIP-numbers
33
+ * `set_redirect(id, params)` - enable/disable sip forwarding
34
+ * `pbx_internal` - list PBX internal numbers
35
+ * `pbx_record(id, status, params = {})` - toggle call recording
36
+ * `send_sms(number, message, params = {})` - send sms
37
+ * `statistics(date_start, date_end, params = {})` - get stats
38
+ * `pbx_statistics(date_start, date_end)` - get PBX stats
39
+
40
+ ## Contributing
41
+
42
+ 1. Fork it ( https://github.com/zhekanax/zadarma-ruby/fork )
43
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
44
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
45
+ 4. Push to the branch (`git push origin my-new-feature`)
46
+ 5. Create a new Pull Request
@@ -0,0 +1,12 @@
1
+ require "active_support"
2
+ require "active_support/core_ext"
3
+ require "active_support/json"
4
+ require "active_support/hash_with_indifferent_access"
5
+
6
+ require "zadarma/methods.rb"
7
+ require "zadarma/client.rb"
8
+ require "zadarma/error.rb"
9
+
10
+ module Zadarma
11
+ mattr_accessor :api_key, :api_secret, :log_requests
12
+ end
@@ -0,0 +1,51 @@
1
+ require "digest/md5"
2
+ require "openssl"
3
+ require "base64"
4
+ require "faraday"
5
+
6
+ module Zadarma
7
+ class Client
8
+ class << self
9
+ include Methods
10
+
11
+ def request(method, path, params = {})
12
+ raise "No auth data provided" unless Zadarma.api_key && Zadarma.api_secret
13
+
14
+ params.merge!(format: "json")
15
+
16
+ response = client.send(method, "/v1" + path) do |request|
17
+ request.params = params
18
+ request.headers["Accept"] = "application/json"
19
+ request.headers["Authorization"] = "#{Zadarma.api_key}:#{signature("/v1" + path, params)}"
20
+ end
21
+
22
+ result = ActiveSupport::JSON.decode(response.body).with_indifferent_access
23
+ raise Zadarma::Error.new("Error [HTTP #{response.status}]: #{result[:message]} ") unless "success" == result[:status]
24
+
25
+ result.except("status")
26
+
27
+ rescue ActiveSupport::JSON.parse_error
28
+ raise Zadarma::Error.new("Response is not JSON: #{response.body}")
29
+
30
+ end
31
+
32
+
33
+ protected
34
+
35
+ def client
36
+ @client ||= ::Faraday.new(url: "https://api.zadarma.com") do |faraday|
37
+ faraday.request :url_encoded
38
+ faraday.response :logger if Zadarma.log_requests
39
+ faraday.adapter Faraday.default_adapter
40
+ end
41
+ end
42
+
43
+ def signature(url, params)
44
+ query = Hash[params.sort].to_query
45
+ sign_str = url + query + Digest::MD5.hexdigest(query)
46
+ sign = Base64.encode64(OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha1"), Zadarma.api_secret, sign_str)).strip
47
+ sign
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,4 @@
1
+ module Zadarma
2
+ class Error < StandardError
3
+ end
4
+ end
@@ -0,0 +1,66 @@
1
+ module Zadarma
2
+ module Methods
3
+ def balance
4
+ request :get, "/info/balance/"
5
+ end
6
+
7
+ def price(number)
8
+ request :get, "/info/price/", number: number
9
+ end
10
+
11
+ def callback(from, to, params = {})
12
+ request :get, "/request/callback/", params.merge(from: from, to: to)
13
+ end
14
+
15
+ def sip
16
+ request :get, "/sip/"
17
+ end
18
+
19
+ def set_sip_caller(id, number)
20
+ request :put, "/sip/callerid/", id: id, number: number
21
+ end
22
+
23
+ def redirection(params = {})
24
+ request :get, "/sip/redirection/", params
25
+ end
26
+
27
+ def set_redirect(id, params)
28
+ request :put, "/sip/redirection/", params.merge(id: id)
29
+ end
30
+
31
+
32
+ def pbx_internal
33
+ request :get, "/pbx/internal/"
34
+ end
35
+
36
+ def pbx_record(id, status, params = {})
37
+ request :put, "/pbx/internal/recording/", params.merge(id: id, status: status)
38
+ end
39
+
40
+ def send_sms(number, message, params = {})
41
+ request :post, "/sms/send/", params.merge(number: number, message: message)
42
+ end
43
+
44
+
45
+ def statistics(time_start, time_end, params = {})
46
+ result = request :get, "/statistics/", params.merge(start: time_s(time_start), end: time_s(time_end))
47
+
48
+ result[:start] = Time.parse(result[:start])
49
+ result[:end] = Time.parse(result[:end])
50
+ result[:stats].each { |item| item[:callstart] = Time.parse(item[:callstart]) }
51
+
52
+ result
53
+ end
54
+
55
+ def pbx_statistics(time_start, time_end)
56
+ request :get, "/statistics/pbx/", start: time_s(time_start), end: time_s(time_end)
57
+ end
58
+
59
+
60
+ protected
61
+
62
+ def time_s(value)
63
+ value.to_time.strftime "%Y-%m-%d %H:%M:%S" if value.present?
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,19 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "zadarma"
3
+ s.version = "0.0.1"
4
+ s.date = "2015-10-07"
5
+ s.summary = "zadarma.com api ruby interface"
6
+ s.description = ""
7
+ s.authors = ["Eugeniy Belyaev"]
8
+ s.email = "eugeniy.b@gmail.com"
9
+ s.files = `git ls-files -z`.split("\x0")
10
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
11
+ s.require_paths = ["lib"]
12
+ s.homepage = "https://github.com/zhekanax/zadarma-ruby"
13
+ s.license = "MIT"
14
+
15
+
16
+ s.add_dependency "json"
17
+ s.add_dependency "faraday"
18
+ s.add_dependency "activesupport"
19
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zadarma
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Eugeniy Belyaev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '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'
55
+ description: ''
56
+ email: eugeniy.b@gmail.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - ".gitignore"
62
+ - Gemfile
63
+ - LICENSE.txt
64
+ - README.md
65
+ - lib/zadarma.rb
66
+ - lib/zadarma/client.rb
67
+ - lib/zadarma/error.rb
68
+ - lib/zadarma/methods.rb
69
+ - zadarma.gemspec
70
+ homepage: https://github.com/zhekanax/zadarma-ruby
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.4.5.1
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: zadarma.com api ruby interface
94
+ test_files: []