Sha256: 0badcbbcb18c9e907542e20561034ddc04ec9d9a39ea3248fb477b004716cd14

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 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, :aliases => '-l', :default => :us
    def all(asin)
      lookup(asin, options[:locale])
    end
    map 'a' => 'all'

    desc 'kosher', 'Looks up kosher offers for an ASIN'
    method_option :locale, :aliases => '-l', :default => :us
    def kosher(asin)
      lookup(asin, options[: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.10 lib/fassbinder/cli.rb