Class: Demografix::Client
- Inherits:
-
Object
- Object
- Demografix::Client
- Defined in:
- lib/demografix/client.rb
Overview
Synchronous client for the three Demografix APIs: genderize, agify, and nationalize. One instance covers all three services. Quota is read from the returned value or a raised error, never cached on the client.
Constant Summary collapse
- HOSTS =
Per-service hosts. Hardcoded constants, not options.
{ genderize: "https://api.genderize.io", agify: "https://api.agify.io", nationalize: "https://api.nationalize.io" }.freeze
- USER_AGENT =
"demografix-ruby/#{VERSION}"- MAX_BATCH =
10- DEFAULT_TIMEOUT =
10
Instance Method Summary collapse
-
#agify(name, country_id: nil) ⇒ Object
— agify ————————————————————-.
- #agify_batch(names, country_id: nil) ⇒ Object
-
#genderize(name, country_id: nil) ⇒ Object
— genderize ———————————————————.
- #genderize_batch(names, country_id: nil) ⇒ Object
-
#initialize(api_key:, timeout: DEFAULT_TIMEOUT) ⇒ Client
constructor
A new instance of Client.
-
#nationalize(name) ⇒ Object
— nationalize ——————————————————-.
- #nationalize_batch(names) ⇒ Object
Constructor Details
#initialize(api_key:, timeout: DEFAULT_TIMEOUT) ⇒ Client
Returns a new instance of Client.
28 29 30 31 32 33 34 |
# File 'lib/demografix/client.rb', line 28 def initialize(api_key:, timeout: DEFAULT_TIMEOUT) key = api_key.to_s raise ValidationError.new("api_key is required", status: 422) if key.strip.empty? @api_key = key @timeout = timeout end |
Instance Method Details
#agify(name, country_id: nil) ⇒ Object
— agify ————————————————————-
51 52 53 54 55 |
# File 'lib/demografix/client.rb', line 51 def agify(name, country_id: nil) body, quota = request(:agify, [name], country_id: country_id, batch: false) pred = parse_agify(single(body)) AgifyResult.new(**pred.to_h, quota: quota) end |
#agify_batch(names, country_id: nil) ⇒ Object
57 58 59 60 |
# File 'lib/demografix/client.rb', line 57 def agify_batch(names, country_id: nil) body, quota = request(:agify, validate_batch(names), country_id: country_id, batch: true) Batch.new(results: array(body).map { |o| parse_agify(o) }, quota: quota) end |
#genderize(name, country_id: nil) ⇒ Object
— genderize ———————————————————
38 39 40 41 42 |
# File 'lib/demografix/client.rb', line 38 def genderize(name, country_id: nil) body, quota = request(:genderize, [name], country_id: country_id, batch: false) pred = parse_genderize(single(body)) GenderizeResult.new(**pred.to_h, quota: quota) end |
#genderize_batch(names, country_id: nil) ⇒ Object
44 45 46 47 |
# File 'lib/demografix/client.rb', line 44 def genderize_batch(names, country_id: nil) body, quota = request(:genderize, validate_batch(names), country_id: country_id, batch: true) Batch.new(results: array(body).map { |o| parse_genderize(o) }, quota: quota) end |
#nationalize(name) ⇒ Object
— nationalize ——————————————————-
64 65 66 67 68 |
# File 'lib/demografix/client.rb', line 64 def nationalize(name) body, quota = request(:nationalize, [name], batch: false) pred = parse_nationalize(single(body)) NationalizeResult.new(**pred.to_h, quota: quota) end |
#nationalize_batch(names) ⇒ Object
70 71 72 73 |
# File 'lib/demografix/client.rb', line 70 def nationalize_batch(names) body, quota = request(:nationalize, validate_batch(names), batch: true) Batch.new(results: array(body).map { |o| parse_nationalize(o) }, quota: quota) end |