REST request with oauth gem - ruby-on-rails

I am missing something that should be very easy!
I am using oauth-plugin gem to access jira. I could easily build the consumer part, but now I don't know how to make a rest request.
It says in documentation:
The gem's author assumes that it is self-explanatory how to make a get/post-request. But I cannot figure it out.
I've tried to use Net::HTTP as follows:
uri = URI.parse('/oauth_consumers/jira/client/rest/api/2/project')
request = Net::HTTP.get(uri,{})
And I am getting Connection refused - connect(2) error.
I thought it could work, if I use current_user and his token (jira in my case):
current_user.jira.get('/oauth_consumers/jira/client/rest/api/2/project')
And RoR says that jira doesn't have such a method.
I've tried to search online, but there are very few projects that use oauth-plugin gem.
Thank you in advance.

You are almost right with current_user.jira.get.
Just add client after jira and change path a little bit:
current_user.jira.client.get('/rest/api/2/project')
JiraToken doesn't have get method, while AccessToken (client) does.

Related

How to Parse Json with Bearer Token in Ruby

I have recently graduated a Bootcamp and trying my luck in some projects and applying for jobs.
Got a test, for this potential job, that I don't know how to tackle exactly. At this stage, kind of just wondering how to solve the test.
I need to create a website and display a list of all of the buildings, from this real estate company via their API. They have provided me with the token for the access already.
However, I'm not entirely sure how to connect with their API via Ruby. They gave me an example of how to connect but I haven't seen this before.
curl -H "Authorization: Bearer token_they_provided" https://theirsite/api/buildings.
Any assistance to point me in the right direction will be appreciated.
Thank you all!
What you are trying to achieve is pretty much simple. You can just add HTTP gem (there are other options, like Faraday gem) to your project, which turns everything even simpler.
Using HTTP Gem
Add it to your Gemfile
Install using bundle
Fetches API as below:
response = HTTP[Authorization: "Bearer #{token_they_provided}"].get("https://theirsite/api/buildings")
Parse JSON body:
JSON.parse(response.body)
You can read the gem docs for more information. Also, feel free to connect me via LinkedIn

What is the best way to send a SOAP request in Ruby on Rails?

I want to send a SOAP request to a server that requires authentication. I've tried doing some of the methods in the HTTP:NET documentation but basic auth won't work. What would be the best way to contact this server with a user agent? I usually always get a connection time out error (500).
Savonrb gem will be helpful for you in this case.
It is a ruby SOAP client to make SOAP communications easier. Also, check out this Railscasts episode to get started.

Forcing Gibbon Gem (or Faraday) to use QuotaGuard Static HTTP proxy on Heroku

Full disclaimer; I'm not a strong Ruby dev, but I am learning quickly :)
I've set up a simple Ruby script on a Heroku dyno that listens for calls from our donation platform.
When a donation is made, it hits a webhook endpoint within my app, which then sends a donation receipt via Mandrill (which works fine), and updates/inserts a record in a Mailchimp list, via the 'upsert' method of the wonderful Gibbon gem.
That all works fine; except when the Heroku box happens to come up on an IP address that has done something bad in the past, and Mailchimp's API drops with a 403 (Forbidden) error.
I've had this confirmed by the Mailchimp API team; they suggest using something like QuotaGuard Static to tunnel the API requests to Mailchimp through, removing the issue of API calls from inconsistent (and sometimes untrusted) IP addresses.
I'd love some advice on how to make this happen. I can see that Gibbon uses Faraday to handle HTTP requests, but I'm not an advanced enough Ruby dev to fork the code and add in HTTP proxy functionality.
If there's a way to globally force the Faraday calls to use a HTTP proxy (ie QuotaGuard Static), that's what I'm looking for. A config setting for Faraday, for example.
Or perhaps there's a tweak I can make to my Procfile:
web: bundle exec ruby webhooks.rb -p $PORT
...that will force the outbound traffic to go via the QuotaGuard Static proxy. I know Proximo has this functionality, but it also blocks inbound access to the app, which doesn't work for this app.
Appreciate any ideas the community can offer. Thanks!
Gibbon Author here. You can simply set the proxy value to the proxy URL in Gibbon 2.2.0 and later.
From the Faraday documentation (here) the Connectionclass uses the proxy specified in the http_proxy environment variable. I have never tried it, but looking at the source code it should work.
I wanted to provide a bit more information, since the two answers pointed me on the right track but still required me to do some digging. I solved this issue by first adding the QuotaGuard Static add-on in Heroku (free for up to 250 uses per month) and then initializing Gibbon like so:
g = Gibbon::Request.new
g.proxy = ENV["QUOTAGUARDSTATIC_URL"]
And here is the relevant section from the Gibbon docs: https://github.com/amro/gibbon#other

Why am I receiving a 500 status when trying to connect with ASANA api?

I am using this example: https://github.com/ajimix/asana-api-php-class/blob/master/examples/oauth.php to learn how to use the asana's oauth. I already registered my app on the asana's developers webpage. But, when I get the asana's response with my 'redirect_url'?code=0%XXXXXXX, localhost gives me a 500 status. I don't know why, does anyone know how I can solve it?
I tried using the other examples in there: https://github.com/ajimix/asana-api-php-class/blob/master/examples/, but I wasn't able to make them run either, I always get a 500 status. I think that the problem must be in my server, but I don't how solve it. Maybe changing something on the .htaccess file, but I don't really know what to put in there.
If you're looking to use the Asana API from PHP may I recommend the official first-party client: https://github.com/Asana/php-asana
It also contains examples of how to use with OAuth and is officially supported by us at Asana :-)

Rails how to follow redirect in HTTParty GET request

I am using httparty to send a get request and then trying to follow the redirect:
get 'https://accounts.google.com/o/oauth2/auth'
how can i follow the redirect using HTTParty?
sorry if this have been asked before, but i could not find the answer anywhere.
Thanks
Probably it's not needed now, but for someone who need.
From documentation, there is special option for automatically redirects
follow_redirects(value = true) ⇒ Object
Proceed to the location header when an HTTP response dictates a redirect. Redirects are always followed by default.
So you can use this options like that:
HTTParty.get('http://google.com', follow_redirects: true)
Guessing this is a little out of date now, what with the question being about a month old... but my understanding of the HTTParty docs is that it should automatically follow redirects, unless you set the no_follow flag. I haven't successfully tested it though (I'm a total HTTParty n00b and stumbled on this trying to find an answer to my own problem, d'oh...)
If that doesn't work, you could always read the header directly and parse the redirect manually. I've done that in Javascript AJAX requests before, it is not difficult. But you shouldn't have to do that with HTTParty.

Resources