Ruby Gem
The Ammitto Ruby gem provides a simple interface for accessing sanctions data in your Ruby and Rails applications.
Installation
Gemfile
# Add to Gemfile
gem 'ammitto'
# Or install directly
gem install ammittoBasic Usage
Basic Usage
require 'ammitto'
# Search every source. Returns an Ammitto::Search::ResultSet.
results = Ammitto.search('bin laden')
# Narrow it: :sources, :limit and :offset are the options search accepts
eu_results = Ammitto.search('bin laden', sources: [:eu], limit: 25)
puts "Found #{results.size} of #{results.total_count}"
# Entries are entity objects. display_name is the string; primary_name
# returns a NameVariant, which is rarely what you want to print.
results.each do |entity|
puts "#{entity.entity_type}: #{entity.display_name}"
end
# The set can slice itself without a second query
vessels = results.by_entity_type('vessel')
puts results.entity_types.inspect
# Which sources exist, and what is cached locally
puts Ammitto.sources.inspect
puts Ammitto.cache_status.inspect
# Refresh the local cache (~/.ammitto by default)
Ammitto.refresh_cacheRails Integration
Rails Configuration
# config/initializers/ammitto.rb
Ammitto.configure do |config|
# Set this to the host that serves the API. Whatever you point it at
# must answer directly: the client does not follow redirects, so a
# host that 301s fails every download.
config.api_base_url = 'https://www.ammitto.org/api/v1'
config.cache_dir = Rails.root.join('tmp', 'ammitto').to_s
config.cache_ttl = 3600 # seconds
end
# Usage in a Rails model or service
class SanctionsChecker
def check_name(name)
Ammitto.search(name, limit: 1).any?
end
def matches_for(name)
Ammitto.search(name).map do |entity|
{ name: entity.display_name, type: entity.entity_type }
end
end
endAPI Reference
| Method | Parameters | Description |
|---|---|---|
| Ammitto.search | term, sources:, limit:, offset: | Search entities by name. Returns a ResultSet of entity objects. |
| Ammitto.sources | none | The source codes the gem knows about, as symbols. |
| Ammitto.cache_status | none | Per-source cache state: whether it is present and how old. |
| Ammitto.refresh_cache | sources:, all:, force: | Re-download cached source data. Defaults to every source. |
| Ammitto.schema | none | The JSON-LD context the published documents reference. |
| Ammitto.configure | block | Set the options in the table below. |
Working with results
Ammitto.search returns a ResultSet. Its entries are entity objects, so reach for display_name to print a name — primary_name hands back a NameVariant object.
| each / map / first / last / [] | Iterate the entity objects. |
| size / count / total_count | How many came back, and how many matched. |
| empty? / any? | Whether anything matched. |
| by_entity_type / by_authority / by_status | Slice the set without querying again. |
| entity_types / authorities | The distinct values present in this set. |
| to_json / to_json_ld | Serialize the set. |
Configuration Options
| Option | Description | Default |
|---|---|---|
| api_base_url | Where source data is downloaded from | the live API host |
| cache_dir | Where downloaded source data is cached | ~/.ammitto |
| cache_ttl | Cache lifetime in seconds | 3600 |
| connection_timeout | Connect timeout in seconds | 10 |
| read_timeout | Read timeout in seconds | 30 |
| verbose | Log what the client is doing | false |