Skip to content

About

Ruby gem: parse firearm and ammo product titles (brand, model, caliber, capacity, barrel length, rounds per box, cost per round)

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

gunsnprices

Gem Version CI License

Parse firearm and ammunition product titles into structured data: brand, model, caliber, magazine capacity, barrel length, rounds per box and cost per round.

require "gunsnprices"

info = GunsNPrices.parse_title("Glock 19 Gen 5 MOS 9mm 4.02in 15rd Pistol PA195S203MOS")
info.model.model     # => "Glock 19 Gen 5 MOS"
info.caliber.name    # => "9×19mm Parabellum"
info.capacity        # => 15
info.barrel_length   # => 4.02

This is the title parser behind GunsNPrices, a price comparison site that tracks 40+ US gun and ammo stores. Store titles are messy (G19 GEN5 9MM 15RD FS, O/U 12/28 BL/WD 3", Federal 308 150gr ... 20rds), and matching the same product across stores starts with reading them reliably. The model dictionaries and the caliber list in this gem are the ones the site uses to group listings, so you get the same model names you see on gunsnprices.com.

Pure Ruby, no runtime dependencies; the data ships inside the gem. It is a port of the Python package and gives identical results: the test suite checks the same 1,150 recorded store titles as the Python, JavaScript and other versions, and it agrees with Python on all 63,755 titles of the GunsNPrices catalog.

Install

gem install gunsnprices

or in a Gemfile:

gem "gunsnprices", "~> 0.1"

Requires Ruby 3.0 or newer.

What it recognizes

Field Example in a title Result
Firearm or ammo ... 150gr ... 20rds kind == "ammo"
Brand and model (50 brands) G19 Gen5 MOS, part number PA195S203 Glock 19 Gen 5 MOS
Calibers (293) .357 Mag / .38 Spl, 6.5 CM, 12ga, 300 BLK, 7MM RM .357 Mag, .38 S&W Special, ...
Magazine capacity 15+1, 17RD, 13rd/15rd, 10-shot 15, 17, 15, 10
Barrel length, inches 4.02in, 16 1/2", 22 BBL, O/U 12/28 4.02, 16.5, 22.0, 28.0
Rounds per box 50/Box, Box of 20, 1,000 Rounds, 20/25 50, 20, 1000, 20
Cost per round price 449.99 + 1000 Rounds 0.45

Brands with model dictionaries: Barrett, Benelli, Beretta, Bergara, Browning, Cadex, Canik, Charles Daly, Chiappa, Christensen Arms, Cimarron, CMMG, Colt, CVA, CZ, Daniel Defense, Desert Eagle, Diamondback, Fierce, FN, Glock, Great Lakes, Henry, Heritage, HK, Howa, Kel-Tec, Kimber, Kriss, Marlin, Mossberg, Remington, Retay, Rossi, Ruger, Savage, SCCY, Seekins, Shadow Systems, Sig Sauer, Smith & Wesson, Springfield, Stevens, Stoeger, Taurus, Tikka, Tristar, Walther, Weatherby, Winchester.

Usage

Everything at once

info = GunsNPrices.parse_title("Federal American Eagle 5.56x45mm 55gr FMJ 1000 Rounds", price: 449.99)
info.kind            # => "ammo"
info.ammo?           # => true
info.caliber.name    # => "5.56×45mm NATO"
info.rounds          # => 1000
info.cost_per_round  # => 0.45

info.to_h            # plain Hash, nested models and calibers included
info.to_json         # the same JSON as the Python package's to_dict()

GunsNPrices.parse_title(title, brand: nil, mpn: nil, sku: nil, price: nil, kind: nil) returns a frozen GunsNPrices::ProductInfo:

Attribute Type Notes
kind String "firearm" or "ammo"; pass kind: to skip the guess; also firearm? and ammo?
brand String brand of the identified model, else the brand: you passed, else ""
model ModelMatch or nil firearms only
caliber Caliber or nil first caliber in the title
calibers Array<Caliber> all calibers, in order
capacity Integer or nil magazine capacity (for 15+1 it is 15), firearms only
barrel_length Float or nil inches, firearms only
rounds Integer or nil rounds per box, ammo only
cost_per_round Float or nil ammo with price: only

price: can be an Integer, Float, BigDecimal, Rational or a numeric String. Passing brand: (the manufacturer field from a feed) helps with titles that don't name the brand. Passing mpn: lets it decode Glock part numbers when the title says nothing useful:

m = GunsNPrices.identify_model("GLOCK 9MM PISTOL 15RD", brand: "Glock", mpn: "UA4350201")
m.model   # => "Glock 43"
m.source  # => "mpn"

Models

m = GunsNPrices.identify_model("Sig Sauer P365 XL 9mm 3.7\" 12+1 Optic Ready")
[m.brand, m.family, m.model]  # => ["Sig Sauer", "Sig Sauer P365", "Sig Sauer P365 XL"]

GunsNPrices.identify_model("Glock 19 holster")               # => nil
GunsNPrices.identify_model("Umarex Glock 19 CO2 BB pistol")  # => nil

A ModelMatch has brand, family, model (the most specific name), generation, configs (such as MOS, Compact), features (such as Threaded Barrel, FDE) and source ("title" or "mpn"). Accessories, parts, airguns and airsoft are rejected. GunsNPrices.list_brands returns the 50 brands.

Calibers

GunsNPrices.find_calibers("Henry Big Boy .44 Mag/.44 Spl 20in").map(&:name)
# => [".44 Magnum", ".44 S&W Special"]

GunsNPrices.normalize_caliber("308 WIN (7.62X51 NATO)")
# => #<struct GunsNPrices::Caliber id="308win", name=".308 Win", type="rifle",
#      url="https://gunsnprices.com/ammo/rifle-ammo/308-win">

Each Caliber has a stable id, a display name, a type (handgun, rifle, rimfire, shotgun, airgun, muzzleloader) and the url of its GunsNPrices price comparison page. normalize_caliber is meant for caliber fields in product feeds; find_calibers and find_caliber search free text. list_calibers returns all 293 and caliber_by_id("9x19mmparabellum") looks one up.

Numbers

GunsNPrices.magazine_capacity("Springfield Hellcat Pro OSP 9mm 3.7in 15rd/17rd")  # => 17
GunsNPrices.barrel_length('Mossberg 590 Persuader 12/20 BL/SY 3"')               # => 20.0 (3" is the chamber)
GunsNPrices.rounds_per_box("Winchester 308 150gr PowerMax Bonded 20rds")          # => 20
GunsNPrices.cost_per_round(31.99, 20)                                             # => 1.5995
GunsNPrices.looks_like_ammo?("Federal 9mm 115gr FMJ 50/Box")                      # => true

Command line

gunsnprices "Ruger GP100 .357 Mag / .38 Special 4.2in 6-shot Revolver"
gunsnprices --price 18.99 "Federal 9mm 115gr FMJ 50/Box"
gunsnprices < titles.txt > parsed.jsonl      # one title per line in, JSON Lines out

Options: --brand, --mpn, --sku, --price, --kind firearm|ammo.

In a Rails, Solidus or Spree app

The gem has no Rails, Solidus or Spree integration of its own: these are plain Ruby calls you can make from a model callback, an importer or a background job.

# app/models/listing.rb (your own columns)
class Listing < ApplicationRecord
  before_save :fill_specs, if: :will_save_change_to_title?

  private

  def fill_specs
    info = GunsNPrices.parse_title(title, brand: manufacturer, price: price)
    self.caliber        = info.caliber&.name
    self.capacity       = info.capacity
    self.barrel_length  = info.barrel_length
    self.rounds_per_box = info.rounds
    self.cost_per_round = info.cost_per_round
  end
end
# app/jobs/caliber_properties_job.rb (Solidus or Spree product properties)
class CaliberPropertiesJob < ApplicationJob
  def perform(product_ids)
    Spree::Product.where(id: product_ids).find_each do |product|
      info = GunsNPrices.parse_title(product.name, price: product.price)
      product.set_property("Caliber", info.caliber.name) if info.caliber
      product.set_property("Capacity", info.capacity.to_s) if info.capacity
    end
  end
end

To compile the rules at boot instead of on the first call (for example before Puma forks its workers), call GunsNPrices.warm_up in an initializer.

Performance

The data is read and the rules are compiled on the first call (about 0.25 s), or when you call GunsNPrices.warm_up. After that parse_title takes about 0.6 ms per title on one core (Ruby 4.0, Apple Silicon, measured over the 63,755 catalog titles). Every function is thread-safe and returns frozen objects.

How it matches Python

The rules are written for Python's re module. The gem translates them to Ruby's regular expression engine (Onigmo) so that they match exactly the way Python does. On ASCII text, which is almost every store title, they run natively. On other text, \w, \s and \d follow Python's Unicode definitions, case-insensitive matching follows Python's (i also matches ı and İ, ss does not match ß), and word boundaries next to combining marks are handled. lower(), html.unescape() and round() behave like Python's too. The Unicode tables come from Python 3.13 (Unicode 15.1), the same as the other ports, so the results don't depend on your Ruby version.

Accuracy

Measured with the reference implementation on 50,202 firearm and 13,555 ammunition listings from 40+ stores on gunsnprices.com (October 2026), against the attributes stored on the site:

What Found in the title Right when found
Model (32,317 listings with a known model) 99.97% 99.8% same name as the site
Caliber, firearms 81% 97.8%
Caliber, ammo 91% 94.9%
Magazine capacity 46% 95.6%
Barrel length 58% 94.4%
Rounds per box 80% 96.6%

"Found" is limited by the titles themselves: many stores don't put the caliber or barrel length in the title at all. Most of the "wrong" cases we checked by hand are errors in the store data, not in the parser.

Data

The model dictionaries and caliber list ship inside the gem (data/*.json, the same files as the Python package). The same data, with listing counts and price levels, is published as open datasets:

The same parser is available for Python, JavaScript, PHP, Rust, Java, .NET, Dart and Elixir, and as plugins for WordPress, Joomla, Drupal and other platforms: gunsnprices.com/developers.

Limitations

  • Titles from US retailers, in English.
  • Models are covered for the 50 brands above. Other brands still get calibers and numbers.
  • Built for firearms and ammunition. Magazines, parts and accessories get calibers and numbers but no model.

Development

bundle install
bundle exec rake test        # 1,150 recorded titles + unit tests

# compare every public function with the Python package (golden titles with Unicode edits;
# add --catalog DIR with lib_guns.tsv / lib_ammo.tsv for more titles)
pip install gunsnprices==0.1.2
python3.13 tools/make_corpus.py /tmp/corpus.jsonl
ruby -Ilib tools/crosscheck.rb /tmp/corpus.jsonl

# regenerate the Unicode and HTML tables from Python 3.13
python3.13 tools/gen_tables.py > lib/gunsnprices/tables.rb

License

The code is under the MIT License. The model and caliber data in data/ is under CC BY 4.0: you can use it for anything, including commercial use, if you credit GunsNPrices (gunsnprices.com) with a link.

Questions, corrections and missing models: open an issue or write to app@gunsnprices.com. Current gun and ammo prices: gunsnprices.com.

About

Ruby gem: parse firearm and ammo product titles (brand, model, caliber, capacity, barrel length, rounds per box, cost per round)

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages