genderize-io 0.1.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.reek +4 -0
- data/README.md +43 -11
- data/lib/genderize/io.rb +2 -1
- data/lib/genderize/io/base.rb +68 -0
- data/lib/genderize/io/batch/lookup.rb +33 -0
- data/lib/genderize/io/lookup.rb +12 -59
- data/lib/genderize/io/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ebc6e06738af6ebd88bea0db2a23de063cf8105bf7496bb438853bf643cdbba4
|
4
|
+
data.tar.gz: 9bd34f44ae411ac7c66c6d9f5b8b7844697cda77409d064cf334effd76210568
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4fb103eaa24e8e7d1e9ebacc4b91aa02ab09c4c68a8a23a43e49a2d3b6c69fcc6250765feb7292f89cc43c47446f58fe218292854539a9ae7d80abbb7d685be2
|
7
|
+
data.tar.gz: e912b7613bde5cdbb9055345cf989b60462f86329f8b4ed608194276569c6276ee7c81e2416737bedbb560060f5aa9826ac37dbc0feb420db04e250770c4744c
|
data/.reek
CHANGED
@@ -3,6 +3,8 @@ BooleanParameter:
|
|
3
3
|
enabled: false
|
4
4
|
DuplicateMethodCall:
|
5
5
|
enabled: false
|
6
|
+
FeatureEnvy:
|
7
|
+
enabled: false
|
6
8
|
InstanceVariableAssumption:
|
7
9
|
enabled: false
|
8
10
|
IrresponsibleModule:
|
@@ -11,6 +13,8 @@ LongParameterList:
|
|
11
13
|
enabled: false
|
12
14
|
NilCheck:
|
13
15
|
enabled: false
|
16
|
+
RepeatedConditional:
|
17
|
+
enabled: false
|
14
18
|
TooManyInstanceVariables:
|
15
19
|
enabled: false
|
16
20
|
TooManyStatements:
|
data/README.md
CHANGED
@@ -3,7 +3,8 @@
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/genderize-io.svg)](http://badge.fury.io/rb/genderize-io)
|
4
4
|
[![Build Status](https://travis-ci.org/drexed/genderize-io.svg?branch=master)](https://travis-ci.org/drexed/genderize-io)
|
5
5
|
|
6
|
-
API wrapper for the
|
6
|
+
API wrapper for the [Genderize.io](https://genderize.io) first name gender determination system.
|
7
|
+
*Be aware that the free API has a limit of 1000 determinations per day.*
|
7
8
|
|
8
9
|
## Installation
|
9
10
|
|
@@ -22,22 +23,27 @@ Or install it yourself as:
|
|
22
23
|
$ gem install genderize-io
|
23
24
|
|
24
25
|
## Usage
|
26
|
+
|
27
|
+
### Lookup Determination
|
28
|
+
|
25
29
|
```ruby
|
26
30
|
# Basic Lookup
|
27
|
-
lookup = Genderize::Io::Lookup.new('kim').
|
28
|
-
lookup = Genderize::Io::Lookup.
|
31
|
+
lookup = Genderize::Io::Lookup.new('kim').determine
|
32
|
+
lookup = Genderize::Io::Lookup.determine('kim')
|
29
33
|
|
30
|
-
# Custom
|
31
|
-
lookup = Genderize::Io::Lookup.
|
32
|
-
|
34
|
+
# Custom Options
|
35
|
+
lookup = Genderize::Io::Lookup.determine('kim', host: 'https://api.genderize.io?api_key=xxx',
|
36
|
+
country_id: 'dk',
|
37
|
+
language_id: 'en')
|
38
|
+
```
|
33
39
|
|
34
|
-
|
35
|
-
lookup = Genderize::Io::Lookup.new('kim', country_id: 'dk').verify
|
36
|
-
lookup = Genderize::Io::Lookup.verify('kim', language_id: 'en')
|
40
|
+
### Lookup Methods
|
37
41
|
|
38
|
-
|
42
|
+
```ruby
|
39
43
|
lookup.url => 'https://api.genderize.io?name=kim'
|
40
|
-
lookup.
|
44
|
+
lookup.data => { 'name' => 'kim', 'gender' => 'female', ... }
|
45
|
+
|
46
|
+
# Data Methods
|
41
47
|
lookup.name => 'kim'
|
42
48
|
lookup.gender => 'female'
|
43
49
|
lookup.probability => 0.94
|
@@ -50,6 +56,32 @@ lookup.x_rate_reset => 13829
|
|
50
56
|
lookup.error => 'sorry, my bad!'
|
51
57
|
```
|
52
58
|
|
59
|
+
### Batch Lookup Determination
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
# Basic Lookup
|
63
|
+
lookup = Genderize::Io::Batch::Lookup.new(['kim', 'jim']).determine
|
64
|
+
lookup = Genderize::Io::Batch::Lookup.determine(['kim', 'jim'])
|
65
|
+
|
66
|
+
# Custom Options
|
67
|
+
lookup = Genderize::Io::Batch::Lookup.determine(['kim', 'jim'], host: 'https://api.genderize.io?api_key=xxx',
|
68
|
+
country_id: 'dk',
|
69
|
+
language_id: 'en')
|
70
|
+
```
|
71
|
+
|
72
|
+
### Batch Lookup Methods
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
lookup.url => 'https://api.genderize.io?name[0]=kim&name[1]=jim'
|
76
|
+
lookup.data => {
|
77
|
+
'rate_limits' => { 'x_rate_limit_limit' => 1069, ... },
|
78
|
+
'responses' => [
|
79
|
+
{ 'name' => 'kim', 'gender' => 'female', ... },
|
80
|
+
{ 'name' => 'jim', 'gender' => 'male', ... }
|
81
|
+
]
|
82
|
+
}
|
83
|
+
```
|
84
|
+
|
53
85
|
## Contributing
|
54
86
|
|
55
87
|
Your contribution is welcome.
|
data/lib/genderize/io.rb
CHANGED
@@ -0,0 +1,68 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'typhoeus'
|
4
|
+
require 'user_agent_db'
|
5
|
+
|
6
|
+
module Genderize
|
7
|
+
module Io
|
8
|
+
class Base
|
9
|
+
|
10
|
+
DEFAULT_HOST ||= 'https://api.genderize.io'
|
11
|
+
HEADER_KEYS ||= %w[x_rate_limit_limit x_rate_limit_remaining x_rate_reset].freeze
|
12
|
+
|
13
|
+
attr_reader :country_id, :data, :host, :language_id, :name, :request
|
14
|
+
|
15
|
+
def initialize(name, host: DEFAULT_HOST, country_id: nil, language_id: nil)
|
16
|
+
@name = name
|
17
|
+
@host = host
|
18
|
+
@country_id = country_id
|
19
|
+
@language_id = language_id
|
20
|
+
@request = nil
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.determine(name, host: DEFAULT_HOST, country_id: nil, language_id: nil)
|
24
|
+
klass = new(name, host: host, country_id: country_id, language_id: language_id)
|
25
|
+
klass.determine
|
26
|
+
klass
|
27
|
+
end
|
28
|
+
|
29
|
+
def url
|
30
|
+
connector = @host.include?('?') ? '&' : '?'
|
31
|
+
"#{@host}#{connector}#{param_name}#{param_country_id}#{param_language_id}"
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def generate_data
|
37
|
+
return if @request.nil?
|
38
|
+
Genderize::Io::Parser::Json.parse(@request.response_body)
|
39
|
+
end
|
40
|
+
|
41
|
+
def generate_headers
|
42
|
+
return if @request.nil?
|
43
|
+
Genderize::Io::Parser::Header.parse(@request.response_headers)
|
44
|
+
end
|
45
|
+
|
46
|
+
def generate_rate_limits
|
47
|
+
headers = generate_headers
|
48
|
+
return if headers.nil?
|
49
|
+
HEADER_KEYS.each_with_object({}) { |key, hash| hash[key] = headers.send(key).to_i }
|
50
|
+
end
|
51
|
+
|
52
|
+
def generate_request
|
53
|
+
return @request unless @request.nil?
|
54
|
+
Typhoeus::Config.user_agent = UserAgentDB.random
|
55
|
+
Typhoeus.get(url, accept_encoding: 'gzip,deflate')
|
56
|
+
end
|
57
|
+
|
58
|
+
def param_country_id
|
59
|
+
"&country_id=#{@country_id}" unless @country_id.nil?
|
60
|
+
end
|
61
|
+
|
62
|
+
def param_language_id
|
63
|
+
"&language_id=#{@language_id}" unless @language_id.nil?
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Genderize
|
4
|
+
module Io
|
5
|
+
module Batch
|
6
|
+
class Lookup < Genderize::Io::Base
|
7
|
+
|
8
|
+
def initialize(name, host: DEFAULT_HOST, country_id: nil, language_id: nil)
|
9
|
+
super(name, host: host, country_id: country_id, language_id: language_id)
|
10
|
+
@data = {
|
11
|
+
'rate_limits' => {},
|
12
|
+
'responses' => []
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
def determine
|
17
|
+
return @data unless @request.nil?
|
18
|
+
|
19
|
+
@request = generate_request
|
20
|
+
@data['responses'] = generate_data
|
21
|
+
@data['rate_limits'] = generate_rate_limits
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def param_name
|
27
|
+
@name.map.with_index { |name, i| "name[#{i}]=#{name}" }.join('&')
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/genderize/io/lookup.rb
CHANGED
@@ -1,82 +1,35 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'typhoeus'
|
4
|
-
require 'user_agent_db'
|
5
|
-
|
6
3
|
module Genderize
|
7
4
|
module Io
|
8
|
-
class Lookup
|
9
|
-
|
10
|
-
DEFAULT_HOST ||= 'https://api.genderize.io'
|
11
|
-
RESPONSE_KEYS ||= %w[
|
12
|
-
count country_id error language_id gender probability x_rate_limit_limit
|
13
|
-
x_rate_limit_remaining x_rate_reset
|
14
|
-
].freeze
|
5
|
+
class Lookup < Genderize::Io::Base
|
15
6
|
|
16
|
-
|
7
|
+
RESPONSE_KEYS ||= %w[count country_id error language_id gender probability].freeze
|
17
8
|
|
18
9
|
def initialize(name, host: DEFAULT_HOST, country_id: nil, language_id: nil)
|
19
|
-
|
20
|
-
@
|
21
|
-
@country_id = country_id
|
22
|
-
@language_id = language_id
|
23
|
-
@response = nil
|
24
|
-
@hash = {}
|
25
|
-
end
|
26
|
-
|
27
|
-
def self.verify(name, host: DEFAULT_HOST, country_id: nil, language_id: nil)
|
28
|
-
klass = new(name, host: host, country_id: country_id, language_id: language_id)
|
29
|
-
klass.verify
|
30
|
-
klass
|
10
|
+
super(name, host: host, country_id: country_id, language_id: language_id)
|
11
|
+
@data = {}
|
31
12
|
end
|
32
13
|
|
33
|
-
def
|
34
|
-
return @
|
14
|
+
def determine
|
15
|
+
return @data unless @request.nil?
|
35
16
|
|
36
|
-
|
37
|
-
@
|
38
|
-
|
39
|
-
generate_hash
|
40
|
-
generate_rate_limits
|
41
|
-
end
|
42
|
-
|
43
|
-
def url
|
44
|
-
connector = @host.include?('?') ? '&' : '?'
|
45
|
-
"#{@host}#{connector}#{param_name}#{param_country_id}#{param_language_id}"
|
17
|
+
@request = generate_request
|
18
|
+
@data = generate_data.merge(generate_rate_limits)
|
46
19
|
end
|
47
20
|
|
48
|
-
|
49
|
-
@
|
21
|
+
HEADER_KEYS.each do |key|
|
22
|
+
define_method(key) { @data[key] }
|
50
23
|
end
|
51
24
|
|
52
25
|
RESPONSE_KEYS.each do |key|
|
53
|
-
define_method(key) { @
|
26
|
+
define_method(key) { @data[key] }
|
54
27
|
end
|
55
28
|
|
56
29
|
private
|
57
30
|
|
58
|
-
def generate_hash
|
59
|
-
return if @response.response_body.nil?
|
60
|
-
@hash = Genderize::Io::Parser::Json.parse(@response.response_body)
|
61
|
-
end
|
62
|
-
|
63
|
-
def generate_rate_limits
|
64
|
-
return if @response.response_headers.nil?
|
65
|
-
headers = Genderize::Io::Parser::Header.parse(@response.response_headers)
|
66
|
-
RESPONSE_KEYS.last(3).each { |key| @hash[key] = headers.send(key).to_i }
|
67
|
-
end
|
68
|
-
|
69
31
|
def param_name
|
70
|
-
|
71
|
-
@name.map.with_index { |name, i| "name[#{i}]=#{name}" }.join('&')
|
72
|
-
end
|
73
|
-
|
74
|
-
def param_country_id
|
75
|
-
"&country_id=#{@country_id}" unless @country_id.nil?
|
76
|
-
end
|
77
|
-
|
78
|
-
def param_language_id
|
79
|
-
"&language_id=#{@language_id}" unless @language_id.nil?
|
32
|
+
"name=#{@name}"
|
80
33
|
end
|
81
34
|
|
82
35
|
end
|
data/lib/genderize/io/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: genderize-io
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juan Gomez
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-11-
|
11
|
+
date: 2017-11-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: typhoeus
|
@@ -145,6 +145,8 @@ files:
|
|
145
145
|
- bin/setup
|
146
146
|
- genderize-io.gemspec
|
147
147
|
- lib/genderize/io.rb
|
148
|
+
- lib/genderize/io/base.rb
|
149
|
+
- lib/genderize/io/batch/lookup.rb
|
148
150
|
- lib/genderize/io/lookup.rb
|
149
151
|
- lib/genderize/io/parser/header.rb
|
150
152
|
- lib/genderize/io/parser/json.rb
|