-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a CLI tool to test receipts from the filesystem.
- Loading branch information
Showing
6 changed files
with
177 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters