Permalink
Browse files

Add a CLI tool to test receipts from the filesystem.

  • Loading branch information...
Manfred committed Sep 18, 2012
1 parent f0148e0 commit 9f340df4cbebe234ad8b2e61e141e5f18e7194a4
Showing with 177 additions and 4 deletions.
  1. +7 −0 bin/oja
  2. +55 −0 lib/oja/cli.rb
  3. +1 −1 lib/oja/mock.rb
  4. +36 −0 lib/oja/option_parser.rb
  5. +33 −0 spec/cli_spec.rb
  6. +45 −3 spec/preamble.rb
View
@@ -0,0 +1,7 @@
+#!/usr/bin/env ruby
+
+$:.unshift File.expand_path('../../lib', __FILE__)
+
+require 'oja/cli'
+
+Oja::CLI.run(ARGV)
View
@@ -0,0 +1,55 @@
+require 'oja/option_parser'
+require 'oja'
+
+module Oja
+ class CLI
+ def initialize(argv)
+ @options, @argv = Oja::OptionParser.parse(argv)
+ end
+
+ def usage
+ puts "Usage: #{File.basename($0)} <receipt-file>"
+ end
+
+ def receipt_filename
+ @argv[0]
+ end
+
+ def print_receipt_details(receipt_data)
+ receipt_data.each do |key, value|
+ puts "#{key}: #{value}"
+ end
+ end
+
+ def check_receipt
+ if response = Oja.verify_filename(receipt_filename)
+ if response.active?
+ puts "[!] Receipt appears to be valid and active"
+ puts
+ print_receipt_details(response.receipt_data)
+ else
+ puts "[!] Receipt is invalid (#{response.humanized_status})"
+ end
+ else
+ log("[!] Apple Store seems inacessible")
+ end
+ end
+
+ def run
+ if receipt_filename
+ check_receipt
+ else
+ usage
+ end
+ end
+
+ def log(message)
+ $stderr.puts(message)
+ end
+
+ def self.run(argv)
+ cli = new(argv.dup)
+ cli.run
+ end
+ end
+end
View
@@ -27,7 +27,7 @@ class << self
def self.next_response_arguments
if responses.empty?
- [200, { status: 0, receipt: '' }]
+ [200, { status: 0, receipt: {}}]
else
responses.pop
end
View
@@ -0,0 +1,36 @@
+module Oja
+ class OptionParser
+ # Parses ARGV from a Ruby script and returns options as a hash and
+ # arguments as a list.
+ #
+ # OptionParser.parse(%w(create --username manfred)) #=>
+ # [{"username"=>"manfred"}, ["create"]]
+ def self.parse(argv)
+ return [{},[]] if argv.empty?
+
+ options = {}
+ rest = []
+ switch = nil
+
+ for value in argv
+ bytes = value.respond_to?(:bytes) ? value.bytes.first(2) : [value[0], value[1]]
+ # value is a switch
+ if bytes[0] == 45
+ switch = value.slice((bytes[1] == 45 ? 2 : 1)..-1)
+ options[switch] = nil
+ else
+ if switch
+ # we encountered another switch so this
+ # value belongs to the last switch
+ options[switch] = value
+ switch = nil
+ else
+ rest << value
+ end
+ end
+ end
+
+ [options, rest]
+ end
+ end
+end
View
@@ -0,0 +1,33 @@
+require File.expand_path('../preamble', __FILE__)
+require 'oja/mock'
+require 'oja/cli'
+
+describe Oja::CLI do
+ before do
+ Peck::Context::Collector.reset!
+ end
+
+ it "verifies a receipt from disk" do
+ Oja::Mock.responses << [200, { status: Oja::Response.status_code(:active), receipt: { 'product_id' => 'day' }}]
+ cli = Oja::CLI.new([receipt_filename('receipt')])
+ output = capture_stdout do
+ cli.run
+ end.to_s
+ output.should.include 'Receipt appears to be valid and active'
+ output.should.include 'product_id'
+ end
+
+ it "shows a message when the receipt is invalid" do
+ Oja::Mock.responses << [200, { status: Oja::Response.status_code(:inactive) }]
+ output = oja(receipt_filename('receipt'))
+ output.should.include 'Receipt is invalid (Inactive)'
+ end
+
+ private
+
+ def oja(*args)
+ capture_stdout do
+ Oja::CLI.run(args)
+ end
+ end
+end
View
@@ -1,10 +1,42 @@
-require 'bundler/setup'
-require 'peck/flavors/vanilla'
-
$:.unshift File.expand_path('../../lib', __FILE__)
require 'oja'
+require 'bundler/setup'
+require 'peck/flavors/vanilla'
+
class Peck::Context
+ class Collector
+ attr_accessor :written
+
+ def self.instance
+ @instance ||= new
+ end
+
+ def initialize
+ reset!
+ end
+
+ def puts(line=nil)
+ @written << "#{line}\n"
+ end
+
+ def write(line=nil)
+ @written << line
+ end
+
+ def to_s
+ @written.join("\n")
+ end
+
+ def reset!
+ @written = []
+ end
+
+ def self.reset!
+ instance.reset!
+ end
+ end
+
def receipt_filename(name)
filename = File.expand_path("../fixtures/receipts/#{name}.txt", __FILE__)
if File.exist?(filename)
@@ -17,4 +49,14 @@ def receipt_filename(name)
def receipt_data(name)
File.read(receipt_filename(name))
end
+
+ def capture_stdout(&block)
+ stdout = $stdout
+ $stdout = Collector.instance
+ Collector.reset!
+ block.call
+ ensure
+ $stdout = stdout
+ return Collector.instance.to_s
+ end
end

0 comments on commit 9f340df

Please sign in to comment.