Faye: big delay in http post request - ruby-on-rails

I have this code in Faye rackup script:
faye_server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 45)
faye_server.add_extension(ServerAuth.new)
server_uri = URI.parse(BacklinkHealth::FAYE_SERVER)
faye_server.on(:subscribe) do |client_id, channel|
puts "subscribed #{channel}"
if channel.starts_with?('/pagination')
website_id = channel.split('/')[2]
page = channel.split('/')[3]
app = ActionDispatch::Integration::Session.new(Rails.application)
app.get app.pagination_website_backlinks_path(website_id, :page => page)
message = {:channel => channel, :data => app.response.body, :ext => {:auth_token => BacklinkHealth::FAYE_TOKEN}}
puts 'started HTTP post'
Net::HTTP.post_form(server_uri, :message => message.to_json)
puts 'finished HTTP post'
end
end
The problem is that the execution comes to "started HTTP post" and then it takes more than a minute for the message to be registered at the client-side javascript. The messge "finished HTTP post" is never printed :( I don't understand what is going on.
If I try to execute an identical HTTP post from Rails console, it goes through in an instant although it did happen once or twice that it took a minute.
Any ideas?

It's much better to use this:
require 'eventmachine'
EM.run {
client = Faye::Client.new(BacklinkHealth::FAYE_SERVER)
client.publish(channel, 'pagination' => pagination)
}
than Http requests... I couldn't get this to work with authentication (yet) but I will.

Related

How to solve this error " (pre:svcFault) Service Fault"?

I am trying to call a SOAP api using Savon gem. I am getting the following error: "(pre:svcFault) Service Fault"
I created both the header and message for the request.
Here is the request sent from SoapUI: SoapUI request.
i am getting a true response from SoapUI.
My code is shown below:
class SoapApi
require 'savon'
def self.initialize
header = {
"ebmCID" => "9366498d-bc79-4fad-be2b-fa1a0e84241a",
"ebmMID" => "9366498d-bc79-4fad-be2b-fa1a0e84241a",
"ebmRTID" => "9366498d-bc79-4fad-be2b-fa1a0e84241a",
"ebmSID" => "FMobile-FCUBS",
"ebmTimestamp" => "2019-06-10T12:27:46.1623586Z",
}
message = {
customerId: '00653473'
}
client = Savon.client(
:wsdl => "https://192.168.176.103:8012/tevs/pp.pm.evs.Customer_1.2?wsdl",
:ssl_verify_mode => :none
)
response = client.call(
:get_account_list,
:soap_header => header,
:message => message
)
return response
end
end
And here i am calling the above method:
#index.html.erb
<%=
SoapApi.initialize
puts #response
%>
Where you able to create a valid call using SoapUI (https://www.soapui.org.)? Try this first and make it work.
Next create a call from a plain ruby script - without Rails - which sends the same functional XML as you did in SoapUI before.
Third embed this code into your RoR application.
You can put the following in your client definition for better logging:
client = Savon.client(
:wsdl => "https://192.168.176.103:8012/tevs/pp.pm.evs.Customer_1.2?wsdl",
:ssl_verify_mode => :none,
log: true,
log_level: :debug,
pretty_print_xml: true
)
Compare the output with your working SoapUI example.

Ruby on Rails mailgun send mail with API fails

I am trying to send a mail with mailgun api but I always get a BAD REQUEST 400 error. The corresponding piece of code is
data = Multimap.new
data[:from] = "Excited User <postmaster#mydomain.mailgun.org"
data[:to] = "foo#bar.com"
data[:subject] = "Hello"
data[:text] = "Testing some Mailgun awesomness!"
RestClient.post "https://api:my-key"\
"#api.mailgun.net/v3/mydomain/messages", data
If I rewrite the code as
RestClient.post "https://api:my-key"\
"#api.mailgun.net/v3/mydomain/messages",
:from => "Mailgun Sandbox <postmaster#mydomain.mailgun.org>",
:to => "foo#bar.com",
:subject => "Hello",
:text => "Testing some Mailgun awesomness!"
it works but i want to add attachements, html etc . Am I missing something here?

How do I get clientId who published the message using faye

I have implemented faye for chatting services. But I want to know how can I find the clientId of that person who published the msg. I tried this:
faye_server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 45)
faye_server.bind(:publish) do |clientId, channel, data|
puts "client_id is #{clientId}"
end
But it gives me a blank ID. How can I fetch the clientID?

Troubleshooting Active Merchant returning "Failed with 500 Internal Server Error"

The following code
purchase = #order.authorize_payment(#credit_card, options)
is_success = purchase.success?
if is_success
...
else
flash[:notice] = "!! " + purchase.message + "" +
purchase.params['missingField'].to_s
redirect_to :action => :payment, :id => #order.id
end
results in "!! Failed with 500 Internal Server Error" in my flash[:notice]. There is no stacktrace, no webserver error, all that I know is that purchase.message is populated and purchase.success? is false.
I am really at a loss to figure out how to troubleshoot this. I think it might be an ssl requirement, but I can't either see the soap request, or test basic connectivity with cybersource (my payment gateway).
I establish my gateway with this code (after config.after_initialize do):
ActiveMerchant::Billing::Base.mode = :production # :test
ActiveMerchant::Billing::CreditCard.require_verification_value = false
ActiveMerchant::Billing::CyberSourceGateway.wiredump_device = File.new(File.join([Rails.root, "log", "cybersource.log"]), "a") # doesn't work (!)
# we need to open an external file to get the password
mypassphrase = File.open('/var/www/foo/shared/passphrase.txt').read
OrderTransaction.gateway = ActiveMerchant::Billing::CyberSourceGateway.new(:login => 'vxxxxxxx',
:password => mypassphrase.to_s,
:test => false,
:vat_reg_number => 'your VAT registration number',
# sets the states/provinces where you have a physical presense for tax purposes
:nexus => "GA OH",
# don‘t want to use AVS so continue processing even if AVS would have failed
:ignore_avs => true,
# don‘t want to use CVV so continue processing even if CVV would have failed
:ignore_cvv => true,
:money_format => :dollars
)
Can I see the soap request? Are there ways to test part of this? Any help greatly appreciated.
Best,
Tim
ActiveMerchant::Billing::CyberSourceGateway.logger = your_logger
So, late response but...
I've done a good amount of work with the Cybersource gateway, and the only way to see the SOAP request/response of the cybersource gateway currently is to open up the gem and edit it.
If you modify the commit method of lib/active_merchant/billing/gateways/cybersource.rb, you can do something like this:
def commit(request, options)
puts "*** POSTING TO: #{test? ? TEST_URL : LIVE_URL}"
request = build_request(request, options)
puts "*** POSTING:"
puts request
begin
post_response = ssl_post(test? ? TEST_URL : LIVE_URL, request)
rescue ActiveMerchant::ResponseError => e
puts "ERROR!"
puts e.response
end
puts post_response
It would be nice if there was a way to get that response without going through that hassle, I'll see if there's a way to pass that information up through the response object that's returned and add it to my fork.

Mechanize with FakeWeb

I'm using Mechanize to extract the links from the page.
To ease with development, I'm using fakeweb to do superfast response to get less waiting and annoying with every code run.
tags_url = "http://website.com/tags/"
FakeWeb.register_uri(:get, tags_url, :body => "tags.txt")
agent = WWW::Mechanize.new
page = agent.get(tags_url)
page.links.each do |link|
puts link.text.strip
end
When I run the above code, it says:
nokogiri_test.rb:33: undefined method `links' for #<WWW::Mechanize::File:0x9a886e0> (NoMethodError)
After inspecting the class of the page object
puts page.class # => File
If I don't fake out the tags_url, it works since the page class is now Page
puts page.class # => Page
So, how can I use the fakeweb with mechanize to return Page instead of File object?
Use FakeWeb to replay a prefetched HTTP request:
tags_url = "http://website.com/tags/"
request = `curl -is #{tags_url}`
FakeWeb.register_uri(:get, tags_url, :response => request)
agent = WWW::Mechanize.new
page = agent.get(tags_url)
page.links.each do |link|
puts link.text.strip
end
Calling curl with the -i flag will include headers in the response.
You can easily fix that adding the option :content_type => "text/html" you your FakeWeb.register_uri call

Resources