Get code for your action and see in my timeline - ruby-on-rails

I'm building an Open Graph action.
Before I was using curl in command line in order to post in my timeline, now when I'm using this command, nothing appears in my timeline.
The response is correct in the graph. And I can see my all those actions on my graph but still nothing on my timeline https://graph.facebook.com/me/[namespace]:[action]?access_token=[...]
I don't understand what is happening
I'm using fb_graph gem in the back-end
def self.action(objet, user, url)
me = current_user.facebook
Thread.new do
sleep(4)
action = me.og_action!(
FACEBOOK_NAMESPACE+":action",
:objet => url
)
end
end
This has always worked
Any idea ?
Thanks
Maxime

Related

Rails App Breaks with 404 Error when Last.fm API Call Returns Nothing

I wonder if anyone can help, and if the issue is specific to Last.fm (perhaps not the greatest of APIs).
I've built an album search feature into my app that takes two parameters - album and artist. Now, this returns an album just fine if there's a result, but in the instances that there isn't a result - if just type gibberish into the fields - Rails breaks with a 404 error when trying to run URI.open(url).read.
What I don't quite understand (and I am fairly new at this), is that when I run the same API call url in my search engine, with the gibberish, I do get a JSON response:
// https://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=xxxxxxx&artist=akjsdhkasd&album=hdkuahd&format=json
{
"message": "Album not found",
"error": 6
}
So, I don't understand why I'm getting a 404 error when it runs in my code.
Is there any way that I can rescue this, so that I can just render a 'no result', rather than crashing the entire site?
Not sure my code adds much to the picture, but this is where I run the URI:
def get_album(artist, album)
album = ERB::Util.url_encode(album)
artist = ERB::Util.url_encode(artist)
url = "https://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=xxxx&artist=#{artist}&album=#{album}&format=json"
serialized = URI.open(url).read
JSON.parse(serialized, object_class: OpenStruct).album
end
Thanks for any pointers.
From what I understood, you are using open-uri to reach this service. If you want to rescue an exception in this process you can try something like this:
def get_album(artist, album)
album = ERB::Util.url_encode(album)
artist = ERB::Util.url_encode(artist)
url = "https://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=xxxx&artist=#{artist}&album=#{album}&format=json"
serialized = URI.open(url).read
JSON.parse(serialized, object_class: OpenStruct).album
rescue OpenURI::HTTPError
"Error when trying to fetch album information"
end
*I'm returning just a string but you can implement an appropriate return that fits your purpose.
I'm not sure if it's possible to rescue specific 404 - Not Found errors using this strategy. But, you can take a look into 'net/http' or other HTTP Client Libraries (httparty, typhoeus, etc..) to try different approaches if you want..

Docusign gem block request

I'm using the DocuSign ruby client (https://github.com/docusign/docusign-ruby-client) on ruby on rails to send a document via email to some clients, but when I deploy the project after 15 minutes the request between the application and DocuSign gets "paused". For some reason the gem creates the request but doesn't send it as you can see in the next image where I enable the debug in the gem:
In that point the log doesn't print any more after 15 minutes.
The code that send the message in my app is:
access_token = "xxxxxxx"
account_id = "xxxxxxxxx"
base_path = "xxxxxxxxxx"
envelope_args = {
signer_email: contact.email,
signer_name: contact.name,
template_id: document.docusign_id
}
#args = {
account_id: account_id,
base_path: base_path,
access_token: access_token,
envelope_args: envelope_args
}
envelope_args = #args[:envelope_args]
# 1. Create the envelope request object
envelope_definition = make_envelope(envelope_args)
# 2. Call Envelopes::create API method
# Exceptions will be caught by the calling function
envelope_api = create_envelope_api(#args)
envelope_api.create_envelope #args[:account_id], envelope_definition
I don't know what can I do.
Thank you
Your screenshot shows everything happening at 18:29:05 -- I don't understand the issue.
Also, have you tried install/using the RoR code example?
See if it has the same problem.
We looked for the issue in our code, but we saw that in the step which the request is send in the gem here always freezes. So we debugged there and we saw that curl was being used for ruby. At that point we saw that curl was trying to reconnect with Docusign but it wasn't success, so we found this issue in the version of curl that we had (https://github.com/curl/curl/issues/4499 )
To fix it we updated the version to the latest, and it fixed the issue.
Thanks for your answers.

Mechanize - Receiving Errno::EMFILE: Too many open files - socket(2) after a day

I'm running an application that uses mechanize to fetch some data every so often from an RSS feed.
It runs as a heroku worker and after a day or so I'm receiving the following error:
Errno::EMFILE: Too many open files - socket(2)
I wasn't able to find a "close" method within mechanize, is there anything special I need to be doing in order to close out my browser sessions?
Here is how I create the browser + read information:
def mechanize_browser
#mechanize_browser ||= begin
agent = Mechanize.new
agent.redirect_ok = true
agent.request_headers = {
'Accept-Encoding' => "gzip,deflate,sdch",
'Accept-Language' => "en-US,en;q=0.8",
}
agent
end
end
And actually fetching information:
response = mechanize_browser.get(url)
And then closing after the response:
def close_mechanize_browser
#mechanize_browser = nil
end
Thanks in advance!
Since you manually can't close each instance of Mechanize, you can try invoking Mechanize as a block. According to the docs:
After the block executes, the instance is cleaned up. This includes closing all open connections.
So, rather than abstracting Mechanize.new into a custom function, try running Mechanize via the start class method, which should automatically close all your connections upon completion of the request:
Mechanize.start do |m|
m.get("http://example.com")
end
I ran into this same issue. The Mechanize start example by #zeantsoi is the answer that I ended up following, but there is also a Mechanize.shutdown method if you want to do this manually without their block.
There is also an option that you can add a lambda on post_connect_hooks
Mechanize.new.post_connect_looks << lambda {|agent, url, response, response_body| agent.shutdown }

Clicks on OpenGraph links in timeline fail to authenticate with "Error validating verification code"

I'm trying to authenticate users coming to my site from a click on an opengraph action in facebook.
I've generate the action successfully with the following code. This
goes out to my timeline successfully. So far, so good.
require 'cgi'
client_id ="CLIENT_ID"
client_secret = "CLIENT_SECRET"
recipe = "http://foodonthetable.shadr.us/recipes/1007-spanish-chicken-and-rice"
access_token = "LONGACCESSTOKEN"
# PUBLISH
result = `curl -F access_token=#{access_token} -F recipe=#{recipe} https://graph.facebook.com/me/foodonthetable_mrh:plan_to_cook`
puts result
When I click on the link in the timeline, I'm taken to the following URL. This is where
I start having issues. Basically, I want to recognize the user coming in
through this link and log them in (or create a new user). I have
authenticated referrals turned on, and I'm receiving the code parameter as
expected.
http://foodonthetable.shadr.us/recipes/1007-spanish-chicken-and-rice?fb_action_ids=384156608318085&fb_action_types=foodonthetable_mrh%3Aplan_to_cook&fb_source=timeline_og&action_object_map=%7B%22384156608318085%22%3A10151004406923956%7D&code=AQAPExNw7MHkxcLTEi5L24iD79pVa-WYxyhBA_bhdWLCM0PCGDuPjh1WqmAyd3_O3_LSjYzPawrinHNP3nv9BCMB_XuTnr8De8xQ2AwXqCaeHUzZUPm2MPyQ_eodOC9-YtjkvXm_PzRX3JG58khalT3AJjVuZvKHn5hWGDSohXLRbHGxW-vOg_Whm3mt_WUkdd0#_=_
However, when I try to get the auth_token using this code, I'm not able to.
I've tried using a couple of different versions of the redirect_uri thinking
that might be the problem (based on other posts) but nothing seems to work.
redirect_uri = "http://foodonthetable.shadr.us/recipes/1007-spanish-chicken-and-rice"
redirect_uri = "http://foodonthetable.shadr.us/facebook_connect/connect"
code = "AQAPExNw7MHkxcLTEi5L24iD79pVa-WYxyhBA_bhdWLCM0PCGDuPjh1WqmAyd3_O3_LSjYzPawrinHNP3nv9BCMB_XuTnr8De8xQ2AwXqCaeHUzZUPm2MPyQ_eodOC9-YtjkvXm_PzRX3JG58khalT3AJjVuZvKHn5hWGDSohXLRbHGxW-vOg_Whm3mt_WUkdd0"
result = `curl -F client_id=#{client_id} -F redirect_uri=#{redirect_uri} -F client_secret=#{client_secret} -F code=#{code} https://graph.facebook.com/oauth/access_token`
puts result
No matter what I do here, I always get the following error:
{"error":{"message":"Error validating verification code.","type":"OAuthException","code":100}}
Any help would be appreciated.
This does fix it.
redirect_uri = request.url.split('&code=').first

Get online users XMPP4r + Rails

I'm trying get online friends by user in XMPP server (Ejabberd). I'm using Ruby on Rails 3.2.
The idea is to add in array all online users to use this on view page.
I found asynchronous code (below), but it use Thread and it's difficult to work on controller method.
jid = Jabber::JID.new('user#localhost')
cl = Jabber::Client.new(jid)
cl.connect
cl.auth('123456')
#online_users = [] #online users queue
roster = Jabber::Roster::Helper.new(cl)
mainthread = Thread.current
roster.add_presence_callback { |item,oldpres,pres|
if item.online?
#online_users.push item
else
#online_users.delete_if {|x| x.jid == item.jid }
end
puts #online_users.inspect
puts "#{item.jid} - online: #{item.online?}"
}
cl.send(Jabber::Presence.new.set_show(:dnd))
t = Thread.new { sleep XMPP_REQUEST_TIMEOUT; mainthread.wakeup;}
Thread.stop
cl.close
So I need some synchronous code, or some way to execute this kind of code in controller method.
Thanks.
For this found another solution that help me:
I installed a mod_rest in ejabberd server. This module allow that you do HTTP request of terminal commands of ejabberdctl.
So it has "ejabberdctl connected_users", that return users online.
So in your model app using gem rest-client you can do something like it:
def online_users
response = RestClient.post('http://localhost:5280/rest', "connected_users")
response
end
You will be much happier in the long run if you use a library like Strophe.js to do this in the browser, talking to an XMPP server that has BOSH enabled. Snapshots of presence are never anywhere as interesting as you expect them to be, and you're going to have really bad authentication/authorization problems on the path down which you're heading.

Resources