Skip to content

Commit

Permalink
Merge pull request #239 from plivo/VT-7574
Browse files Browse the repository at this point in the history
maskingSession
  • Loading branch information
manjunath-plivo authored May 31, 2024
2 parents f66fb42 + 5bfc00c commit bb9554c
Show file tree
Hide file tree
Showing 11 changed files with 401 additions and 37 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
# Change Log

## [4.59.0)](https://github.com/plivo/plivo-go/tree/v4.59.0) (2024-05-31)
**Feature - SubAccount and GeoMatch**
- Added sub_account and geo_match support

## [4.58.0](https://github.com/plivo/plivo-ruby/tree/v4.58.0) (2023-05-17)
**Feature - Adding support for location whatsapp messages**
- Added new param `location` to [send message API](https://www.plivo.com/docs/sms/api/message#send-a-message) to support location `whatsapp` messages
- Added new param `location` in templates to support location based templated messages
-

## [4.57.0](https://github.com/plivo/plivo-ruby/tree/v4.57.0) (2023-05-07)
**Feature - Adding support for interactive whatsapp messages**
- Added new param `interactive` to [send message API](https://www.plivo.com/docs/sms/api/message#send-a-message) to support interactive `whatsapp` messages


## [4.56.0](https://github.com/plivo/plivo-ruby/tree/v4.56.0) (2023-04-18)
**Feature - Support for dynamic button components when sending a templated WhatsApp message**
- Added new param `payload` in templates to support dynamic payload in templates
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The Plivo Ruby SDK makes it simpler to integrate communications into your Ruby a
Add this line to your application's Gemfile:

```ruby
gem 'plivo', '>= 4.58.0'
gem 'plivo', '>= 4.59.0'
```

And then execute:
Expand Down
2 changes: 1 addition & 1 deletion lib/plivo/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ module Base
PHLO_API_URL = 'https://phlorunner.plivo.com'.freeze
LOOKUP_API_URL = 'https://lookup.plivo.com'.freeze
end
end
end
49 changes: 45 additions & 4 deletions lib/plivo/base/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,39 @@ def parse_and_set(resource_json)
@id = resource_json[@_identifier_string]
end

def set_instance_variables(hash)
hash.each do |k, v|
instance_var_name = "@#{k}"

if v.is_a?(Hash)
instance_variable_set(instance_var_name, v)
self.class.send(:attr_reader, k.to_sym)
v.each do |nested_k, nested_v|
instance_var_name = "@#{nested_k}"
instance_variable_set(instance_var_name, nested_v)
self.class.send(:attr_reader, nested_k.to_sym)
end
else
instance_variable_set(instance_var_name, v)
self.class.send(:attr_reader, k.to_sym)
end
end
end

def parse_and_set_response(resource_json)
return unless resource_json.is_a?(Hash)

set_instance_variables(resource_json)

if @_identifier_string && resource_json.key?(@_identifier_string)
@id = resource_json[@_identifier_string]
end
end

def perform_update(params, use_multipart_conn = false)
unless @id
raise_invalid_request("Cannot update a #{@_name} resource "\
'without an identifier')
'without an identifier')
end

response_json = @_client.send_request(@_resource_uri, 'POST', params, nil, use_multipart_conn, is_voice_request: @_is_voice_request)
Expand All @@ -64,14 +93,26 @@ def perform_update(params, use_multipart_conn = false)
self
end

def perform_masking_update(params, use_multipart_conn = false)
unless @id
raise_invalid_request("Cannot update a #{@_name} resource "\
'without an identifier')
end

response_json = @_client.send_request(@_resource_uri, 'POST', params, nil, use_multipart_conn, is_voice_request: @_is_voice_request)
parse_and_set(params)
parse_and_set_response(response_json)
self
end

def perform_action(action = nil, method = 'GET', params = nil, parse = false)
resource_path = action ? @_resource_uri + action + '/' : @_resource_uri
response = @_client.send_request(resource_path, method, params,nil,false,is_voice_request: @_is_voice_request)
parse ? parse_and_set(response) : self
method == 'POST' ? parse_and_set(params) : self
self
end

def perform_custome_action(action = nil, method = 'GET', params = nil, parse = false)
resource_path = action ? @_resource_uri + action + '/' : @_resource_uri
response = @_client.send_request(resource_path, method, params,nil,false,is_voice_request: @_is_voice_request)
Expand Down Expand Up @@ -101,7 +142,7 @@ def perform_custom_action_apiresponse(action = nil, method = 'GET', params = nil
def perform_delete(params=nil)
unless @id
raise_invalid_request("Cannot delete a #{@_name} resource "\
'without an identifier')
'without an identifier')
end

Response.new(@_client.send_request(@_resource_uri, 'DELETE', params, nil, false, is_voice_request: @_is_voice_request),
Expand Down Expand Up @@ -145,4 +186,4 @@ def configure_secondary_resource_uri
end
end
end
end
end
12 changes: 12 additions & 0 deletions lib/plivo/base/resource_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ def perform_get(identifier, params = nil)
@_resource_type.new(@_client, resource_json: response_json)
end

def perform_get_with_response(identifier, params = nil)
valid_param?(:identifier, identifier, [String, Symbol], true)
response_json = @_client.send_request(@_resource_uri + identifier.to_s + '/', 'GET', params, nil, false, is_voice_request: @_is_voice_request)
resource_json = response_json["response"]
# Pass the parsed JSON to initialize the resource object
@_resource_type.new(@_client, resource_json: resource_json)
end

def perform_get_without_identifier(params)
valid_param?(:params, params, Hash, true)
response_json = @_client.send_request(@_resource_uri, 'GET', params, nil, false, is_voice_request: @_is_voice_request)
Expand Down Expand Up @@ -98,6 +106,10 @@ def perform_list(params = nil)
}
end

def perform_list_with_response(params = nil)
@_client.send_request(@_resource_uri, 'GET', params, nil, false, is_voice_request: @_is_voice_request)
end

def perform_action(action = nil, method = 'GET', params = nil, parse = false)
resource_path = action ? @_resource_uri + action + '/' : @_resource_uri
response = @_client.send_request(resource_path, method, params, nil, false, is_voice_request: @_is_voice_request)
Expand Down
64 changes: 34 additions & 30 deletions lib/plivo/base_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -343,41 +343,41 @@ def send_delete(resource_path, data, timeout, options = nil)

def handle_response_exceptions(response)
exception_mapping = {
400 => [
Exceptions::ValidationError,
'A parameter is missing or is invalid while accessing resource'
],
401 => [
Exceptions::AuthenticationError,
'Failed to authenticate while accessing resource'
],
404 => [
Exceptions::ResourceNotFoundError,
'Resource not found'
],
405 => [
Exceptions::InvalidRequestError,
'HTTP method used is not allowed to access resource'
],
409 => [
Exceptions::InvalidRequestError,
'Conflict'
],
422 => [
Exceptions::InvalidRequestError,
'Unprocessable Entity'
],
500 => [
Exceptions::PlivoServerError,
'A server error occurred while accessing resource'
]
400 => [
Exceptions::ValidationError,
'A parameter is missing or is invalid while accessing resource'
],
401 => [
Exceptions::AuthenticationError,
'Failed to authenticate while accessing resource'
],
404 => [
Exceptions::ResourceNotFoundError,
'Resource not found'
],
405 => [
Exceptions::InvalidRequestError,
'HTTP method used is not allowed to access resource'
],
409 => [
Exceptions::InvalidRequestError,
'Conflict'
],
422 => [
Exceptions::InvalidRequestError,
'Unprocessable Entity'
],
500 => [
Exceptions::PlivoServerError,
'A server error occurred while accessing resource'
]
}

response_json = response[:body]
return unless exception_mapping.key? response[:status]
return unless exception_mapping.key?(response[:status])

exception_now = exception_mapping[response[:status]]
error_message = if (response_json.is_a? Hash) && (response_json.key? 'error')
error_message = if response_json.is_a?(Hash) && response_json.key?('error')
response_json['error']
else
exception_now[1] + " at: #{response[:url]}"
Expand All @@ -386,6 +386,10 @@ def handle_response_exceptions(response)
error_message = error_message['error']
end

# Add api_id to the error message if present
if response_json.is_a?(Hash) && response_json.key?('api_id')
error_message += " (api_id: #{response_json['api_id']})"
end
raise exception_now[0], error_message.to_s
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/plivo/resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require_relative 'resources/calls'
require_relative 'resources/token'
require_relative 'resources/endpoints'
require_relative 'resources/maskingsession'
require_relative 'resources/addresses'
require_relative 'resources/identities'
require_relative 'resources/phlos'
Expand Down
Loading

0 comments on commit bb9554c

Please sign in to comment.