Sha256: 868f9afae5d074f2a5bc466300f7262709459bb2e8f992b76dce39d49ca4a24c

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

require 'pp'
require 'thor'
require 'fassbinder'

module Fassbinder
  class CLI < Thor
    desc 'all', 'Looks up all offers for an ASIN'
    method_option :locale, :required => true, :aliases => '-l'
    def all(asin)
      locale = options[:locale] || :us
      lookup(asin, locale)
    end
    map 'a' => 'all'

    desc 'kosher', 'Looks up kosher offers for an ASIN'
    method_option :locale, :required => true, :aliases => '-l'
    def kosher(asin)
      locale = options[:locale] || :us
      lookup(asin, locale, true)
    end
    map 'k' => 'kosher'

    private

    def lookup(asin, locale, kosher_only=false)
      request = Request.new(credentials)
      request.locale = locale
      request.batchify(asin)
      offers = request.get.to_a.first.offers

      offers.select!(&:kosher?) if kosher_only

      offers.each do |offer|
        puts offer.kosher? ? 'kosher' : 'unkosher'

        offer.id = offer.id[0, 24] + '...'

        description = offer.item.description.text
        if description.size > 77
          offer.item.description.text = description[0, 77] + '...'
        end

        pp offer
        puts
      end
    end
    def credentials
      { 'key'    => ENV['AMAZON_KEY'],
        'secret' => ENV['AMAZON_SECRET'] }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fassbinder-0.0.9 lib/fassbinder/cli.rb