Created
February 14, 2012 03:25
-
-
Save js1972/1823195 to your computer and use it in GitHub Desktop.
Test code - comparing receipt-bank receipts with mongo db
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
require 'bundler' | |
Bundler.require | |
# This is a demo program for the receipt-bank.com rest api. | |
class ReceiptBank | |
def initialize(u, p) | |
@user, @password = u, p | |
end | |
def login | |
puts 'Logging in to receipt-bank.com...' | |
response = RestClient.post("https://www.receipt-bank.com/api/login", :login => @user, :password => @password) | |
session = JSON.load(response) | |
if session["errorcode"] == 401 | |
puts "Unauthorised!" | |
raise "Unauthorised" | |
end | |
@sessionid = session['sessionid'] | |
puts "Logged in with Session ID = #{session['sessionid']}" | |
end | |
# Just get ALL receipts for now. This is not client (mobile app) facing and unless there is thousands of receipts there won't be much data | |
# | |
def get_receipts(new_receipts_only=nil, receiptid=nil) | |
if !@sessionid | |
login | |
end | |
# list all receipts | |
response = RestClient.post("https://www.receipt-bank.com/api/receipts", :sessionid => @sessionid, :new => new_receipts_only, :id => receiptid) | |
JSON.load(response) | |
#if receipts['errormessage'] == 'Session expired' | |
# session = login | |
# response = RestClient.post("https://www.receipt-bank.com/api/receipts", :sessionid => @sessionid, :new => new_receipts_only, :id => receiptid) | |
# receipts = JSON.load(response) | |
# puts JSON.pretty_generate(receipts) | |
#end | |
end | |
def get_receipt_status(receipt_id) | |
if !@sessionid | |
login | |
end | |
#get receipt status - returns an array of json strings (one in this case) | |
json_array = "[{\"receiptid\":\"#{receipt_id}\"}]" | |
response = RestClient.post("https://www.receipt-bank.com/api/getReceiptsStatus", :sessionid => @sessionid, :receipts => "#{json_array}") | |
array_vals = JSON.load(response) | |
array_vals[0]["status"] | |
end | |
def logout | |
if @sessionid | |
response = RestClient.post("https://www.receipt-bank.com/api/logout", :sessionid => @sessionid) | |
@sessionid = nil | |
end | |
end | |
end | |
class Receipt | |
include MongoMapper::Document | |
key :receiptid, String | |
key :amount, String | |
key :currency, String | |
key :gst, String | |
key :category, String | |
key :project, String | |
key :invcoice_no, String | |
key :date, String | |
key :type, String | |
key :status, String | |
key :payee, String | |
end | |
if __FILE__ == $PROGRAM_NAME | |
# Mongodb can be started at the command line with "mongod --dbpath=data/" - execute this within the folder of the application! | |
MongoMapper.connection = Mongo::Connection.from_uri(ENV['MONGO_URI']) | |
MongoMapper.database = ENV['MONGO_DB'] | |
if MongoMapper.connection == nil | |
puts "Not connected to a MondoDB..." | |
return | |
else | |
puts "Connected to MongoDB..." | |
end | |
# UN-COMMENT THIS TO DELETE ALL DOCUMENTS!!! | |
#Receipt.delete_all | |
# lets list all the documents in our mongodb first | |
puts("MongoDB document count: " << Receipt.all.count.to_s) | |
query = Receipt.fields(:receiptid, :amount).all | |
query.each {|q| puts "#{q.receiptid} - #{q.amount}"} | |
# now login to receipt-bank.com | |
begin | |
rb = ReceiptBank.new('jason@jaylin.com.au', 'sophie05') | |
rescue | |
puts "ERROR: Unable to login to receipt-bank.com" | |
end | |
#get all the receipts from receipt-bank | |
rb_list = rb.get_receipts() | |
puts "Number of receipts returned: #{rb_list["size"]}" | |
rb_array = rb_list["receipts"] | |
puts "\nListing of receipt id's:" | |
# loop through each receipt and insert it into mongo if it doesnt already exist | |
rb_array.each do |r| | |
puts "Receipt-bank receiptid: "<< r['id'].to_s | |
puts "Number of receipts matching this id in mongo: " << Receipt.where(:receiptid => r['id']).count.to_s | |
#check if the receipt already exists in Mongodb? | |
if Receipt.all(:receiptid => r['id']).count == 0 | |
puts "saving new receipt in mongodb" | |
#receipt = Receipt.create({ | |
# :receiptid => r["id"], | |
# :amount => r["base_total"], | |
# :currency => r["base_currency"], | |
# :gst => r["vat_amount"], | |
# :category => r["category"], | |
# :project => r["project"], | |
# :invoice_no => r["invoice_number"], | |
# :date => r["date"], | |
# :type => r["type"], | |
# :status => r["status"], | |
# :payee => r["payee"] }) | |
# | |
#receipt.save(:safe => true) #safe raises an error if the save is not successful | |
else | |
puts "receipt already exists - do nothing" | |
end | |
end | |
# ask user for receipt number | |
#puts "" | |
#print "Enter a receipt id to get its status: " | |
#receiptid = gets.chomp! | |
# get receipt status | |
#status = rb.get_receipt_status(receiptid) | |
#puts status | |
# logout - not really necessary as it will timeout anyway after approx 1hr | |
puts "logging out..." | |
puts rb.logout | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The Gemfile includes:
source :rubygems
gem 'rest-client'
gem 'json'
gem 'mongo_mapper'
gem 'log_buddy'
and .rvmrc:
rvm use ruby-1.9.3-p0
export MONGO_URI=mongodb://127.0.0.1:27017
export MONGO_DB=receipt-db