I'm new to Rails and trying to send a request to Chargify to cancel a subscription. Their API says I need to send the method DELETE to a xml URL. This isn't a Chargify based question but rather... how would I have a user click a button that then generates this request and sends it within my Rails app? You can view this url to see what I"m trying to do - http://docs.chargify.com/api-subscriptions#cancel. Also it's working fine when I run a command-line test so I know my code works, just now sure how to put it into my Rails app (view/controller). Thanks
Something like this should work using Net::HTTP in the Ruby Standard Library:
require 'net/http'
http = Net::HTTP.new('subdomain.chargify.com')
request = Net::HTTP::Delete.new('/subscriptions/1337.xml')
response = http.request(request)
You could include it in your controller's method, but unless you need to make sure the request finished before you send a response back, I recommend making the request to Chargify in a background job. Check out the delayed_job or resque libraries.
You probably want to use the Net::HTTP library. Also, checkout the source of some other ruby API bindings on Github to see how they use and structure this type of behavior (specifically, Twilio, Dropbox, and Flickraw).
Related
I am having a Rails Application, where I will receive a SOAP request using POST method to it.
I need to handle that request, parse the corresponding XML, and need to respond to that request accordingly.
But, Ruby on Rails by default allows only REST request.
Can anyone please let me know how to handle that in Ruby on Rails?
Thanks in advance.
If you absolutely cannot avoid requiring soap requests coming into your application (in lieu of say, json) check out the wash_out gem.
I am using a Clear bit gem. How do I call the external API in the Rails application and send data back forth without the default database to store data.I need to build a form like the Clear bit form and send data back and forth.Do I need a controller and model for the same?
To call any external url, you should require rest-client gem.
url = "https://...."
body = RestClient.get(url)
JSON.parse(body)
You have to check the rest-client documentation
I'm trying to understand how webhook works. My understanding is that its the ability to connect two different applications. If I submit a webhook with url
localhost:3000/receiver
to one application, and I have my application with a method
def receiver
end
I was wondering if I don't know what the callback is from the webhook would be, how would I capture data? How do I save any JSON data thats communicating with my application? I was thinking maybe save some file to see what the objects are, but I'm still fairly new and not sure how to capture JSON data?
Thanks
If you are sure that the webhook is returning a JSON, you can so something like this
data_json = JSON.parse request.body.read
Sure, a webhook a is tool to sincronize two apps
You HAVE tou know the structure of the incoming json, because you need to get the info inside
By definition a webhook is sent by POST method, so you can capture it just inspecting the body of the petition, i.e.
webHook = JSON.parse(params[:something])
Your would try with github web hooks and publish your app in heroku, the api is very well documented and there are many examples.
My question is simple here. I want to send json data from one Rails app to other Rails app via POST. How do I achieve that?
You can find a complete architectural example of how to do this with ActiveResource here
If you just need a simple call, I'd suggest to use REST-client for the calling side.
The provider side is pretty standard. Just expose an action that responds to JSON posts. Let me know if you need more details.
Have one Rails app accept JSON via a POST action in a controller, and have the other Rails app submit the JSON via an HTTP library like rest-client.
How to get response of seperate ajax requests in a web page using capybara-webkit?
Is there any particular method available to capture response time of each request?
Note:
Am using capybara with rspec.
For eg: i have 3 Ajax requests in a web page. I need to get separate response time of each request and the response time of entire web page.
Thanks,
Priya
Generally it's not possible since webserver is running in the different process but you could create a custom rack middle-ware and dump all responses to the separate log file. For the beginning you could implement technique described here: https://gist.github.com/2975611 and for dumping headers you could use some code snippets from http://rack.rubyforge.org/doc/Rack/ContentLength.html