quickemailverification 1.0.1

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,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 32dcbf1542623a4c188f82b21f33a230f8e44016dcc00ab06ad7f10f07fcf55d
4
+ data.tar.gz: 53f85e4016f00fbfc2e66c30ab879d1b9c466faaec28d97a35f452a309fb2866
5
+ SHA512:
6
+ metadata.gz: d660971e48ebd1ccee8d5315da06d2cf40349282e3bb940895024d9f12964a4344360b1f26b943e384f96f87f1b440af5049769769cbe4ad8bfca1295a633c83
7
+ data.tar.gz: e95334842139bccb22caa34a24155062678adcc72ae108852f0438da6d2fe9ab1b3cb917d5ea5d3a0a1d2fbfa7754b3b3aa0ab2c995ec403d92611eb7a84f412
@@ -0,0 +1,8 @@
1
+ require "rubygems"
2
+
3
+ require "quickemailverification/client"
4
+ require "quickemailverification/error"
5
+ require "quickemailverification/http_client"
6
+
7
+ module QuickEmailVerification
8
+ end
@@ -0,0 +1,29 @@
1
+ module QuickEmailVerification
2
+
3
+ module Api
4
+
5
+ # QuickEmailVerification Class for email verification
6
+ class Quickemailverification
7
+
8
+ def initialize(client)
9
+ @client = client
10
+ end
11
+
12
+ # Verify email address and get detailed response
13
+ #
14
+ # '/verify?email=:email' GET
15
+ #
16
+ # email - send email address in query parameter
17
+ def verify(email, options = {})
18
+ body = options.fetch("query", {})
19
+
20
+ email = CGI::escape(email)
21
+
22
+ @client.get("/verify?email=#{email}", body, options)
23
+ end
24
+
25
+ end
26
+
27
+ end
28
+
29
+ end
@@ -0,0 +1,21 @@
1
+ require "faraday"
2
+ require "json"
3
+
4
+ require "quickemailverification/api/quickemailverification"
5
+
6
+ module QuickEmailVerification
7
+
8
+ class Client
9
+
10
+ def initialize(auth = {}, options = {})
11
+ @http_client = QuickEmailVerification::HttpClient::HttpClient.new(auth, options)
12
+ end
13
+
14
+ # QuickEmailVerification Class for email verification
15
+ def quickemailverification()
16
+ QuickEmailVerification::Api::Quickemailverification.new(@http_client)
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1,9 @@
1
+ require "quickemailverification/error/client_error"
2
+
3
+ module QuickEmailVerification
4
+
5
+ module Error
6
+
7
+ end
8
+
9
+ end
@@ -0,0 +1,18 @@
1
+ module QuickEmailVerification
2
+
3
+ module Error
4
+
5
+ class ClientError < ::StandardError
6
+
7
+ attr_reader :code
8
+
9
+ def initialize(message, code)
10
+ @code = code
11
+ super message
12
+ end
13
+
14
+ end
15
+
16
+ end
17
+
18
+ end
@@ -0,0 +1,124 @@
1
+ require "quickemailverification/http_client/auth_handler"
2
+ require "quickemailverification/http_client/error_handler"
3
+ require "quickemailverification/http_client/request_handler"
4
+ require "quickemailverification/http_client/response"
5
+ require "quickemailverification/http_client/response_handler"
6
+
7
+ module QuickEmailVerification
8
+
9
+ module HttpClient
10
+
11
+ # Main HttpClient which is used by Api classes
12
+ class HttpClient
13
+
14
+ attr_accessor :options, :headers
15
+
16
+ def initialize(auth = {}, options = {})
17
+
18
+ if auth.is_a?(String)
19
+ auth = { :http_header => auth }
20
+ end
21
+
22
+ @options = {
23
+ :base => "http://api.quickemailverification.com",
24
+ :api_version => "v1",
25
+ :user_agent => "quickemailverification-ruby/1.0.1 (https://github.com/quickemailverification/quickemailverification-ruby)"
26
+ }
27
+
28
+ @options.update(options)
29
+
30
+ @headers = {
31
+ "user-agent" => @options[:user_agent]
32
+ }
33
+
34
+ if @options.has_key?(:headers)
35
+ @headers.update(Hash[@options[:headers].map { |k, v| [k.downcase, v] }])
36
+ @options.delete(:headers)
37
+ end
38
+
39
+ @client = Faraday.new(@options[:base]) do |conn|
40
+ conn.use(QuickEmailVerification::HttpClient::AuthHandler, auth)
41
+ conn.use(QuickEmailVerification::HttpClient::ErrorHandler)
42
+
43
+ conn.adapter(Faraday.default_adapter)
44
+ end
45
+ end
46
+
47
+ def get(path, params = {}, options = {})
48
+ request(path, nil, "get", options.merge({ :query => params }))
49
+ end
50
+
51
+ def post(path, body = {}, options = {})
52
+ request(path, body, "post", options)
53
+ end
54
+
55
+ def patch(path, body = {}, options = {})
56
+ request(path, body, "patch", options)
57
+ end
58
+
59
+ def delete(path, body = {}, options = {})
60
+ request(path, body, "delete", options)
61
+ end
62
+
63
+ def put(path, body = {}, options = {})
64
+ request(path, body, "put", options)
65
+ end
66
+
67
+ # Intermediate function which does three main things
68
+ #
69
+ # - Transforms the body of request into correct format
70
+ # - Creates the requests with give parameters
71
+ # - Returns response body after parsing it into correct format
72
+ def request(path, body, method, options)
73
+ options = @options.merge(options)
74
+
75
+ options[:headers] = options[:headers] || {}
76
+ options[:headers] = @headers.merge(Hash[options[:headers].map { |k, v| [k.downcase, v] }])
77
+
78
+ options[:body] = body
79
+
80
+ if method != "get"
81
+ options[:body] = options[:body] || {}
82
+ options = set_body(options)
83
+ end
84
+
85
+ response = create_request(method, path, options)
86
+
87
+ env = response.env
88
+ body = get_body(env)
89
+
90
+ QuickEmailVerification::HttpClient::Response.new(body, env.status, env.response_headers)
91
+ end
92
+
93
+ # Creating a request with the given arguments
94
+ #
95
+ # If api_version is set, appends it immediately after host
96
+ def create_request(method, path, options)
97
+ version = options.has_key?(:api_version) ? "/#{options[:api_version]}" : ""
98
+
99
+ path = "#{version}#{path}"
100
+
101
+ instance_eval <<-RUBY, __FILE__, __LINE__ + 1
102
+ @client.#{method}(path) do |req|
103
+ req.body = options[:body]
104
+ req.headers.update(options[:headers])
105
+ req.params.update(options[:query]) if options[:query]
106
+ end
107
+ RUBY
108
+ end
109
+
110
+ # Get response body in correct format
111
+ def get_body(env)
112
+ QuickEmailVerification::HttpClient::ResponseHandler.get_body(env)
113
+ end
114
+
115
+ # Set request body in correct format
116
+ def set_body(options)
117
+ QuickEmailVerification::HttpClient::RequestHandler.set_body(options)
118
+ end
119
+
120
+ end
121
+
122
+ end
123
+
124
+ end
@@ -0,0 +1,73 @@
1
+ require "base64"
2
+
3
+ module QuickEmailVerification
4
+
5
+ module HttpClient
6
+
7
+ # AuthHandler takes care of devising the auth type and using it
8
+ class AuthHandler < Faraday::Middleware
9
+
10
+ HTTP_HEADER = 1
11
+
12
+ def initialize(app, auth = {}, options = {})
13
+ @auth = auth
14
+ super(app)
15
+ end
16
+
17
+ def call(env)
18
+ if !@auth.empty?
19
+ auth = get_auth_type
20
+ flag = false
21
+
22
+ if auth == HTTP_HEADER
23
+ env = http_header(env)
24
+ flag = true
25
+ end
26
+
27
+ if !flag
28
+ raise StandardError.new "Unable to calculate authorization method. Please check"
29
+ end
30
+ else
31
+ raise StandardError.new "Server requires authentication to proceed further. Please check"
32
+ end
33
+
34
+ @app.call(env)
35
+ end
36
+
37
+ # Calculating the Authentication Type
38
+ def get_auth_type()
39
+
40
+ if @auth.has_key?(:http_header)
41
+ return HTTP_HEADER
42
+ end
43
+
44
+ return -1
45
+ end
46
+
47
+ # Authorization with HTTP header
48
+ def http_header(env)
49
+ env[:request_headers]["Authorization"] = "token #{@auth[:http_header]}"
50
+
51
+ return env
52
+ end
53
+
54
+ def query_params(url)
55
+ if url.query.nil? or url.query.empty?
56
+ {}
57
+ else
58
+ Faraday::Utils.parse_query(url.query)
59
+ end
60
+ end
61
+
62
+ def merge_query(env, query)
63
+ query = query.update query_params(env[:url])
64
+
65
+ env[:url].query = Faraday::Utils.build_query(query)
66
+
67
+ return env
68
+ end
69
+ end
70
+
71
+ end
72
+
73
+ end
@@ -0,0 +1,51 @@
1
+ module QuickEmailVerification
2
+
3
+ module HttpClient
4
+
5
+ # ErrorHanlder takes care of selecting the error message from response body
6
+ class ErrorHandler < Faraday::Middleware
7
+
8
+ def initialize(app)
9
+ super(app)
10
+ end
11
+
12
+ def call(env)
13
+ @app.call(env).on_complete do |env|
14
+ code = env.status
15
+ type = env.response_headers["content-type"] || ''
16
+
17
+ case code
18
+ when 500...599
19
+ raise QuickEmailVerification::Error::ClientError.new("Error #{code}", code)
20
+ when 400...499
21
+ body = QuickEmailVerification::HttpClient::ResponseHandler.get_body(env)
22
+ message = ""
23
+
24
+ # If HTML, whole body is taken
25
+ if body.is_a?(String)
26
+ message = body
27
+ end
28
+
29
+ # If JSON, a particular field is taken and used
30
+ if type.include?("json") and body.is_a?(Hash)
31
+ if body.has_key?("message")
32
+ message = body["message"]
33
+ else
34
+ message = "Unable to select error message from json returned by request responsible for error"
35
+ end
36
+ end
37
+
38
+ if message == ""
39
+ message = "Unable to understand the content type of response returned by request responsible for error"
40
+ end
41
+
42
+ raise QuickEmailVerification::Error::ClientError.new message, code
43
+ end
44
+ end
45
+ end
46
+
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -0,0 +1,24 @@
1
+ module QuickEmailVerification
2
+
3
+ module HttpClient
4
+
5
+ # RequestHandler takes care of encoding the request body into format given by options
6
+ class RequestHandler
7
+
8
+ def self.set_body(options)
9
+ type = options.fetch(:request_type, "raw")
10
+
11
+ # Raw body
12
+ if type == "raw"
13
+ options[:body] = options[:body].is_a?(Hash) ? "" : options[:body]
14
+ options[:headers].delete("content-type")
15
+ end
16
+
17
+ return options
18
+ end
19
+
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,20 @@
1
+ module QuickEmailVerification
2
+
3
+ module HttpClient
4
+
5
+ # Response object contains the response returned by the client
6
+ class Response
7
+
8
+ attr_accessor :body, :code, :headers
9
+
10
+ def initialize(body, code, headers)
11
+ @body = body
12
+ @code = code
13
+ @headers = headers
14
+ end
15
+
16
+ end
17
+
18
+ end
19
+
20
+ end
@@ -0,0 +1,24 @@
1
+ module QuickEmailVerification
2
+
3
+ module HttpClient
4
+
5
+ # ResponseHandler takes care of decoding the response body into suitable type
6
+ class ResponseHandler
7
+
8
+ def self.get_body(env)
9
+ type = env.response_headers["content-type"] || ''
10
+ body = env.body
11
+
12
+ # Response body is in JSON
13
+ if type.include?("json")
14
+ body = JSON.parse body
15
+ end
16
+
17
+ return body
18
+ end
19
+
20
+ end
21
+
22
+ end
23
+
24
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: quickemailverification
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - QuickEmailVerification
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-01-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.9'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.8'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.8'
41
+ description: Official QuickEmailVerification API library client for ruby.
42
+ email: support@quickemailverification.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/quickemailverification.rb
48
+ - lib/quickemailverification/api/quickemailverification.rb
49
+ - lib/quickemailverification/client.rb
50
+ - lib/quickemailverification/error.rb
51
+ - lib/quickemailverification/error/client_error.rb
52
+ - lib/quickemailverification/http_client.rb
53
+ - lib/quickemailverification/http_client/auth_handler.rb
54
+ - lib/quickemailverification/http_client/error_handler.rb
55
+ - lib/quickemailverification/http_client/request_handler.rb
56
+ - lib/quickemailverification/http_client/response.rb
57
+ - lib/quickemailverification/http_client/response_handler.rb
58
+ homepage: https://quickemailverification.com
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.7.4
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Official QuickEmailVerification API library client for ruby
82
+ test_files: []