kickbox_rails 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.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NGYxMTJhY2NjNzU4NDlhZjgzMjZiN2M1NTYyZjMxOWQ2MGMyNzJmNA==
5
+ data.tar.gz: !binary |-
6
+ MjdiZjBkY2Q0ZDQ3NWM0Yzg1MjVjNmVmNTA2NjZhZWFjMzg1ODM1NQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ NWVlYzRkNzAzMjAzNDgzZjZhY2M5Yzg2NDI4MmI0NTMzNmM3NmJkN2Q2ZjBh
10
+ ZmQzZmM3N2NhNGU2ZDI1OTNiNjMwYWNkYjIyNzBkZTZhZWIwMmY4YzRjNzI1
11
+ Y2Y4MTgxYjQyYjdhMmI5ZjlkYzhmMjAyNGI5ZDkxM2Q2MzM5ODQ=
12
+ data.tar.gz: !binary |-
13
+ YmU4NDMyMmY4MGNjODNmNTA3MzNkNDE4MGE0MWI5NjJhMGEwYTYxODQ2Y2Nj
14
+ NzA0YzExNzI5Y2VmY2QwODZmZjRmMzJhMzEyYjJlMjVkODQyYzNjZDQyNmY0
15
+ ZTMyNjJjOTM2MWM3Y2Q5ZGYzY2I1NDQ0NjFlMzkyOWE3MjAwZmE=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mail-validator.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Samuel Navas
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # KickboxRails
2
+
3
+ Validate emails using Kickbox.io API in Ruby on Rails and fallback to basic syntax email validation if Kickbox.io API fails for any reason
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'kickbox_rails'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Execute next command for generate initializer kickbox_rails.rb, where you can configure your kickbox.io account api_key:
17
+
18
+ $ rails g kickbox_rails:install
19
+
20
+ ## Usage
21
+
22
+ # Rails
23
+ class Person < ActiveRecord::Base
24
+ validates_kickbox_email_format :email
25
+ # OR
26
+ validates :email, kickbox_email_format: { message: 'Email is invalid' }
27
+ end
28
+
29
+ # Non Rails
30
+ KickboxRails.valid?("example@email.com")
31
+ KickboxRails.invalid?("example@email.com")
32
+
33
+ If you want to get related info from kickbox.io API use validate method
34
+
35
+ KickboxRails.validate("bill.lumbergh@gamil.com") =>
36
+
37
+ {
38
+ "result":"invalid",
39
+ "reason":"rejected_email",
40
+ "role":false,
41
+ "free":false,
42
+ "disposable":false,
43
+ "accept_all":false,
44
+ "did_you_mean":"bill.lumbergh@gmail.com",
45
+ "sendex":0,
46
+ "email":"bill.lumbergh@gamil.com",
47
+ "user":"bill.lumbergh",
48
+ "domain":"gamil.com",
49
+ "success":true,
50
+ "message":null
51
+ }
52
+
53
+
54
+ ## Contributing
55
+
56
+ 1. Fork it ( http://github.com/the-cocktail/kickbox_rails/fork )
57
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
58
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
59
+ 4. Push to the branch (`git push origin my-new-feature`)
60
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ Dir.glob('tasks/**/*.rake').each(&method(:import))
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kickbox_rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "kickbox_rails"
8
+ spec.version = KickboxRails::VERSION
9
+ spec.authors = ["Samuel Navas"]
10
+ spec.email = ["samuel.navas@the-cocktail.com"]
11
+ spec.summary = %q{email validation using kikbox.io service}
12
+ spec.description = %q{Validates email against Kickbox.io service}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+
25
+ spec.add_dependency 'i18n'
26
+ spec.add_dependency "faraday"
27
+ spec.add_dependency "json"
28
+
29
+ end
@@ -0,0 +1,14 @@
1
+ module KickboxRails
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../../templates", __FILE__)
5
+ desc "Creates KickboxRails initializer for your application"
6
+
7
+ def copy_initializer
8
+ template "kickbox_rails_initializer.rb", "config/initializers/kickbox_rails.rb"
9
+
10
+ puts "Install complete!"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ KickboxRails.configure do |config|
2
+ # Configure validator with Kickbox.io API KEY. Uncomment next lines and keep api_url
3
+ # and api_resource or change them if kickbox api url has been modified.
4
+ config.api_url = 'https://api.kickbox.io'
5
+ config.api_resource = '/v1/verify'
6
+ config.api_key = 'API_KEY_HERE'
7
+ end
@@ -0,0 +1,29 @@
1
+ class KickboxApi
2
+ def initialize opts = {}
3
+ @token = opts[:token]
4
+ @end_point = opts[:end_point]
5
+ @url = opts[:url]
6
+
7
+ raise ArgumentError if @token.nil? || @end_point.nil? || @url.nil?
8
+
9
+ @conn = Faraday.new(@url) do |faraday|
10
+ faraday.adapter Faraday.default_adapter
11
+ end
12
+ end
13
+
14
+ def verify email
15
+ begin
16
+ response = @conn.get do |req|
17
+ req.url "#{@end_point}?email=#{email}&apikey=#{@token}"
18
+ req.options.timeout = 2
19
+ req.options.open_timeout = 2
20
+ end
21
+
22
+ if response.status == 200
23
+ JSON.parse(response.body) if response.body.is_a? String
24
+ end
25
+ rescue
26
+ {'success' => 'false'}
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,23 @@
1
+ require 'kickbox_rails'
2
+ require 'i18n'
3
+
4
+ module ActiveModel
5
+ module Validations
6
+ class KickboxEmailFormatValidator < EachValidator
7
+
8
+ DEFAULT_MESSAGE = "Email is invalid"
9
+
10
+ def validate_each(record, attribute, value)
11
+ if KickboxRails.invalid?(value)
12
+ record.errors[attribute] << (defined?(I18n) ? I18n.t(:invalid_email_address, :default => (options[:message]||DEFAULT_MESSAGE), :locale => I18n.default_locale) : (options[:message]||DEFAULT_MESSAGE))
13
+ end
14
+ end
15
+ end
16
+
17
+ module HelperMethods
18
+ def validates_kickbox_email_format(*attr_names)
19
+ validates_with KickboxEmailFormatValidator, _merge_attributes(attr_names)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module KickboxRails
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,70 @@
1
+ require 'json'
2
+ require 'faraday'
3
+ require 'kickbox_api'
4
+
5
+
6
+ module KickboxRails
7
+ class << self
8
+ attr_accessor :configuration, :provider
9
+
10
+ def configure
11
+ @configuration ||= Configuration.new
12
+ yield(configuration)
13
+ @provider = KickboxApi.new(url: @configuration.api_url,
14
+ end_point: @configuration.api_resource,
15
+ token: @configuration.api_key)
16
+ end
17
+
18
+ def validate email
19
+ begin
20
+ response = @provider.verify(email||String.new)
21
+ # fallbacks if provider return success = false
22
+ throw Exception unless response['success']
23
+
24
+ response
25
+
26
+ rescue Exception => e
27
+ # If Kickbox service respond with Error we do fallback to basic validation
28
+ basic_email_validation email
29
+ end
30
+ end
31
+
32
+ def valid? email
33
+ response = validate(email)
34
+ response['result'] == 'valid' ? true : false
35
+ end
36
+
37
+ def invalid? email
38
+ !valid? email
39
+ end
40
+
41
+ private
42
+
43
+ # Basic validation with standard regexp for emails
44
+ def basic_email_validation email, options = {}
45
+ result = if (email =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i).nil?
46
+ {
47
+ 'result' => 'invalid',
48
+ 'reason' => options[:reason] || "invalid_email"
49
+ }
50
+ else
51
+ {
52
+ 'result' => 'valid',
53
+ }
54
+ end
55
+ end
56
+ end
57
+
58
+ class Configuration
59
+ attr_accessor :api_key, :api_url, :api_resource
60
+
61
+ def initialize
62
+ @api_key = nil
63
+ @api_url = nil
64
+ @api_resource = nil
65
+ end
66
+ end
67
+ end
68
+
69
+
70
+ require 'kickbox_email_format_validator' if defined?(::ActiveModel) && !(ActiveModel::VERSION::MAJOR < 2 || (2 == ActiveModel::VERSION::MAJOR && ActiveModel::VERSION::MINOR < 1))
@@ -0,0 +1,120 @@
1
+ require 'spec_helper'
2
+
3
+ describe KickboxRails do
4
+ subject { KickboxRails.new }
5
+
6
+ describe "Configuration" do
7
+ before do
8
+ KickboxRails.configure do |config|
9
+ config.api_key = 'example_key'
10
+ config.api_url = 'http://example-url.com/'
11
+ config.api_resource = 'v1/resource'
12
+ end
13
+ end
14
+
15
+ it 'is configurable' do
16
+ expect(KickboxRails.configuration.api_key).to eq('example_key')
17
+ expect(KickboxRails.configuration.api_url).to eq('http://example-url.com/')
18
+ expect(KickboxRails.configuration.api_resource).to eq('v1/resource')
19
+ end
20
+ end
21
+
22
+ describe "Email" do
23
+
24
+ let(:valid_email) { "example@gmail.com" }
25
+ let(:invalid_email) { "examplemail.com" }
26
+ let(:valid_result) { Hash['result','valid'] }
27
+ let(:invalid_result) { Hash['result','invalid','reason','invalid_email'] }
28
+ let(:valid_service_result) do
29
+ {
30
+ "result" => "valid",
31
+ "reason" => "accepted_email",
32
+ "role" => false,
33
+ "free" => false,
34
+ "disposable" => false,
35
+ "accept_all" => true,
36
+ "did_you_mean" => nil,
37
+ "sendex" => 0.9,
38
+ "email" => "example@gmail.com",
39
+ "user" => "example",
40
+ "domain" => "gmail.com",
41
+ "success" => true,
42
+ "message" => nil
43
+ }
44
+ end
45
+
46
+ let(:invalid_service_result) do
47
+ {
48
+ "result" => "invalid",
49
+ "reason" => "rejected_email",
50
+ "role" => false,
51
+ "free" => false,
52
+ "disposable" => false,
53
+ "accept_all" => false,
54
+ "did_you_mean" => "example@gmail.com",
55
+ "sendex" => "0",
56
+ "email" => "example@gamil.com",
57
+ "user" => "example",
58
+ "domain" => "gamil.com",
59
+ "success" => true,
60
+ "message" => nil
61
+ }
62
+ end
63
+
64
+ let(:error_service_result) do
65
+ {
66
+ "success" => false,
67
+ "message" => "No email address defined"
68
+ }
69
+ end
70
+
71
+ it 'is valid with basic validation' do
72
+ expect(KickboxRails.validate(valid_email)).to eq(valid_result)
73
+ end
74
+
75
+ it 'is invalid with basic validation' do
76
+ expect(KickboxRails.validate(invalid_email)).to eq(invalid_result)
77
+ end
78
+
79
+ it 'is invalid with nil parameter' do
80
+ expect(KickboxRails.validate(nil)).to eq(invalid_result)
81
+ end
82
+
83
+ it 'is valid with service validation' do
84
+ allow(KickboxRails.provider).to receive(:verify).and_return(valid_service_result)
85
+ expect(KickboxRails.validate(valid_email)).to eq(valid_service_result)
86
+ end
87
+
88
+ it 'is invalid with service validation' do
89
+ allow(KickboxRails.provider).to receive(:verify).and_return(invalid_service_result)
90
+ expect(KickboxRails.validate(valid_email)).to eq(invalid_service_result)
91
+ end
92
+
93
+ it 'fallbacks to valid on service error ' do
94
+ allow(KickboxRails.provider).to receive(:verify).and_return(error_service_result)
95
+ expect(KickboxRails.validate(valid_email)).to eq(valid_result)
96
+ end
97
+
98
+ it 'fallbacks to invalid on service error ' do
99
+ allow(KickboxRails.provider).to receive(:verify).and_return(error_service_result)
100
+ expect(KickboxRails.validate(invalid_email)).to eq(invalid_result)
101
+ end
102
+
103
+ it 'fallbacks to invalid on nil value and service error ' do
104
+ allow(KickboxRails.provider).to receive(:verify).and_return(error_service_result)
105
+ expect(KickboxRails.validate(nil)).to eq(invalid_result)
106
+ end
107
+
108
+ it 'is valid? eq true' do
109
+ expect(KickboxRails.valid?(valid_email)).to eq(true)
110
+ end
111
+
112
+ it 'is valid? eq false' do
113
+ expect(KickboxRails.valid?(invalid_email)).to eq(false)
114
+ end
115
+
116
+ it 'is valid? eq false with nil parameter' do
117
+ expect(KickboxRails.valid?(nil)).to eq(false)
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,17 @@
1
+ require 'bundler'
2
+ Bundler.setup
3
+
4
+ require 'kickbox_rails'
5
+
6
+ Bundler.require(:test)
7
+
8
+ RSpec.configure do |config|
9
+ # Use color in STDOUT
10
+ config.color = true
11
+
12
+ # Use color not only in STDOUT but also in pagers and files
13
+ config.tty = true
14
+
15
+ # Use the specified formatter
16
+ config.formatter = :documentation # :progress, :html, :textmate
17
+ end
data/tasks/rspec.rake ADDED
@@ -0,0 +1,3 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kickbox_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Samuel Navas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: i18n
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: json
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Validates email against Kickbox.io service
98
+ email:
99
+ - samuel.navas@the-cocktail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - kickbox_rails.gemspec
110
+ - lib/generators/kickbox_rails/install_generator.rb
111
+ - lib/generators/templates/kickbox_rails_initializer.rb
112
+ - lib/kickbox_api.rb
113
+ - lib/kickbox_email_format_validator.rb
114
+ - lib/kickbox_rails.rb
115
+ - lib/kickbox_rails/version.rb
116
+ - spec/email_validator_spec.rb
117
+ - spec/spec_helper.rb
118
+ - tasks/rspec.rake
119
+ homepage: ''
120
+ licenses:
121
+ - MIT
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ! '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.2.2
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: email validation using kikbox.io service
143
+ test_files:
144
+ - spec/email_validator_spec.rb
145
+ - spec/spec_helper.rb