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 ammitto

Basic 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_cache

Rails 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
end

API Reference

MethodParametersDescription
Ammitto.searchterm, sources:, limit:, offset:Search entities by name. Returns a ResultSet of entity objects.
Ammitto.sourcesnoneThe source codes the gem knows about, as symbols.
Ammitto.cache_statusnonePer-source cache state: whether it is present and how old.
Ammitto.refresh_cachesources:, all:, force:Re-download cached source data. Defaults to every source.
Ammitto.schemanoneThe JSON-LD context the published documents reference.
Ammitto.configureblockSet 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_countHow many came back, and how many matched.
empty? / any?Whether anything matched.
by_entity_type / by_authority / by_statusSlice the set without querying again.
entity_types / authoritiesThe distinct values present in this set.
to_json / to_json_ldSerialize the set.

Configuration Options

OptionDescriptionDefault
api_base_urlWhere source data is downloaded fromthe live API host
cache_dirWhere downloaded source data is cached~/.ammitto
cache_ttlCache lifetime in seconds3600
connection_timeoutConnect timeout in seconds10
read_timeoutRead timeout in seconds30
verboseLog what the client is doingfalse