nexmo 5.0.2 → 5.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: 5167882ec4a5aa4053e97dee2d7251f6b3667b28
4
- data.tar.gz: 4fb6a105aec379f9b3dff7498bbc9b33bae38435
3
+ metadata.gz: f4155d7a6a67606fc9bc5a9d43fe530585a4d160
4
+ data.tar.gz: 4be1b6e5b4bf2043b10a4f2c238ea283c60ceb85
5
5
  SHA512:
6
- metadata.gz: 9dcfa140d4139b36a04088f6ff459708fc4cb1196c8706ba595695163ac3e39c761218da0e2dea3c7835c59b25e1fb48ff67a29f74cd031aea0b68b1c004035b
7
- data.tar.gz: 375895395424f0e5c25b8eed5c07e69e0abed1a5a465f58dd23657f09fb2bc290cacafba6c0b10984eaafc08c2ec0e88296f7c06cc1c2df216611290b4a1dbf9
6
+ metadata.gz: bd048ba74997b9900fc7ec2697478f6e5b2b6de7cebcd3849b12371e360770d2fd7f3f33939ce1af8e21a7d000ee558e71f2b44b9f139f3d576a00a6803d5067
7
+ data.tar.gz: fa30b6e1059ed7de07825e94f4e348245a3c6c007175d11fb95dc0be4519b3a682fe3adcde71c3196d6f5236db440cb9469ca3b9efd16ea79895e02fb10e401d
data/README.md CHANGED
@@ -5,6 +5,7 @@
5
5
  This is the Ruby client library for Nexmo's API. To use it you'll
6
6
  need a Nexmo account. Sign up [for free at nexmo.com][signup].
7
7
 
8
+ * [Requirements](#requirements)
8
9
  * [Installation](#installation)
9
10
  * [Usage](#usage)
10
11
  * [SMS API](#sms-api)
@@ -13,7 +14,10 @@ need a Nexmo account. Sign up [for free at nexmo.com][signup].
13
14
  * [Number Insight API](#number-insight-api)
14
15
  * [Application API](#application-api)
15
16
  * [Numbers API](#numbers-api)
16
- * [Coverage](#api-coverage)
17
+ * [Logging](#logging)
18
+ * [JWT authentication](#jwt-authentication)
19
+ * [Webhook signatures](#webhook-signatures)
20
+ * [API coverage](#api-coverage)
17
21
  * [License](#license)
18
22
 
19
23
 
@@ -353,6 +357,23 @@ client.numbers.update(country: 'GB', msisdn: '447700900000', voice_callback_type
353
357
  Docs: [https://developer.nexmo.com/api/developer/numbers#update-a-number](https://developer.nexmo.com/api/developer/numbers?utm_source=DEV_REL&utm_medium=github&utm_campaign=ruby-client-library#update-a-number)
354
358
 
355
359
 
360
+ ## Logging
361
+
362
+ Use the logger option or attribute writer method to specify a logger. For example:
363
+
364
+ ```ruby
365
+ require 'logger'
366
+
367
+ logger = Logger.new(STDOUT)
368
+
369
+ client = Nexmo::Client.new(logger: logger)
370
+ ```
371
+
372
+ By default the library sets the logger to `Rails.logger` if it is defined.
373
+
374
+ To disable logging set the logger to `nil`.
375
+
376
+
356
377
  ## JWT authentication
357
378
 
358
379
  By default the library generates a short lived JWT per request.
@@ -377,7 +398,7 @@ client.auth_token = auth_token
377
398
  ````
378
399
 
379
400
 
380
- ## Validate webhook signatures
401
+ ## Webhook signatures
381
402
 
382
403
  ```ruby
383
404
  client = Nexmo::Client.new(signature_secret: 'secret')
@@ -391,11 +412,10 @@ end
391
412
 
392
413
  Docs: [https://developer.nexmo.com/concepts/guides/signing-messages](https://developer.nexmo.com/concepts/guides/signing-messages?utm_source=DEV_REL&utm_medium=github&utm_campaign=ruby-client-library)
393
414
 
394
- Note: you'll need to contact support@nexmo.com to enable message signing on
395
- your account before you can validate webhook signatures.
415
+ Note: you'll need to contact support@nexmo.com to enable message signing on your account.
396
416
 
397
417
 
398
- ## API Coverage
418
+ ## API coverage
399
419
 
400
420
  * Account
401
421
  * [X] Balance
data/lib/nexmo.rb CHANGED
@@ -9,6 +9,7 @@ require 'nexmo/errors/error'
9
9
  require 'nexmo/errors/client_error'
10
10
  require 'nexmo/errors/server_error'
11
11
  require 'nexmo/errors/authentication_error'
12
+ require 'nexmo/key_value_logger'
12
13
  require 'nexmo/namespace'
13
14
  require 'nexmo/client'
14
15
  require 'nexmo/account'
data/lib/nexmo/client.rb CHANGED
@@ -2,7 +2,13 @@
2
2
 
3
3
  module Nexmo
4
4
  class Client
5
- attr_accessor :auth_token, :user_agent
5
+ attr_writer :api_key
6
+ attr_writer :api_secret
7
+ attr_writer :signature_secret
8
+ attr_writer :application_id
9
+ attr_writer :private_key
10
+ attr_accessor :user_agent
11
+ attr_accessor :auth_token
6
12
 
7
13
  def initialize(options = {})
8
14
  @api_key = options[:api_key] || ENV['NEXMO_API_KEY']
@@ -15,11 +21,17 @@ module Nexmo
15
21
 
16
22
  @private_key = options[:private_key]
17
23
 
18
- @host = options.fetch(:host) { 'rest.nexmo.com' }
24
+ @user_agent = UserAgent.string(options[:app_name], options[:app_version])
19
25
 
20
- @api_host = options.fetch(:api_host) { 'api.nexmo.com' }
26
+ self.logger = options[:logger] || (defined?(Rails.logger) && Rails.logger)
27
+ end
21
28
 
22
- @user_agent = UserAgent.string(options[:app_name], options[:app_version])
29
+ def logger
30
+ @logger
31
+ end
32
+
33
+ def logger=(logger)
34
+ @logger = Nexmo::KeyValueLogger.new(logger)
23
35
  end
24
36
 
25
37
  def authorization
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nexmo
4
+ class KeyValueLogger
5
+ def initialize(logger)
6
+ @logger = logger
7
+ end
8
+
9
+ def debug(message, data = nil)
10
+ @logger.debug(format(message, data)) if @logger
11
+ end
12
+
13
+ def info(message, data = nil)
14
+ @logger.info(format(message, data)) if @logger
15
+ end
16
+
17
+ def warning(message, data = nil)
18
+ @logger.warning(format(message, data)) if @logger
19
+ end
20
+
21
+ def error(message, data = nil)
22
+ @logger.error(format(message, data)) if @logger
23
+ end
24
+
25
+ private
26
+
27
+ def format(message, hash)
28
+ return message if hash.nil?
29
+
30
+ fields = hash.map { |key, value| "#{key}=#{value}" if value }.compact
31
+ fields.unshift(message)
32
+ fields.join(' ')
33
+ end
34
+ end
35
+ end
@@ -5,6 +5,9 @@ module Nexmo
5
5
  class Namespace
6
6
  def initialize(client)
7
7
  @client = client
8
+
9
+ @http = Net::HTTP.new(host, Net::HTTP.https_default_port)
10
+ @http.use_ssl = true
8
11
  end
9
12
 
10
13
  private
@@ -26,6 +29,10 @@ module Nexmo
26
29
  false
27
30
  end
28
31
 
32
+ def logger
33
+ @client.logger
34
+ end
35
+
29
36
  def request(path, params: nil, type: Get, &block)
30
37
  uri = URI('https://' + host + path)
31
38
 
@@ -40,41 +47,42 @@ module Nexmo
40
47
  end
41
48
 
42
49
  message = type.new(uri.request_uri)
43
-
44
- if type::REQUEST_HAS_BODY
45
- if json_body?
46
- message['Content-Type'] = 'application/json'
47
- message.body = JSON.generate(params)
48
- else
49
- message.form_data = params
50
- end
51
- end
52
-
53
50
  message['Authorization'] = @client.authorization if authorization_header?
54
51
  message['User-Agent'] = @client.user_agent
55
52
 
56
- http = Net::HTTP.new(uri.host, Net::HTTP.https_default_port)
57
- http.use_ssl = true
53
+ encode_body(params, message) if type::REQUEST_HAS_BODY
58
54
 
59
- response = http.request(message)
55
+ logger.info('Nexmo API request', method: message.method, path: uri.path)
56
+
57
+ response = @http.request(message)
60
58
 
61
59
  parse(response, &block)
62
60
  end
63
61
 
62
+ def encode_body(params, message)
63
+ if json_body?
64
+ message['Content-Type'] = 'application/json'
65
+ message.body = JSON.generate(params)
66
+ else
67
+ message.form_data = params
68
+ end
69
+ end
70
+
64
71
  def parse(response, &block)
72
+ logger.info('Nexmo API response',
73
+ host: host,
74
+ status: response.code,
75
+ type: response.content_type,
76
+ length: response.content_length,
77
+ trace_id: response['x-nexmo-trace-id'])
78
+
65
79
  case response
66
80
  when Net::HTTPNoContent
67
81
  :no_content
68
82
  when Net::HTTPSuccess
69
83
  parse_success(response, &block)
70
- when Net::HTTPUnauthorized
71
- raise AuthenticationError, "#{response.code} response from #{host}"
72
- when Net::HTTPClientError
73
- raise ClientError, "#{response.code} response from #{host}"
74
- when Net::HTTPServerError
75
- raise ServerError, "#{response.code} response from #{host}"
76
84
  else
77
- raise Error, "#{response.code} response from #{host}"
85
+ handle_error(response)
78
86
  end
79
87
  end
80
88
 
@@ -87,5 +95,20 @@ module Nexmo
87
95
  response.body
88
96
  end
89
97
  end
98
+
99
+ def handle_error(response)
100
+ logger.debug(response.body)
101
+
102
+ case response
103
+ when Net::HTTPUnauthorized
104
+ raise AuthenticationError
105
+ when Net::HTTPClientError
106
+ raise ClientError
107
+ when Net::HTTPServerError
108
+ raise ServerError
109
+ else
110
+ raise Error
111
+ end
112
+ end
90
113
  end
91
114
  end
data/lib/nexmo/pricing.rb CHANGED
@@ -5,7 +5,9 @@ module Nexmo
5
5
  def initialize(client, type: nil)
6
6
  raise ArgumentError if type.nil?
7
7
 
8
- @client, @type = client, type
8
+ @type = type
9
+
10
+ super client
9
11
  end
10
12
 
11
13
  attr_reader :type
data/lib/nexmo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Nexmo
2
- VERSION = '5.0.2'
2
+ VERSION = '5.1.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nexmo
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.2
4
+ version: 5.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Craft
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-26 00:00:00.000000000 Z
11
+ date: 2018-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jwt
@@ -92,6 +92,7 @@ files:
92
92
  - lib/nexmo/errors/server_error.rb
93
93
  - lib/nexmo/files.rb
94
94
  - lib/nexmo/jwt.rb
95
+ - lib/nexmo/key_value_logger.rb
95
96
  - lib/nexmo/keys.rb
96
97
  - lib/nexmo/messages.rb
97
98
  - lib/nexmo/namespace.rb
@@ -126,7 +127,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
126
127
  version: '0'
127
128
  requirements: []
128
129
  rubyforge_project:
129
- rubygems_version: 2.6.13
130
+ rubygems_version: 2.6.14.1
130
131
  signing_key:
131
132
  specification_version: 4
132
133
  summary: This is the Ruby client library for Nexmo's API. To use it you'll need a