Population Data API - Ruby Gem

Get comprehensive population data for any country including total population, growth rate, density, urban/rural breakdown, and life expectancy.

Installation

Add this line to your application's Gemfile:

gem 'apiverve_population'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install apiverve_population

Getting Started

Get your API key at APIVerve

Basic Usage

require 'apiverve_population'

# Initialize the client
client = APIVerve::Population::Client.new(api_key: "YOUR_API_KEY")

# Make a request
response = client.execute({
  country: "US",
  year: 2023
})

# Print the response
puts response

Error Handling

begin
  response = client.execute({ country: "US", year: 2023 })
  puts response["data"]
rescue APIVerve::Population::ValidationError => e
  puts "Validation error: #{e.errors.join(', ')}"
rescue APIVerve::Population::APIError => e
  puts "API error: #{e.message}"
  puts "Status code: #{e.status_code}"
end

Debug Mode

# Enable debug logging
client = APIVerve::Population::Client.new(
  api_key: "YOUR_API_KEY",
  debug: true
)

Example Response

{
  "status": "ok",
  "error": null,
  "data": {
    "country": "US",
    "countryISO3": "USA",
    "countryName": "United States",
    "year": 2023,
    "population": 334914895,
    "populationFormatted": "334.91 million",
    "growthRate": 0.53,
    "density": 37.13,
    "densityUnit": "per sq km",
    "urbanPercent": 83.08,
    "ruralPercent": 16.92,
    "urbanPopulation": 278284512,
    "ruralPopulation": 56630383,
    "lifeExpectancy": 77.28,
    "lastUpdated": "2024-01-15T04:00:00.000Z"
  },
  "code": 200
}

Documentation

For more information, visit the API Documentation.

Support

License

This gem is available under the MIT License.