How to parse response from Recurly ruby client - ruby-on-rails

I am trying to work with Recurly ruby client gem but getting frustrated on how to pass its response.
I'm doing a simple call to get a list of all my current plans ie
def plans
#result_object = Recurly::Plan.all
end
I believe its sending me a list of plans as an array as if I inspect the response I'm getting a bunch data ie. http://pastie.org/3086492
But if so how are you meant to parse the setup_fee_in_cents: when I am getting
setup_fee_in_cents: #<Recurly::Money USD: 0_00>
I have tried to convert the response to_hash but getting an error undefined method 'to_hash'
Has anyone used the recurly gem before or can shed some light on how I should parse the response.
Hope someone can advise.

I've never used the gem before but it appears as though it's returning you an object of type Recurly::Money. I would try reading the documentation on the Recurly::Money class to get a better idea of how to use it.

to_hash works fine on Recurly::Money object. Try updating your gem. I am using recurly 2.0.9.
r[0].unit_amount_in_cents.to_hash[:USD] gave me the price in cents.

Related

How to filtering message with AWS SNS with Ruby on Rails

I'm using 'aws-sdk' gem.
https://github.com/aws/aws-sdk-ruby
Using below gem
'aws-sdk (1.11.0)'
I want to use filtering messages function.
https://docs.aws.amazon.com/sns/latest/dg/message-filtering.html
I understood I should set Filter policy.
but, I could not find how to set it.
I have read below.
https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/sns-examples.html
https://docs.aws.amazon.com/sdk-for-ruby/v3/api/index.html
How can I use this function?
Thank you.
I could set FilterPolicy using set_subscription_attributes.
DeliveryPolicy and RawMessageDelivery were valid attribute names in the description of this page, and FilterPolicy could be used too.
https://docs.aws.amazon.com/sdkforruby/api/Aws/SNS/Client.html#set_subscription_attributes-instance_method
params = {subscription_arn: subscription_arn, attribute_name: 'FilterPolicy', attribute_value: '{"event_type": ["order_placed", "order_cancelled"]}'}
client.set_subscription_attributes(params)

Active resource find doesn't work in production

As the title says, I have a simple ActiveResource in my application that is supposed to get data from an api. The collection works perfectly both locally and on the production server. However, .find doesn't work in production, i get a weird error:
MyResource.find(1, params: { website_id: 2 })
ArgumentError: wrong number of arguments (given 2, expected 0..1)
The same query works if I run it from local console connected to the api. API returns a valid response. I have no idea how else to debug it.
class MyResource < ActiveResource::Base
self.site = Rails.configuration.content_url
self.prefix = "/api/websites/:website_id/"
self.element_name = "game"
end
I have to say, i have other resources in the application using the same api, find method works for them, only this one has issues.
for some reason it makes a call to
https://github.com/rails/rails/blob/master/activerecord/lib/active_record/core.rb#L330
with 2 arguments, but i can't figure out who makes such call. And it only happens in production mode, not in dev.
I found the problem, but it's the weirdest thing ever.
In my show method in the api controller i was returning an object but i was merging the hash with some extra data. If i opened in the browser, the response was fine, development worked, but for some reason, in production it doesn't want to accept the merge.
So problem solved, but still have no idea why it's happening like that.
I ran into a similar issue in an app that used ActiveRecord for a vendor model and ActiveResource for a delivery model that has a vendor attribute. In development, if the vendor model has not yet been loaded, you can fetch a delivery and deserialize it, including the vendor attribute. If, however, the vendor model has been loaded already, attempting to fetch a delivery leads to the error described in this issue.
How to reproduce in a console:
Vendor.connection
Delivery.find(1)
Seems that AResource is trying to create a new instance of Vendor.

How to get values from stripe return in ruby controller

I am getting a return from stripe and trying to parse it and am apparently doing it incorrectly.
Code in stripe_webhooks.rb controller
#event_data = JSON.parse(request.body.read)
When I log that is is the full return. However when I try to access any of the values like this(this is how it is set up in the tutorial I am working from)
#event_data.data.object.id
I get the error
NoMethodError (undefined method `data' for #<Hash:0x007fe533bebe08>):
What I need to access (among other things) is the id from the charge, not the first id which is for the event . So the id with "ch_..." in it from the output below.
Log of the output
Parameters: {"id"=>"evt_19DMt92cKHX9k3v0rcdvLs5a", "object"=>"event", "api_version"=>"2014-03-28", "created"=>1478562655, "data"=>{"object"=>{"id"=>"ch_19DMt92cKHX9k3v0iQKMEHDu", "object"=>"charge",...
If anyone could shed some light on how to access the data that is inside the "data => {"object" =>{ " section it would be greatly appreciated.
You want to access the Hash you're loading by using bracket accessors.
#event_data["data"]["object"]["id"]`
which will give you what you're looking for.
Also many times ruby code prefers to use symbols, which means you can use
#event_data = JSON.parse(request.body.read).with_indifferent_access
to access the same content via
#event_data[:data][:object][:id]
Note these solutions do not handle missing values, in which case you'll need to guard against accessors returning nil instead of the structure you defined.

Trouble Accessing Ruby Array and Hash Value

I am making an api call and receiving the following response (it's long, so I'm showing the important part):
... "fields":{"count"_1:["0"],"count_2":["5"]} ...
when I do:
call["fields"]["count_1"]
It returns
["0"]
I need it to give me just the integer. I tried:
call["fields"]["count_1"][0]
And I also tried:
call["fields"]["count_1"][0].to_i
I'm running this in Rails and it gives me the error:
undefined method `[]' for nil:NilClass
But it's not working.
Try as below using String#to_i
call["fields"]["count_1"][0].to_i # => 0
Some tips:
Try wrapping the API response in JSON.parse(...). That is, if you're not making the call via a gem that already does this. This might require 'json'.
Try call['fields']['count_1'].first.to_i
Do some debugging: check the value of call.class, call['fields'].class and call['fields']['count_1'].class. The last one should definitly be an Array.
Add an if clause to check if call['fields'][['count_1'].is_empty?.
Look for typos :)
For some reason the API call was making the zeros nil instead of zero. Thanks for all your help.

Rails XML - Updating server with multiple records

I'm using a Rails 2.x server, and trying to handle all interaction using XML. I've got no problem with updating the database with single XML records (creating/updating/deleting), but I've got a group of new records I'd like to add in one transaction, and I can't get it to work.
The XML I'm submitting looks like this:
<invitees>
<invitee>
<event>32</event>
<name>Jack</name>
</invitee>
<invitee>
<event>42</event>
<name>Alan</name>
</invitee>
</invitees>
I'm new to Rails; I can handle the basic Ruby/Rails stuff, but handling this is clearly beyond me! The code to handle this is failing, and I don't understand why. This is what I've got:
#invitees = params[:invitees]
for #invitee in #invitees
thisinvitee = Invitee.new(#invitee)
thisinvitee.save
end
This fails in "Invitee.new", with error:
NoMethodError (undefined method `stringify_keys!' for #):
Can anyone give me an idea what I'm doing wrong, and how to handle multiple XML records being submitted in a single transaction from a remote client?
Thanks for any help/pointers.

Resources