Skip to content

Commit

Permalink
Add a CLI tool to test receipts from the filesystem.
Browse files Browse the repository at this point in the history
  • Loading branch information
Manfred committed Sep 18, 2012
1 parent f0148e0 commit 9f340df
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 4 deletions.
7 changes: 7 additions & 0 deletions bin/oja
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env ruby

$:.unshift File.expand_path('../../lib', __FILE__)

require 'oja/cli'

Oja::CLI.run(ARGV)
55 changes: 55 additions & 0 deletions lib/oja/cli.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/oja/mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions lib/oja/option_parser.rb
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions spec/cli_spec.rb
Original file line number Diff line number Diff line change
@@ -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
48 changes: 45 additions & 3 deletions spec/preamble.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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.