messagemedia_lookups_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.
@@ -0,0 +1,16 @@
1
+
2
+ module MessageMediaLookups
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 MessageMediaLookups
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 MessageMediaLookups
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 MessageMediaLookups
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,28 @@
1
+
2
+ module MessageMediaLookups
3
+ # message_media_lookups client class.
4
+ class MessageMediaLookupsClient
5
+ # Singleton access to lookups controller.
6
+ # @return [LookupsController] Returns the controller instance.
7
+ def lookups
8
+ LookupsController.instance
9
+ end
10
+
11
+ # Returns the configuration class for easy access.
12
+ # @return [Configuration] Returns the actual configuration class.
13
+ def config
14
+ Configuration
15
+ end
16
+
17
+ # Initializer with authentication and configuration parameters.
18
+ def initialize(auth_user_name: nil, auth_password: nil, use_hmac: false)
19
+ if (use_hmac == false)
20
+ Configuration.basic_auth_user_name = auth_user_name
21
+ Configuration.basic_auth_password = auth_password
22
+ else
23
+ Configuration.hmac_auth_user_name = auth_user_name
24
+ Configuration.hmac_auth_password = auth_password
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,32 @@
1
+
2
+ module MessageMediaLookups
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
@@ -0,0 +1,58 @@
1
+
2
+ module MessageMediaLookups
3
+ # LookupAPhoneNumberResponse Model.
4
+ class LookupAPhoneNumberResponse < BaseModel
5
+ # TODO: Write general description for this method
6
+ # @return [String]
7
+ attr_accessor :country_code
8
+
9
+ # TODO: Write general description for this method
10
+ # @return [String]
11
+ attr_accessor :phone_number
12
+
13
+ # TODO: Write general description for this method
14
+ # @return [String]
15
+ attr_accessor :type
16
+
17
+ # TODO: Write general description for this method
18
+ # @return [Object]
19
+ attr_accessor :carrier
20
+
21
+ # A mapping from model property names to API property names.
22
+ def self.names
23
+ @_hash = {} if @_hash.nil?
24
+ @_hash['country_code'] = 'country_code'
25
+ @_hash['phone_number'] = 'phone_number'
26
+ @_hash['type'] = 'type'
27
+ @_hash['carrier'] = 'carrier'
28
+ @_hash
29
+ end
30
+
31
+ def initialize(country_code = nil,
32
+ phone_number = nil,
33
+ type = nil,
34
+ carrier = nil)
35
+ @country_code = country_code
36
+ @phone_number = phone_number
37
+ @type = type
38
+ @carrier = carrier
39
+ end
40
+
41
+ # Creates an instance of the object from a hash.
42
+ def self.from_hash(hash)
43
+ return nil unless hash
44
+
45
+ # Extract variables from the hash.
46
+ country_code = hash['country_code']
47
+ phone_number = hash['phone_number']
48
+ type = hash['type']
49
+ carrier = hash['carrier']
50
+
51
+ # Create object from extracted values.
52
+ LookupAPhoneNumberResponse.new(country_code,
53
+ phone_number,
54
+ type,
55
+ carrier)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,31 @@
1
+
2
+ require 'json'
3
+ require 'test/unit'
4
+ require 'message_media_lookups.rb'
5
+ require_relative '../test_helper.rb'
6
+ require_relative '../http_response_catcher.rb'
7
+
8
+ class ControllerTestBase < Test::Unit::TestCase
9
+ include MessageMediaLookups
10
+
11
+ class << self
12
+ attr_accessor :controller
13
+ end
14
+
15
+ # Called only once for a test class before any test has executed.
16
+ def self.startup
17
+ @@api_client = MessageMediaLookupsClient.new
18
+ @@request_timeout = 30
19
+ @@assert_precision = 0.01
20
+
21
+ Configuration.basic_auth_user_name = ENV['MessageMediaApiTestsKey']
22
+ Configuration.basic_auth_password = ENV['MessageMediaApiTestsSecret']
23
+
24
+ end
25
+
26
+ # Called once before every test case.
27
+ def setup
28
+ @response_catcher = HttpResponseCatcher.new
29
+ self.class.controller.http_call_back = @response_catcher
30
+ end
31
+ end
@@ -0,0 +1,58 @@
1
+
2
+ require_relative 'controller_test_base'
3
+
4
+ class LookupsControllerTests < ControllerTestBase
5
+ # Called only once for the class before any test has executed
6
+ def self.startup
7
+ self.controller = @@api_client.lookups
8
+ end
9
+
10
+ # Use the Lookups API to find information about a phone number.
11
+ #A request to the lookups API has the following format:
12
+ #```/v1/lookups/phone/{phone_number}?options={carrier,type}```
13
+ #The `{phone_number}` parameter is a required field and should be set to the phone number to be looked up.
14
+ #The options query parameter can also be used to request additional information about the phone number.
15
+ #By default, a request will only return the `country_code` and `phone_number` properties in the response.
16
+ #To request details about the the carrier, include `carrier` as a value of the options parameter.
17
+ #To request details about the type, include `type` as a value of the options parameter. To pass multiple values
18
+ #to the options parameter, use a comma separated list, i.e. `carrier,type`.
19
+ #A successful request to the lookups endpoint will return a response body as follows:
20
+ #```json
21
+ #{
22
+ # "country_code": "AU",
23
+ # "phone_number": "+61491570156",
24
+ # "type": "mobile",
25
+ # "carrier": {
26
+ # "name": "Telstra"
27
+ # }
28
+ #}
29
+ #```
30
+ #Each property in the response body is defined as follows:
31
+ #- ```country_code``` ISO ALPHA 2 country code of the phone number
32
+ #- ```phone_number``` E.164 formatted phone number
33
+ #- ```type``` The type of number. This can be ```"mobile"``` or ```"landline"```
34
+ #- ```carrier``` Holds information about the specific carrier (if available)
35
+ # - ```name``` The carrier's name as reported by the network
36
+ def test_lookup_a_phone_number()
37
+ # Parameters for the API call
38
+ phone_number = '+61491570156'
39
+ options = 'carrier,type'
40
+
41
+ # Perform the API call through the SDK function
42
+ result = self.class.controller.get_lookup_a_phone_number(phone_number, options)
43
+
44
+ # Test response code
45
+ assert_equal(@response_catcher.response.status_code, 200)
46
+
47
+ # Test headers
48
+ expected_headers = {}
49
+ expected_headers['content-type'] = nil
50
+
51
+ assert(TestHelper.match_headers(expected_headers, @response_catcher.response.headers))
52
+
53
+ # Test whether the captured response is as we expected
54
+ assert_not_nil(result)
55
+ assert_equal('{"carrier":{"name":"AU Landline Carrier"},"country_code":"AU","phone_number":"+61491570156","type":"MOBILE"}', @response_catcher.response.raw_body)
56
+ end
57
+
58
+ end
@@ -0,0 +1,12 @@
1
+
2
+ class HttpResponseCatcher < MessageMediaLookups::HttpCallBack
3
+ attr_accessor :response
4
+
5
+ def on_before_request(request)
6
+ end
7
+
8
+ # Catching the response
9
+ def on_after_response(context)
10
+ @response = context.response
11
+ end
12
+ end
@@ -0,0 +1,90 @@
1
+
2
+ require 'tempfile'
3
+ require 'open-uri'
4
+
5
+ class TestHelper
6
+
7
+ @cache = Hash.new
8
+
9
+ # Class method to compare the received headers with the expected headers.
10
+ # @param [Hash] A hash of expected headers (keys in lower case).
11
+ # @param [Hash] A hash of received headers.
12
+ # @param [Boolean, optional] A flag which determines if we allow extra headers.
13
+ def self.match_headers(expected_headers,
14
+ received_headers,
15
+ allow_extra: true)
16
+ return false if ((received_headers.length < expected_headers.length) ||
17
+ ((allow_extra == false) && (received_headers.length > expected_headers.length)))
18
+
19
+ received_headers = Hash[received_headers.map{|k, v| [k.to_s.downcase, v]}]
20
+ expected_headers.each do |e_key, e_value|
21
+ return false unless received_headers.key?(e_key)
22
+ return false if ((e_value != nil) &&
23
+ (e_value != received_headers[e_key]))
24
+ end
25
+
26
+ return true
27
+ end
28
+
29
+ # Class method to compare the received body with the expected body.
30
+ # @param [Dynamic] The expected body.
31
+ # @param [Dynamic] The received body.
32
+ # @param [Boolean, optional] A flag which determines if we check values in dictionaries.
33
+ # @param [Boolean, optional] A flag which determines if we check the order of array elements.
34
+ # @param [Boolean, optional] A flag which determines if we check the count of array elements.
35
+ def self.match_body(expected_body,
36
+ received_body,
37
+ check_values: false,
38
+ check_order: false,
39
+ check_count: false)
40
+ if expected_body.instance_of? Hash
41
+ return false unless received_body.instance_of? Hash
42
+ for key in expected_body.keys
43
+ return false unless received_body.keys.include? key
44
+ if check_values or expected_body[key].instance_of? Hash
45
+ return false unless TestHelper.match_body(expected_body[key],
46
+ received_body[key],
47
+ check_values: check_values,
48
+ check_order: check_order,
49
+ check_count: check_count)
50
+ end
51
+ end
52
+ elsif expected_body.instance_of? Array
53
+ return False unless received_body.instance_of? Array
54
+ if check_count == true && (expected_body.length != received_body.length)
55
+ return false
56
+ else
57
+ previous_matches = Array.new
58
+ expected_body.each.with_index do |expected_element, i|
59
+ matches = (received_body.map.with_index do |received_element, j|
60
+ j if TestHelper.match_body(expected_element,
61
+ received_element,
62
+ check_values: check_values,
63
+ check_order: check_order,
64
+ check_count: check_count)
65
+ end).compact
66
+ return false if matches.length == 0
67
+ if check_order == true
68
+ return false if (i != 0 && matches.map{|x| previous_matches.map{|y| y > x}.all?}.all?)
69
+ previous_matches = matches
70
+ end
71
+ end
72
+ end
73
+ elsif expected_body != received_body
74
+ return false
75
+ end
76
+ return true
77
+ end
78
+
79
+ # Class method which takes a URL, downloads the file (if not already downloaded
80
+ # for this test session) and returns the path of the file.
81
+ # @param [String] The URL of the required file.
82
+ def self.get_file(url)
83
+ unless @cache.keys.include? url
84
+ @cache[url] = Tempfile.new('APIMatic')
85
+ @cache[url].binmode
86
+ @cache[url].write(open(url, {ssl_ca_cert: Certifi.where}).read)
87
+ end
88
+ return @cache[url].path
89
+ end
90
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: messagemedia_lookups_sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - MessageMedia Developers
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-04-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: logging
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.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.10.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.10.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: test-unit
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.1'
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 3.1.5
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '3.1'
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 3.1.5
61
+ - !ruby/object:Gem::Dependency
62
+ name: certifi
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '2016.9'
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 2016.09.26
71
+ type: :runtime
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '2016.9'
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 2016.09.26
81
+ - !ruby/object:Gem::Dependency
82
+ name: faraday-http-cache
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '1.2'
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: 1.2.2
91
+ type: :runtime
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '1.2'
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: 1.2.2
101
+ description: The MessageMedia Lookups API provides a number of endpoints for validating
102
+ the phone numbers you’re sending to by checking their validity, type and carrier
103
+ records.
104
+ email: developers@messagemedia.com
105
+ executables: []
106
+ extensions: []
107
+ extra_rdoc_files: []
108
+ files:
109
+ - LICENSE
110
+ - README.md
111
+ - lib/message_media_lookups.rb
112
+ - lib/message_media_lookups/api_helper.rb
113
+ - lib/message_media_lookups/configuration.rb
114
+ - lib/message_media_lookups/controllers/base_controller.rb
115
+ - lib/message_media_lookups/controllers/lookups_controller.rb
116
+ - lib/message_media_lookups/exceptions/api_exception.rb
117
+ - lib/message_media_lookups/http/auth/basic_auth.rb
118
+ - lib/message_media_lookups/http/auth/hmac_auth.rb
119
+ - lib/message_media_lookups/http/faraday_client.rb
120
+ - lib/message_media_lookups/http/http_call_back.rb
121
+ - lib/message_media_lookups/http/http_client.rb
122
+ - lib/message_media_lookups/http/http_context.rb
123
+ - lib/message_media_lookups/http/http_method_enum.rb
124
+ - lib/message_media_lookups/http/http_request.rb
125
+ - lib/message_media_lookups/http/http_response.rb
126
+ - lib/message_media_lookups/message_media_lookups_client.rb
127
+ - lib/message_media_lookups/models/base_model.rb
128
+ - lib/message_media_lookups/models/lookup_a_phone_number_response.rb
129
+ - test/controllers/controller_test_base.rb
130
+ - test/controllers/test_lookups_controller.rb
131
+ - test/http_response_catcher.rb
132
+ - test/test_helper.rb
133
+ homepage: https://developers.messagemedia.com/
134
+ licenses:
135
+ - Apache-2.0
136
+ metadata:
137
+ source_code_uri: https://github.com/messagemedia/lookups-ruby-sdk
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - "~>"
145
+ - !ruby/object:Gem::Version
146
+ version: '2.0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 2.5.2
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: MessageMedia Lookups SDK
158
+ test_files: []