genderapi-phone-validator 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 +7 -0
- data/README.md +151 -0
- data/lib/genderapi-phone-validator/client.rb +101 -0
- data/lib/genderapi-phone-validator/version.rb +5 -0
- data/lib/genderapi-phone-validator.rb +8 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ecf9f90771c5ef2e18fab53e7e358f16ec30f66d43d42d200e638ad737f21bfe
|
4
|
+
data.tar.gz: cd2b5aff31a0617bb07393f825b7d0c9e229f387e473195b0edc6dfa2aa0ac7b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d5a059d183404f87ed093a612990c14a4165e225d8f27a2a744af7d50ee6c342fe2dd23309cfc2c38237def9fd999b11dc2483ad1c1962f11328cdd99b7e8897
|
7
|
+
data.tar.gz: 0cc94adb8911ad46186026418e0fb0c6f0694f96b4efbc2af05476c5e85963fdb34273d9f9ae20f889e1192474ef2c39157e807ef7de24a8b94546512d7476be
|
data/README.md
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
# 📞 Phone Number Validation & Formatter API (Ruby SDK)
|
2
|
+
|
3
|
+
The `phonevalidator` gem uses the official [GenderAPI Phone Number Validation & Formatter API](https://www.genderapi.io/) to validate and format phone numbers from over 240 countries and territories.
|
4
|
+
|
5
|
+
Whether your users enter phone numbers in various formats (e.g., `12128675309`, `+1 212 867 5309`, `001-212-867-5309`), this library will intelligently detect, validate, and convert the input into a standardized E.164 format (e.g., `+12128675309`).
|
6
|
+
|
7
|
+
---
|
8
|
+
|
9
|
+
## ✅ Features
|
10
|
+
|
11
|
+
- Converts phone numbers to **E.164** format
|
12
|
+
- Validates if the number is real and structurally possible
|
13
|
+
- Detects number type: **mobile**, **landline**, **VoIP**, etc.
|
14
|
+
- Identifies region/city based on area code
|
15
|
+
- Returns country-level metadata (e.g., ISO code)
|
16
|
+
- Built with Ruby, uses `HTTParty` for HTTP requests
|
17
|
+
|
18
|
+
---
|
19
|
+
|
20
|
+
## 📦 Installation
|
21
|
+
|
22
|
+
Add the gem to your project:
|
23
|
+
|
24
|
+
```bash
|
25
|
+
gem install genderapi-phone-validator
|
26
|
+
```
|
27
|
+
|
28
|
+
Or in your `Gemfile`:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
gem 'genderapi-phone-validator'
|
32
|
+
```
|
33
|
+
|
34
|
+
---
|
35
|
+
|
36
|
+
## 🔐 Authentication
|
37
|
+
|
38
|
+
You need an API key from [www.genderapi.io](https://www.genderapi.io).
|
39
|
+
Sign up and get your key from your dashboard.
|
40
|
+
|
41
|
+
---
|
42
|
+
|
43
|
+
## 🚀 Usage
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
require 'genderapi-phone-validator'
|
47
|
+
|
48
|
+
client = PhoneValidator::Client.new(api_key: "YOUR_API_KEY")
|
49
|
+
|
50
|
+
response = client.validate(
|
51
|
+
number: "+1 212 867 5309", # Required
|
52
|
+
address: "US" # Optional, helps with local formats
|
53
|
+
)
|
54
|
+
|
55
|
+
puts response
|
56
|
+
```
|
57
|
+
|
58
|
+
---
|
59
|
+
|
60
|
+
## 🧾 Input Parameters
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
validate(number:, address: nil) → Hash
|
64
|
+
```
|
65
|
+
|
66
|
+
| Parameter | Type | Required | Description |
|
67
|
+
|-----------|--------|----------|-------------|
|
68
|
+
| `number` | String | ✅ Yes | Phone number in any format |
|
69
|
+
| `address` | String | Optional | ISO country code (`US`), full country name (`Turkey`), or city name (`Berlin`) – improves local number detection |
|
70
|
+
|
71
|
+
#### Example:
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
client.validate(number: "2128675309", address: "US")
|
75
|
+
```
|
76
|
+
|
77
|
+
---
|
78
|
+
|
79
|
+
## 📬 API Response Example
|
80
|
+
|
81
|
+
```json
|
82
|
+
{
|
83
|
+
"status": true,
|
84
|
+
"remaining_credits": 15709,
|
85
|
+
"expires": 0,
|
86
|
+
"duration": "18ms",
|
87
|
+
"regionCode": "US",
|
88
|
+
"country": "United States",
|
89
|
+
"national": "(212) 867-5309",
|
90
|
+
"international": "+1 212-867-5309",
|
91
|
+
"e164": "+12128675309",
|
92
|
+
"isValid": true,
|
93
|
+
"isPossible": true,
|
94
|
+
"numberType": "FIXED_LINE_OR_MOBILE",
|
95
|
+
"nationalSignificantNumber": "2128675309",
|
96
|
+
"rawInput": "+1 212 867 5309",
|
97
|
+
"isGeographical": true,
|
98
|
+
"areaCode": "212",
|
99
|
+
"location": "New York City (Manhattan)"
|
100
|
+
}
|
101
|
+
```
|
102
|
+
|
103
|
+
---
|
104
|
+
|
105
|
+
## 🧠 Response Field Reference
|
106
|
+
|
107
|
+
| Field | Type | Description |
|
108
|
+
|--------------------------|---------|---------------------------------------------|
|
109
|
+
| `status` | Boolean | Was the request successful |
|
110
|
+
| `remaining_credits` | Integer | Remaining API credits |
|
111
|
+
| `regionCode` | String | ISO 3166-1 alpha-2 code (e.g., `US`) |
|
112
|
+
| `country` | String | Country name |
|
113
|
+
| `e164` | String | Number formatted to E.164 |
|
114
|
+
| `isValid` | Boolean | Is number valid according to rules |
|
115
|
+
| `isPossible` | Boolean | Is number structurally possible |
|
116
|
+
| `numberType` | String | Number type (`MOBILE`, `FIXED_LINE`, etc.) |
|
117
|
+
| `areaCode` | String | Area code from input |
|
118
|
+
| `location` | String | City/region matched from area code |
|
119
|
+
|
120
|
+
---
|
121
|
+
|
122
|
+
## 🔢 Number Type Values
|
123
|
+
|
124
|
+
| Value | Description |
|
125
|
+
|------------------------|-----------------------------------------|
|
126
|
+
| `FIXED_LINE` | Landline |
|
127
|
+
| `MOBILE` | Mobile phone |
|
128
|
+
| `FIXED_LINE_OR_MOBILE` | Ambiguous, could be both |
|
129
|
+
| `TOLL_FREE` | e.g., 800 numbers |
|
130
|
+
| `PREMIUM_RATE` | Expensive premium numbers |
|
131
|
+
| `SHARED_COST` | Cost shared between parties |
|
132
|
+
| `VOIP` | Internet-based phone |
|
133
|
+
| `PERSONAL_NUMBER` | Forwarding number |
|
134
|
+
| `PAGER` | Obsolete pager number |
|
135
|
+
| `VOICEMAIL` | Voicemail access |
|
136
|
+
| `UNKNOWN` | Cannot be determined |
|
137
|
+
|
138
|
+
---
|
139
|
+
|
140
|
+
## ℹ️ More Information
|
141
|
+
|
142
|
+
- Supports **240+ countries and territories**
|
143
|
+
- Automatically detects mobile vs. landline
|
144
|
+
- Useful for CRMs, forms, messaging, marketing, and more
|
145
|
+
- Full documentation at: [https://www.genderapi.io/docs-phone-validation-formatter-api](https://www.genderapi.io/docs-phone-validation-formatter-api)
|
146
|
+
|
147
|
+
---
|
148
|
+
|
149
|
+
## 📝 License
|
150
|
+
|
151
|
+
MIT License © [GenderAPI](https://www.genderapi.io)
|
@@ -0,0 +1,101 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "httparty"
|
4
|
+
require "json"
|
5
|
+
|
6
|
+
module PhoneValidator
|
7
|
+
##
|
8
|
+
# Ruby SDK for Phone Number Validation & Formatter API from www.genderapi.io
|
9
|
+
#
|
10
|
+
# This SDK allows you to:
|
11
|
+
# - Validate international phone numbers
|
12
|
+
# - Detect number type (mobile, landline, VoIP, etc.)
|
13
|
+
# - Retrieve region and country metadata
|
14
|
+
# - Format numbers to E.164 or national format
|
15
|
+
#
|
16
|
+
# Learn more: https://www.genderapi.io
|
17
|
+
#
|
18
|
+
class Client
|
19
|
+
include HTTParty
|
20
|
+
base_uri "https://api.genderapi.io"
|
21
|
+
|
22
|
+
##
|
23
|
+
# Initialize the PhoneValidator client.
|
24
|
+
#
|
25
|
+
# @param api_key [String] Your GenderAPI API key as a Bearer token.
|
26
|
+
# @param base_url [String] Optional override for the API base URL.
|
27
|
+
#
|
28
|
+
def initialize(api_key:, base_url: nil)
|
29
|
+
@api_key = api_key
|
30
|
+
self.class.base_uri(base_url) if base_url
|
31
|
+
@headers = {
|
32
|
+
"Authorization" => "Bearer #{@api_key}",
|
33
|
+
"Content-Type" => "application/json"
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
##
|
38
|
+
# Validate and format a phone number.
|
39
|
+
#
|
40
|
+
# @param number [String] The phone number to validate (required).
|
41
|
+
# @param address [String] Optional address or country hint for better accuracy.
|
42
|
+
#
|
43
|
+
# @return [Hash] Parsed JSON response containing validation results.
|
44
|
+
#
|
45
|
+
def validate(number:, address: "")
|
46
|
+
payload = {
|
47
|
+
number: number,
|
48
|
+
address: address
|
49
|
+
}
|
50
|
+
|
51
|
+
_post_request("/api/phone", payload)
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
##
|
57
|
+
# Internal helper for sending POST requests to the GenderAPI.io API.
|
58
|
+
#
|
59
|
+
# Handles:
|
60
|
+
# - Bearer token authorization
|
61
|
+
# - Payload cleanup (removal of nil values)
|
62
|
+
# - Error handling and JSON parsing
|
63
|
+
#
|
64
|
+
# @param endpoint [String] API endpoint path (e.g. "/api/phone")
|
65
|
+
# @param payload [Hash] Request body
|
66
|
+
#
|
67
|
+
# @return [Hash] Parsed JSON response
|
68
|
+
#
|
69
|
+
def _post_request(endpoint, payload)
|
70
|
+
cleaned_payload = payload.reject { |_k, v| v.nil? }
|
71
|
+
|
72
|
+
response = self.class.post(
|
73
|
+
endpoint,
|
74
|
+
headers: @headers,
|
75
|
+
body: JSON.generate(cleaned_payload)
|
76
|
+
)
|
77
|
+
|
78
|
+
if [500, 502, 503, 504, 408].include?(response.code)
|
79
|
+
raise "GenderAPI Server Error or Timeout: HTTP #{response.code} - #{response.body}"
|
80
|
+
else
|
81
|
+
begin
|
82
|
+
parse_json(response.body)
|
83
|
+
rescue JSON::ParserError
|
84
|
+
raise "GenderAPI Response is not valid JSON"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
rescue HTTParty::Error => e
|
88
|
+
raise "GenderAPI Request failed: #{e.message}"
|
89
|
+
end
|
90
|
+
|
91
|
+
##
|
92
|
+
# Safely parse JSON response.
|
93
|
+
#
|
94
|
+
# @param body [String] JSON string
|
95
|
+
# @return [Hash] Ruby Hash
|
96
|
+
#
|
97
|
+
def parse_json(body)
|
98
|
+
JSON.parse(body)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: genderapi-phone-validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Onur Ozturk
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-08-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httparty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.18'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.18'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: json
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.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: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: "\n Official Ruby SDK for the Phone Number Validation & Formatter
|
56
|
+
API by GenderAPI.io.\n\n Features:\n - Validate phone numbers from 240+ countries\n
|
57
|
+
\ - Format numbers to E.164 or national format\n - Detect number type (mobile,
|
58
|
+
landline, VoIP, etc.)\n - Retrieve region, area code, and country metadata\n\n
|
59
|
+
\ Built using HTTParty for robust HTTP requests.\n "
|
60
|
+
email:
|
61
|
+
- support@genderapi.io
|
62
|
+
executables: []
|
63
|
+
extensions: []
|
64
|
+
extra_rdoc_files: []
|
65
|
+
files:
|
66
|
+
- README.md
|
67
|
+
- lib/genderapi-phone-validator.rb
|
68
|
+
- lib/genderapi-phone-validator/client.rb
|
69
|
+
- lib/genderapi-phone-validator/version.rb
|
70
|
+
homepage: https://www.genderapi.io
|
71
|
+
licenses:
|
72
|
+
- MIT
|
73
|
+
metadata:
|
74
|
+
source_code_uri: https://github.com/GenderAPI/phone-validator-ruby
|
75
|
+
changelog_uri: https://github.com/GenderAPI/phone-validator-ruby/blob/main/CHANGELOG.md
|
76
|
+
documentation_uri: https://rubydoc.info/gems/genderapi-phone-validator/1.0.0
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '2.6'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubygems_version: 3.2.3
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Ruby SDK for validating and formatting international phone numbers using
|
96
|
+
GenderAPI.
|
97
|
+
test_files: []
|