messagemedia_webhooks_sdk 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +28 -0
  3. data/README.md +132 -0
  4. data/lib/message_media_webhooks.rb +39 -0
  5. data/lib/message_media_webhooks/api_helper.rb +272 -0
  6. data/lib/message_media_webhooks/configuration.rb +28 -0
  7. data/lib/message_media_webhooks/controllers/base_controller.rb +55 -0
  8. data/lib/message_media_webhooks/controllers/webhooks_controller.rb +369 -0
  9. data/lib/message_media_webhooks/exceptions/api_exception.rb +16 -0
  10. data/lib/message_media_webhooks/exceptions/create_webhook_400_response_exception.rb +25 -0
  11. data/lib/message_media_webhooks/exceptions/retrieve_webhook_400_response_exception.rb +26 -0
  12. data/lib/message_media_webhooks/exceptions/update_webhook_400_response_exception.rb +25 -0
  13. data/lib/message_media_webhooks/http/auth/basic_auth.rb +20 -0
  14. data/lib/message_media_webhooks/http/faraday_client.rb +54 -0
  15. data/lib/message_media_webhooks/http/http_call_back.rb +20 -0
  16. data/lib/message_media_webhooks/http/http_client.rb +90 -0
  17. data/lib/message_media_webhooks/http/http_context.rb +16 -0
  18. data/lib/message_media_webhooks/http/http_method_enum.rb +9 -0
  19. data/lib/message_media_webhooks/http/http_request.rb +46 -0
  20. data/lib/message_media_webhooks/http/http_response.rb +19 -0
  21. data/lib/message_media_webhooks/message_media_webhooks_client.rb +26 -0
  22. data/lib/message_media_webhooks/models/base_model.rb +32 -0
  23. data/lib/message_media_webhooks/models/create_webhook_request.rb +76 -0
  24. data/lib/message_media_webhooks/models/update_webhook_request.rb +67 -0
  25. data/test/controllers/controller_test_base.rb +30 -0
  26. data/test/controllers/test_webhooks_controller.rb +56 -0
  27. data/test/http_response_catcher.rb +16 -0
  28. data/test/test_helper.rb +91 -0
  29. metadata +155 -0
@@ -0,0 +1,16 @@
1
+
2
+ module MessageMediaWebhooks
3
+ # Class for exceptions when there is a network error, status code error, etc.
4
+ class APIException < StandardError
5
+ attr_reader :context, :response_code
6
+
7
+ # The constructor.
8
+ # @param [String] The reason for raising an exception.
9
+ # @param [HttpContext] The HttpContext of the API call.
10
+ def initialize(reason, context)
11
+ super(reason)
12
+ @context = context
13
+ @response_code = context.response.status_code
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,25 @@
1
+
2
+ module MessageMediaWebhooks
3
+ # Create Webhook 400 response class.
4
+ class CreateWebhook400ResponseException < APIException
5
+ # TODO: Write general description for this method
6
+ # @return [String]
7
+ attr_accessor :message
8
+
9
+ # The constructor.
10
+ # @param [String] The reason for raising an exception.
11
+ # @param [HttpContext] The HttpContext of the API call.
12
+ def initialize(reason, context)
13
+ super(reason, context)
14
+ hash = APIHelper.json_deserialize(@context.response.raw_body)
15
+ unbox(hash)
16
+ end
17
+
18
+ # Populates this object by extracting properties from a hash.
19
+ # @param [Hash] The deserialized response sent by the server in the
20
+ # response body.
21
+ def unbox(hash)
22
+ @message = hash['message']
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,26 @@
1
+
2
+
3
+ module MessageMediaWebhooks
4
+ # Retrieve Webhook 400 response class.
5
+ class RetrieveWebhook400ResponseException < APIException
6
+ # TODO: Write general description for this method
7
+ # @return [String]
8
+ attr_accessor :message
9
+
10
+ # The constructor.
11
+ # @param [String] The reason for raising an exception.
12
+ # @param [HttpContext] The HttpContext of the API call.
13
+ def initialize(reason, context)
14
+ super(reason, context)
15
+ hash = APIHelper.json_deserialize(@context.response.raw_body)
16
+ unbox(hash)
17
+ end
18
+
19
+ # Populates this object by extracting properties from a hash.
20
+ # @param [Hash] The deserialized response sent by the server in the
21
+ # response body.
22
+ def unbox(hash)
23
+ @message = hash['message']
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,25 @@
1
+
2
+ module MessageMediaWebhooks
3
+ # Update Webhook 400 response class.
4
+ class UpdateWebhook400ResponseException < APIException
5
+ # TODO: Write general description for this method
6
+ # @return [String]
7
+ attr_accessor :message
8
+
9
+ # The constructor.
10
+ # @param [String] The reason for raising an exception.
11
+ # @param [HttpContext] The HttpContext of the API call.
12
+ def initialize(reason, context)
13
+ super(reason, context)
14
+ hash = APIHelper.json_deserialize(@context.response.raw_body)
15
+ unbox(hash)
16
+ end
17
+
18
+ # Populates this object by extracting properties from a hash.
19
+ # @param [Hash] The deserialized response sent by the server in the
20
+ # response body.
21
+ def unbox(hash)
22
+ @message = hash['message']
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,20 @@
1
+ # This file was automatically generated for MessageMedia by APIMATIC v2.0
2
+ # ( https://apimatic.io ).
3
+
4
+ require 'base64'
5
+
6
+ module MessageMediaWebhooks
7
+ # Utility class for basic authorization.
8
+ class BasicAuth
9
+ # Add basic authentication to the request.
10
+ # @param [HttpRequest] The HttpRequest object to which authentication will
11
+ # be added.
12
+ def self.apply(http_request)
13
+ username = Configuration.basic_auth_user_name
14
+ password = Configuration.basic_auth_password
15
+ value = Base64.strict_encode64("#{username}:#{password}")
16
+ header_value = "Basic #{value}"
17
+ http_request.headers['Authorization'] = header_value
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,54 @@
1
+
2
+ require 'faraday/http_cache'
3
+
4
+ module MessageMediaWebhooks
5
+ # An implementation of HttpClient.
6
+ class FaradayClient < HttpClient
7
+ # The constructor.
8
+ def initialize(timeout: nil, cache: false,
9
+ max_retries: nil, retry_interval: nil)
10
+ @connection = Faraday.new do |faraday|
11
+ faraday.use Faraday::HttpCache, serializer: Marshal if cache
12
+ faraday.request :multipart
13
+ faraday.request :url_encoded
14
+ faraday.ssl[:ca_file] = Certifi.where
15
+ faraday.adapter Faraday.default_adapter
16
+ faraday.options[:params_encoder] = Faraday::FlatParamsEncoder
17
+ faraday.options[:open_timeout] = timeout if timeout
18
+ faraday.request :retry, max: max_retries, interval: if max_retries &&
19
+ retry_interval
20
+ retry_interval
21
+ end
22
+ end
23
+ end
24
+
25
+ # Method overridden from HttpClient.
26
+ def execute_as_string(http_request)
27
+ response = @connection.send(
28
+ http_request.http_method.downcase,
29
+ http_request.query_url
30
+ ) do |request|
31
+ request.headers = http_request.headers
32
+ request.body = http_request.parameters
33
+ end
34
+ convert_response(response)
35
+ end
36
+
37
+ # Method overridden from HttpClient.
38
+ def execute_as_binary(http_request)
39
+ response = @connection.send(
40
+ http_request.http_method.downcase,
41
+ http_request.query_url
42
+ ) do |request|
43
+ request.headers = http_request.headers
44
+ request.body = http_request.parameters
45
+ end
46
+ convert_response(response)
47
+ end
48
+
49
+ # Method overridden from HttpClient.
50
+ def convert_response(response)
51
+ HttpResponse.new(response.status, response.headers, response.body)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,20 @@
1
+
2
+ module MessageMediaWebhooks
3
+ # HttpCallBack allows defining callables for pre and post API calls.
4
+ class HttpCallBack
5
+ # A controller will call this method before making an HTTP Request.
6
+ # @param [HttpRequest] The HttpRequest object which the HttpClient
7
+ # will execute.
8
+ def on_before_request(_http_request)
9
+ raise NotImplementedError, 'This method needs
10
+ to be implemented in a child class.'
11
+ end
12
+
13
+ # A controller will call this method after making an HTTP Request.
14
+ # @param [HttpContext] The HttpContext of the API call.
15
+ def on_after_response(_http_context)
16
+ raise NotImplementedError, 'This method needs
17
+ to be implemented in a child class.'
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,90 @@
1
+
2
+ module MessageMediaWebhooks
3
+ # An interface for the methods that an HTTP Client must implement.
4
+ #
5
+ # This class should not be instantiated but should be used as a base class
6
+ # for HTTP Client classes.
7
+ class HttpClient
8
+ # Execute an HttpRequest when the response is expected to be a string.
9
+ # @param [HttpRequest] The HttpRequest to be executed.
10
+ def execute_as_string(_http_request)
11
+ raise NotImplementedError, 'This method needs
12
+ to be implemented in a child class.'
13
+ end
14
+
15
+ # Execute an HttpRequest when the response is expected to be binary.
16
+ # @param [HttpRequest] The HttpRequest to be executed.
17
+ def execute_as_binary(_http_request)
18
+ raise NotImplementedError, 'This method needs
19
+ to be implemented in a child class.'
20
+ end
21
+
22
+ # Converts the HTTP Response from the client to an HttpResponse object.
23
+ # @param [Dynamic] The response object received from the client.
24
+ def convert_response(_response)
25
+ raise NotImplementedError, 'This method needs
26
+ to be implemented in a child class.'
27
+ end
28
+
29
+ # Get a GET HttpRequest object.
30
+ # @param [String] The URL to send the request to.
31
+ # @param [Hash, Optional] The headers for the HTTP Request.
32
+ def get(query_url,
33
+ headers: {})
34
+ HttpRequest.new(HttpMethodEnum::GET,
35
+ query_url,
36
+ headers: headers)
37
+ end
38
+
39
+ # Get a POST HttpRequest object.
40
+ # @param [String] The URL to send the request to.
41
+ # @param [Hash, Optional] The headers for the HTTP Request.
42
+ # @param [Hash, Optional] The parameters for the HTTP Request.
43
+ def post(query_url,
44
+ headers: {},
45
+ parameters: {})
46
+ HttpRequest.new(HttpMethodEnum::POST,
47
+ query_url,
48
+ headers: headers,
49
+ parameters: parameters)
50
+ end
51
+
52
+ # Get a PUT HttpRequest object.
53
+ # @param [String] The URL to send the request to.
54
+ # @param [Hash, Optional] The headers for the HTTP Request.
55
+ # @param [Hash, Optional] The parameters for the HTTP Request.
56
+ def put(query_url,
57
+ headers: {},
58
+ parameters: {})
59
+ HttpRequest.new(HttpMethodEnum::PUT,
60
+ query_url,
61
+ headers: headers,
62
+ parameters: parameters)
63
+ end
64
+
65
+ # Get a PATCH HttpRequest object.
66
+ # @param [String] The URL to send the request to.
67
+ # @param [Hash, Optional] The headers for the HTTP Request.
68
+ # @param [Hash, Optional] The parameters for the HTTP Request.
69
+ def patch(query_url,
70
+ headers: {},
71
+ parameters: {})
72
+ HttpRequest.new(HttpMethodEnum::PATCH,
73
+ query_url,
74
+ headers: headers,
75
+ parameters: parameters)
76
+ end
77
+
78
+ # Get a DELETE HttpRequest object.
79
+ # @param [String] The URL to send the request to.
80
+ # @param [Hash, Optional] The headers for the HTTP Request.
81
+ def delete(query_url,
82
+ headers: {},
83
+ parameters: {})
84
+ HttpRequest.new(HttpMethodEnum::DELETE,
85
+ query_url,
86
+ headers: headers,
87
+ parameters: parameters)
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,16 @@
1
+
2
+ module MessageMediaWebhooks
3
+ # Represents an Http call in context.
4
+ class HttpContext
5
+ attr_accessor :request, :response
6
+
7
+ # The constructor.
8
+ # @param [HttpRequest] An HttpRequest object representing the HTTP request.
9
+ # @param [HttpResponse] An HttpResponse object representing the HTTP
10
+ # response.
11
+ def initialize(request, response)
12
+ @request = request
13
+ @response = response
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,9 @@
1
+
2
+ module MessageMediaWebhooks
3
+ # HTTP Methods Enumeration.
4
+ class HttpMethodEnum
5
+ HTTPMETHODENUM = [GET = 'GET'.freeze, POST = 'POST'.freeze,
6
+ PUT = 'PUT'.freeze, PATCH = 'PATCH'.freeze,
7
+ DELETE = 'DELETE'.freeze].freeze
8
+ end
9
+ end
@@ -0,0 +1,46 @@
1
+
2
+ module MessageMediaWebhooks
3
+ # Represents a single Http Request.
4
+ class HttpRequest
5
+ attr_accessor :http_method, :query_url, :headers,
6
+ :parameters, :username, :password
7
+
8
+ # The constructor.
9
+ # @param [HttpMethodEnum] The HTTP method.
10
+ # @param [String] The URL to send the request to.
11
+ # @param [Hash, Optional] The headers for the HTTP Request.
12
+ # @param [Hash, Optional] The parameters for the HTTP Request.
13
+ def initialize(http_method,
14
+ query_url,
15
+ headers: {},
16
+ parameters: {})
17
+ @http_method = http_method
18
+ @query_url = query_url
19
+ @headers = headers
20
+ @parameters = parameters
21
+ end
22
+
23
+ # Add a header to the HttpRequest.
24
+ # @param [String] The name of the header.
25
+ # @param [String] The value of the header.
26
+ def add_header(name, value)
27
+ @headers[name] = value
28
+ end
29
+
30
+ # Add a parameter to the HttpRequest.
31
+ # @param [String] The name of the parameter.
32
+ # @param [String] The value of the parameter.
33
+ def add_parameter(name, value)
34
+ @parameters[name] = value
35
+ end
36
+
37
+ # Add a query parameter to the HttpRequest.
38
+ # @param [String] The name of the query parameter.
39
+ # @param [String] The value of the query parameter.
40
+ def add_query_parameter(name, value)
41
+ @query_url = APIHelper.append_url_with_query_parameters(@query_url,
42
+ name => value)
43
+ @query_url = APIHelper.clean_url(@query_url)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,19 @@
1
+
2
+ module MessageMediaWebhooks
3
+ # Http response received.
4
+ class HttpResponse
5
+ attr_accessor :status_code, :headers, :raw_body
6
+
7
+ # The constructor
8
+ # @param [Integer] The status code returned by the server.
9
+ # @param [Hash] The headers sent by the server in the response.
10
+ # @param [String] The raw body of the response.
11
+ def initialize(status_code,
12
+ headers,
13
+ raw_body)
14
+ @status_code = status_code
15
+ @headers = headers
16
+ @raw_body = raw_body
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,26 @@
1
+
2
+
3
+ module MessageMediaWebhooks
4
+ # message_media_webhooks client class.
5
+ class MessageMediaWebhooksClient
6
+ # Singleton access to webhooks controller.
7
+ # @return [WebhooksController] Returns the controller instance.
8
+ def webhooks
9
+ WebhooksController.instance
10
+ end
11
+
12
+ # Returns the configuration class for easy access.
13
+ # @return [Configuration] Returns the actual configuration class.
14
+ def config
15
+ Configuration
16
+ end
17
+
18
+ # Initializer with authentication and configuration parameters.
19
+ def initialize(basic_auth_user_name: nil, basic_auth_password: nil)
20
+ Configuration.basic_auth_user_name = basic_auth_user_name if
21
+ basic_auth_user_name
22
+ Configuration.basic_auth_password = basic_auth_password if
23
+ basic_auth_password
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,32 @@
1
+
2
+ module MessageMediaWebhooks
3
+ # Base model.
4
+ class BaseModel
5
+ # Returns a Hash representation of the current object.
6
+ def to_hash
7
+ hash = {}
8
+ instance_variables.each do |name|
9
+ value = instance_variable_get(name)
10
+ name = name[1..-1]
11
+ key = self.class.names.key?(name) ? self.class.names[name] : name
12
+ if value.instance_of? Array
13
+ hash[key] = value.map { |v| v.is_a?(BaseModel) ? v.to_hash : v }
14
+ elsif value.instance_of? Hash
15
+ hash[key] = {}
16
+ value.each do |k, v|
17
+ hash[key][k] = v.is_a?(BaseModel) ? v.to_hash : v
18
+ end
19
+ else
20
+ hash[key] = value.is_a?(BaseModel) ? value.to_hash : value
21
+ end
22
+ end
23
+ hash
24
+ end
25
+
26
+ # Returns a JSON representation of the curent object.
27
+ def to_json(options = {})
28
+ hash = to_hash
29
+ hash.to_json(options)
30
+ end
31
+ end
32
+ end