Created
December 22, 2013 15:35
-
-
Save hmsk/8084235 to your computer and use it in GitHub Desktop.
Communicate with WebPay on CLI
by `ruby webpay-pry.rb`
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
# coding: utf-8 | |
# ref: http://sho.tdiary.net/20131128.html | |
require 'pry' | |
require 'webpay' | |
require 'singleton' | |
module WebPayConsole | |
class ObjectRepository | |
MAX_COUNT_PAGE_VOL = 100 | |
attr_reader :charges, :customers | |
def initialize | |
@charges = load_objects(WebPay::Charge) | |
@customers = load_objects(WebPay::Customer) | |
end | |
private | |
def load_objects(klass) | |
page = 1 | |
entities = [] | |
loop do | |
acquisitions = klass.all(count: MAX_COUNT_PAGE_VOL, offset: (page - 1) * MAX_COUNT_PAGE_VOL) | |
break if acquisitions.data.size == 0 | |
entities += acquisitions.data | |
page += 1 | |
sleep 1 | |
end | |
entities | |
end | |
end | |
class CLI | |
include Singleton | |
def initialize | |
@@repos = nil | |
webpay_command = Pry::CommandSet.new do | |
block_command 'key' do |*args| | |
output.puts WebPay.api_key = args[0] | |
output.puts 'initializing...' | |
@@repos = ObjectRepository.new | |
output.puts 'loaded...' | |
end | |
block_command 'charges' do |*args| | |
@@repos.charges.each_with_index do |charge, index| | |
output.puts "[#{index}] #{charge.id} #{charge.amount - charge.amount_refunded}yen, #{charge.description}" | |
end | |
output.puts 'You can retrieve charge from above with: e.g. `charge 1`' | |
end | |
block_command 'customers' do |*args| | |
@@repos.customers.each_with_index do |customer, index| | |
output.puts "[#{index}] #{customer.id} #{customer.description}" | |
end | |
output.puts 'You can retrieve customer from above with: e.g. `customer 1`' | |
end | |
block_command /\A(charge|customer)/, keep_retval: true do |*args| | |
@@repos.send(captures[0] + "s")[args[1].to_i] | |
end | |
block_command 'help' do | |
output.puts "charges : show charges" | |
output.puts "charge num: show charges with page number" | |
output.puts "customers : show customers" | |
output.puts "customer num: show customers with page number" | |
end | |
end | |
Pry::Commands.import webpay_command | |
Pry.prompt = [proc { 'WebPay> ' }, proc { 'WebPay* ' }] | |
end | |
def execute_pry | |
puts "At first, set secret key with: key 'test_secret_xxxxxxxxxx'\n\n" | |
puts "Type 'help' to help. type 'exit' to terminate.\n\n" | |
pry | |
end | |
end | |
end | |
console = WebPayConsole::CLI.instance | |
console.execute_pry |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment