/stockfish

Primary LanguageRubyMIT LicenseMIT

Stockfish 🐟

Stockfish is a Ruby library to map JSON documents using JQ filters. It's highly opinionated and expects the JSON format generated by Stockpile.

Philosophy

  • Defined functions MUST be added to top of jq filter.
  • Functions defined directly on class are always included in jq filter.
  • Functions from subclasses will be included if depends_on :func_name is used.
  • When defining a function, :depends_on will be rendered before.

Example

Stockfish::Base.define_function(:parse_url) do
  'def parse_url: ...'
end

Stockfish::Base.define_function(:parse_query) do
  'def parse_query: ...'
end

class MyParser < Stockfish::Base

  depends_on :from_minor_currency

  DEFAULT_MAPPINGS = {
    id: 'SKU',
    title: 'name',
    price: '.price | from_minor_currency'
  }

  define_function(:querystring, depends_on: [:parse_url, :parse_query]) do
    "def querystring(f): parse_url | .query | parse_query | .[f];"
  end

  after_filter do
    'with_entries( select( .value != null ) )'
  end

end

parser = MyParser.new(custom_mappings: { id: 'GTIN' })
parser.jq_filter # => .....