Sha256: 69a4c92cd987ad1ea5db9d5d3b361f082bd6b685f4d39ccd05d5a7024abc7de9

Contents?: true

Size: 1.95 KB

Versions: 4

Compression:

Stored size: 1.95 KB

Contents

module Fletcher
  module CLI
    class Utility < Thor
      include Thor::Actions

      method_option :format, :aliases => "-f", :type => :string, :default => :yaml
      method_option :only, :aliases => "-o", :type => :string, :desc => "Fetch a single attribute [name|description|image|price]", :banner => "ATTRIBUTE"
      method_option :debug, :aliases => "-d", :type => :boolean, :desc => "Print debug information: http response, etc."

      desc 'fetch [URL]', "Fetch Product information\n\n" + 
                          "Example:\n\s\sfletcher fetch 'http://www.amazon.com/gp/product/B004HZYA6E/'"
      def fetch(url)
        product = Fletcher.fetch url

        # Prep output
        output_hash = Hash.new
        output_hash["name"] = product.name
        output_hash["description"] = product.description if product.description
        output_hash["price"] = product.price.format if product.price
        if product.image
          output_hash["image"] = Hash.new 
          output_hash["image"]["src"] = product.image.src 
        end 
        output_hash["url"] = product.url

        # Get single attribute
        if options[:only]
          case options[:only]
          when "image"
            value = output_hash["image"]["src"]
          else
            value = output_hash[options[:only]]
          end 
          value ? say(value.to_s) : say("Unknown attribute: #{options[:only]}")
        else 
          say output_hash.to_yaml
        end

        if options[:debug]
          say "user_agent: #{Fletcher::USER_AGENT}"
          say "response:\n\n"
          say product.doc.to_s
        end        
      end

      desc "websites", "Get a list of supported websites"
      def websites
        say "Supported Websites:"
        for model in Fletcher.models.sort
          say "\t#{model.to_s.capitalize}"
        end 
      end 

      map '-v' => :version
      desc "version", "Get VERSION"
      def version
        say Fletcher.version
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
fletcher-0.6.9 lib/fletcher/cli/utility.rb
fletcher-0.6.8 lib/fletcher/cli/utility.rb
fletcher-0.6.7 lib/fletcher/cli/utility.rb
fletcher-0.6.6 lib/fletcher/cli/utility.rb