Skip to content

Instantly share code, notes, and snippets.

@desnudopenguino
Last active March 22, 2018 06:40
Show Gist options
  • Save desnudopenguino/80c0fea3329a45e1f137a6ed8304c946 to your computer and use it in GitHub Desktop.
Save desnudopenguino/80c0fea3329a45e1f137a6ed8304c946 to your computer and use it in GitHub Desktop.
import java.util.Map;
import org.jruby.embed.IsolatedScriptingContainer;
import org.jruby.embed.PathType;
import com.amazonaws.services.lambda.runtime.Context;
public class AWSLambdaJRuby {
private static final String mainRubyFile = "main.rb";
private static final String gemsRubyFile = "gems.rb";
@SuppressWarnings("rawtypes")
public String handler(Map data, Context context) throws Exception {
IsolatedScriptingContainer container = new IsolatedScriptingContainer();
context.getLogger().log(data.toString());
container.put("$lambda_arg", data);
String result = "";
result += "yaml: " + container.runScriptlet(PathType.CLASSPATH, mainRubyFile);
result += " gems: " + container.runScriptlet(PathType.CLASSPATH, gemsRubyFile);
return result;
}
}
(TypeError) no implicit conversion of Java::JavaUtil::LinkedHashMap into String: org.jruby.embed.EvalFailedException
org.jruby.embed.EvalFailedException: (TypeError) no implicit conversion of Java::JavaUtil::LinkedHashMap into String
at org.jruby.embed.internal.EmbedEvalUnitImpl.run(EmbedEvalUnitImpl.java:131)
at org.jruby.embed.ScriptingContainer.runUnit(ScriptingContainer.java:1307)
at org.jruby.embed.ScriptingContainer.runScriptlet(ScriptingContainer.java:1352)
at AWSLambdaJRuby.handler(AWSLambdaJRuby.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
Caused by: org.jruby.exceptions.RaiseException: (TypeError) no implicit conversion of Java::JavaUtil::LinkedHashMap into String
at json.ext.Parser.initialize(json/ext/Parser.java:174)
at json.ext.Parser.new(json/ext/Parser.java:151)
at RUBY.parse(uri:classloader:/META-INF/jruby.home/lib/ruby/stdlib/json/common.rb:155)
at RUBY.load(uri:classloader:/META-INF/jruby.home/lib/ruby/stdlib/json/common.rb:334)
at RUBY.<top>(classpath:/main.rb:4)
module StripeResponse
RESPONSE = '{ "id": "evt_xxxxx", "object": "event", "api_version": "2016-03-07", "created": 1521583284, "data": { "object": { "id": "cus_xxxx", "object": "customer", "account_balance": 0, "created": 1521583283, "currency": null, "default_source": "card_xxxxx", "delinquent": false, "description": "[email protected]", "discount": null, "email": null, "invoice_prefix": "xxxxx", "livemode": false, "metadata": { "user_id": "2" }, "shipping": null, "sources": { "object": "list", "data": [ { "id": "card_xxxxx", "object": "card", "address_city": null, "address_country": null, "address_line1": null, "address_line1_check": null, "address_line2": null, "address_state": null, "address_zip": null, "address_zip_check": null, "brand": "Visa", "country": "US", "customer": "cus_xxxxx", "cvc_check": "pass", "dynamic_last4": null, "exp_month": 2, "exp_year": 2020, "fingerprint": "xxxxx", "funding": "credit", "last4": "4242", "metadata": { }, "name": "John Doe", "tokenization_method": null }], "has_more": false, "total_count": 1, "url": "/v1/customers/cus_xxxxx/sources" }, "subscriptions": { "object": "list", "data": [ ], "has_more": false, "total_count": 0, "url": "/v1/customers/cus_xxxxx/subscriptions" } } }, "livemode": false, "pending_webhooks": 3, "request": "req_xxxxx", "type": "customer.created" }'
end
class StripeUser
attr_accessor :user_id
attr_accessor :account_id
def digest(request)
request_json = JSON.parse(request)
set_user_id(request_json)
set_account_id(request_json)
end
def set_user_id(json)
self.user_id = json['data']['object']['metadata']['user_id']
end
def set_account_id(json)
self.account_id = json['data']['object']['id']
end
end
puts "START"
u = StripeUser.new()
u.digest(StripeResponse::RESPONSE)
puts "DIGEST RESPONSE"
ddb = Aws::DynamoDB::Client.new(region: 'us-west-2')
item = {
user_id: u.user_id.to_i,
account_id: u.account_id
}
params = {
table_name: "stripe-users",
item: item
}
puts "BEGIN SAVING"
begin
result = ddb.put_item(params)
puts "SUCCESS"
rescue Aws::DynamoDB::Errors::ServiceError => error
puts error.message
end
puts "DONE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment