changeset 402:62b457c50594

add stripe; change Luan.values to only return values, not indexes;
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 28 Apr 2015 22:38:31 -0600
parents 322c03d85ada
children 637f7ad85654
files core/src/luan/modules/BasicLuan.java core/src/luan/modules/Io.luan core/src/luan/modules/JavaLuan.java scripts/cp-luan stripe/ext/gson-2.2.4.jar stripe/ext/stripe-java-1.27.0.jar stripe/src/luan/modules/stripe/Stripe.luan stripe/src/luan/modules/stripe/StripeLuan.java web/src/luan/modules/web/shell.luan
diffstat 9 files changed, 128 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
diff -r 322c03d85ada -r 62b457c50594 core/src/luan/modules/BasicLuan.java
--- a/core/src/luan/modules/BasicLuan.java	Tue Apr 28 13:21:50 2015 -0600
+++ b/core/src/luan/modules/BasicLuan.java	Tue Apr 28 22:38:31 2015 -0600
@@ -194,9 +194,9 @@
 			int i = 0;
 
 			@Override public Object call(LuanState luan,Object[] unused) {
-				if( ++i > args.length )
+				if( i >= args.length )
 					return LuanFunction.NOTHING;
-				return new Object[]{i,args[i-1]};
+				return args[i++];
 			}
 		};
 	}
diff -r 322c03d85ada -r 62b457c50594 core/src/luan/modules/Io.luan
--- a/core/src/luan/modules/Io.luan	Tue Apr 28 13:21:50 2015 -0600
+++ b/core/src/luan/modules/Io.luan	Tue Apr 28 22:38:31 2015 -0600
@@ -17,7 +17,7 @@
 
 function print_to(out,...)
 	local list = {}
-	for _,v in Luan.values(...) do
+	for v in Luan.values(...) do
 		list[#list+1] = to_string(v)
 		list[#list+1] = '\t'
 	end
diff -r 322c03d85ada -r 62b457c50594 core/src/luan/modules/JavaLuan.java
--- a/core/src/luan/modules/JavaLuan.java	Tue Apr 28 13:21:50 2015 -0600
+++ b/core/src/luan/modules/JavaLuan.java	Tue Apr 28 22:38:31 2015 -0600
@@ -380,7 +380,7 @@
 					return fn.rawCall(luan,args);
 				} catch(IllegalArgumentException e) {}
 			}
-			throw luan.exception("no method matched args");
+			throw luan.exception("no method matched args: "+Arrays.asList(args));
 		}
 	}
 
diff -r 322c03d85ada -r 62b457c50594 scripts/cp-luan
--- a/scripts/cp-luan	Tue Apr 28 13:21:50 2015 -0600
+++ b/scripts/cp-luan	Tue Apr 28 22:38:31 2015 -0600
@@ -1,14 +1,20 @@
 LUAN_HOME=~/hg/luan
 
 CLASSPATH=$LUAN_HOME/core/src
-CLASSPATH=$CLASSPATH:$LUAN_HOME/web/src
+
 CLASSPATH=$CLASSPATH:$LUAN_HOME/logging/src
+for i in $LUAN_HOME/logging/ext/* ; do CLASSPATH=$CLASSPATH:$i ; done
+
+CLASSPATH=$CLASSPATH:$LUAN_HOME/web/src
+for i in $LUAN_HOME/web/ext/* ; do CLASSPATH=$CLASSPATH:$i ; done
+
 CLASSPATH=$CLASSPATH:$LUAN_HOME/mail/src
+for i in $LUAN_HOME/mail/ext/* ; do CLASSPATH=$CLASSPATH:$i ; done
+
 CLASSPATH=$CLASSPATH:$LUAN_HOME/lucene/src
-
-for i in $LUAN_HOME/web/ext/* ; do CLASSPATH=$CLASSPATH:$i ; done
-for i in $LUAN_HOME/logging/ext/* ; do CLASSPATH=$CLASSPATH:$i ; done
-for i in $LUAN_HOME/mail/ext/* ; do CLASSPATH=$CLASSPATH:$i ; done
 for i in $LUAN_HOME/lucene/ext/* ; do CLASSPATH=$CLASSPATH:$i ; done
 
+CLASSPATH=$CLASSPATH:$LUAN_HOME/stripe/src
+for i in $LUAN_HOME/stripe/ext/* ; do CLASSPATH=$CLASSPATH:$i ; done
+
 export CLASSPATH
diff -r 322c03d85ada -r 62b457c50594 stripe/ext/gson-2.2.4.jar
Binary file stripe/ext/gson-2.2.4.jar has changed
diff -r 322c03d85ada -r 62b457c50594 stripe/ext/stripe-java-1.27.0.jar
Binary file stripe/ext/stripe-java-1.27.0.jar has changed
diff -r 322c03d85ada -r 62b457c50594 stripe/src/luan/modules/stripe/Stripe.luan
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stripe/src/luan/modules/stripe/Stripe.luan	Tue Apr 28 22:38:31 2015 -0600
@@ -0,0 +1,32 @@
+java()
+local Luan = require "luan:Luan"
+local error = Luan.error
+local assert_integer = Luan.assert_integer
+local StripeLuan = require "java:luan.modules.stripe.StripeLuan"
+local Stripe = require "java:com.stripe.Stripe"
+local Customer = require "java:com.stripe.model.Customer"
+local Charge = require "java:com.stripe.model.Charge"
+
+currency = "usd"
+
+function init(api_key)
+	Stripe.apiKey = api_key
+end
+
+function create_customer(params)
+	local java_customer = Customer.create(params)
+	return StripeLuan.table(java_customer)
+end
+
+function retrieve_customer(id)
+	local java_customer = Customer.retrieve(id)
+	return StripeLuan.table(java_customer)
+end
+
+function create_charge(params)
+	params.amount or error "missing parameter 'amount'"
+	params.amount = assert_integer(params.amount)
+	params.currency = params.currency or currency
+	local java_charge = Charge.create(params)
+	return StripeLuan.table(java_charge)
+end
diff -r 322c03d85ada -r 62b457c50594 stripe/src/luan/modules/stripe/StripeLuan.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stripe/src/luan/modules/stripe/StripeLuan.java	Tue Apr 28 22:38:31 2015 -0600
@@ -0,0 +1,80 @@
+package luan.modules.stripe;
+
+import luan.Luan;
+import luan.LuanTable;
+import luan.LuanProperty;
+import com.stripe.model.Customer;
+import com.stripe.model.Charge;
+import com.stripe.model.Subscription;
+import java.util.List;
+
+
+public final class StripeLuan {
+
+	public static LuanTable table(final Customer customer) {
+
+		LuanTable tbl = Luan.newPropertyTable();
+
+		tbl.put( "id", new LuanProperty() {
+			@Override public Object get() {
+				return customer.getId();
+			}
+		} );
+
+		tbl.put( "subscription_status", new LuanProperty() {
+			@Override public Object get() {
+				Subscription s = getSubscription(customer);
+				return s==null ? null : s.getStatus();
+			}
+		} );
+
+		return tbl;
+	}
+
+	public static LuanTable table(final Charge charge) {
+		LuanTable tbl = Luan.newPropertyTable();
+
+		tbl.put( "id", new LuanProperty() {
+			@Override public Object get() {
+				return charge.getId();
+			}
+		} );
+
+		tbl.put( "amount", new LuanProperty() {
+			@Override public Object get() {
+				return charge.getAmount();
+			}
+/*
+			@Override public boolean set(Object value) {
+				charge.setAmount(check_integer(value));  return true;
+			}
+*/
+		} );
+
+		return tbl;
+	}
+
+	public static Subscription getSubscription(Customer customer) {
+		List<Subscription> list = customer.getSubscriptions().getData();
+		switch(list.size()) {
+		case 0:
+			return null;
+		case 1:
+			return list.get(0);
+		default:
+			throw new RuntimeException("more than 1 subscription");
+		}
+	}
+/*
+	private static Integer check_integer(Object value) {
+		if( value==null )
+			return (Integer)null;
+		Integer i = Luan.asInteger(value);
+		if( i==null )
+			throw new IllegalArgumentException("value must be an integer");
+		return i;
+	}
+*/
+}
+
+// http://javadox.com/com.stripe/stripe-java/1.2.1/overview-summary.html
diff -r 322c03d85ada -r 62b457c50594 web/src/luan/modules/web/shell.luan
--- a/web/src/luan/modules/web/shell.luan	Tue Apr 28 13:21:50 2015 -0600
+++ b/web/src/luan/modules/web/shell.luan	Tue Apr 28 22:38:31 2015 -0600
@@ -21,7 +21,7 @@
 		if cmd ~= nil then
 			Io.stdout = {}
 			function Io.stdout.write(...)
-				for _,v in Luan.values(...) do
+				for v in Luan.values(...) do
 					history[#history+1] = v
 				end
 			end