Class: Direct7::Client
- Inherits:
-
Object
- Object
- Direct7::Client
- Defined in:
- lib/direct7/client.rb
Instance Attribute Summary collapse
-
#adapter ⇒ Object
Returns the value of attribute adapter.
-
#session ⇒ Object
Returns the value of attribute session.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
- #get(host, path, params = nil) ⇒ Object
- #host(value = nil) ⇒ Object
-
#initialize(api_token = nil, timeout = 30, pool_connections = 10, pool_maxsize = 10, max_retries = 3) ⇒ Client
constructor
A new instance of Client.
- #number_lookup ⇒ Object
- #post(host, path, body_is_json, params) ⇒ Object
- #process_response(host, response) ⇒ Object
- #slack ⇒ Object
- #sms ⇒ Object
- #verify ⇒ Object
- #viber ⇒ Object
- #whatsapp ⇒ Object
Constructor Details
#initialize(api_token = nil, timeout = 30, pool_connections = 10, pool_maxsize = 10, max_retries = 3) ⇒ Client
Returns a new instance of Client.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/direct7/client.rb', line 18 def initialize(api_token = nil, timeout = 30, pool_connections = 10, pool_maxsize = 10, max_retries = 3) @api_token = api_token @host = 'https://api.d7networks.com' user_agent = "direct7-ruby-sdk/#{Direct7::VERSION} ruby/#{RUBY_VERSION}" @headers = { 'User-Agent' => user_agent, 'Accept' => 'application/json' } def sms @sms end def verify @verify end def slack @slack end def viber @viber end def number_lookup @number_lookup end def whatsapp @whatsapp end @sms = Direct7::SMS.new(self) @verify = Direct7::VERIFY.new(self) @slack = Direct7::SLACK.new(self) @viber = Direct7::VIBER.new(self) @number_lookup = Direct7::NUMBER_LOOKUP.new(self) @whatsapp = Direct7::WHATSAPP.new(self) # ... initialize other services ... @session = HTTP.persistent(@host) @session.headers('User-Agent' => user_agent, 'Accept' => 'application/json') @session.timeout(write: timeout, connect: timeout) # @session.connect('pool_size'=> pool_connections, 'pool_max' => pool_maxsize) end |
Instance Attribute Details
#adapter ⇒ Object
Returns the value of attribute adapter.
16 17 18 |
# File 'lib/direct7/client.rb', line 16 def adapter @adapter end |
#session ⇒ Object
Returns the value of attribute session.
16 17 18 |
# File 'lib/direct7/client.rb', line 16 def session @session end |
#timeout ⇒ Object
Returns the value of attribute timeout.
16 17 18 |
# File 'lib/direct7/client.rb', line 16 def timeout @timeout end |
Instance Method Details
#get(host, path, params = nil) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/direct7/client.rb', line 103 def get(host, path, params = nil) request_url = "#{host}#{path}" request_headers = @headers.merge('Authorization' => create_bearer_token_string) # puts "GET request sent to #{request_url} with headers #{request_headers} and params #{params}" response = @session.get(URI(request_url), headers: request_headers) process_response(host, response) end |
#host(value = nil) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/direct7/client.rb', line 60 def host(value = nil) if value.nil? @host else @host = value end end |
#number_lookup ⇒ Object
39 40 41 |
# File 'lib/direct7/client.rb', line 39 def number_lookup @number_lookup end |
#post(host, path, body_is_json, params) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/direct7/client.rb', line 111 def post(host, path, body_is_json, params) request_url = "#{host}#{path}" request_headers = @headers.merge('Authorization' => create_bearer_token_string) request_headers['Content-Type'] = body_is_json ? 'application/json' : 'application/x-www-form-urlencoded' # puts "POST request sent to #{request_url} with headers #{request_headers} and params #{params}" params_json = JSON.generate(params) response = if body_is_json @session.post(URI(request_url), headers: request_headers, json: params) else @session.post(URI(request_url), data: params, headers: request_headers) end # puts response process_response(host, response) end |
#process_response(host, response) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/direct7/client.rb', line 68 def process_response(host, response) # puts "Response headers #{response}" case response.code.to_i when 401 raise AuthenticationError, 'Invalid API token' when 200..299 begin result = JSON.parse(response.body) # puts "Successful process response: #{result}" result rescue JSON::ParserError nil end when 400..499 puts "Client error: #{response.code} #{response.body.inspect}" case response.code.to_i when 400 raise BadRequest, response.body.inspect when 404 raise NotFoundError, response.body.inspect when 402 raise InsufficientCreditError, response.body.inspect when 422 raise ValidationError, response.body.inspect else raise ClientError, "#{response.code} response from #{host}" end when 500..599 puts "Server error: #{response.code} #{response.body.inspect}" raise ServerError, "#{response.code} response from #{host}" else nil end end |
#slack ⇒ Object
33 34 35 |
# File 'lib/direct7/client.rb', line 33 def slack @slack end |
#sms ⇒ Object
27 28 29 |
# File 'lib/direct7/client.rb', line 27 def sms @sms end |
#verify ⇒ Object
30 31 32 |
# File 'lib/direct7/client.rb', line 30 def verify @verify end |
#viber ⇒ Object
36 37 38 |
# File 'lib/direct7/client.rb', line 36 def viber @viber end |
#whatsapp ⇒ Object
42 43 44 |
# File 'lib/direct7/client.rb', line 42 def whatsapp @whatsapp end |