Issues using Savon in Ruby on Rails - ruby

I've inherited some Ruby on Rails code and am trying to get it up and running locally. I've seen it work on other people's machines but for me it is throwing an exception. The exception is Savon::UnknownOptionError in DevicesController#index.
What is causing the exception is "Unknown global option: :document=".
Specifically it is failing at row 2 of this call:
wsdl_url = "valid url"
#client = Savon::Client.new do |wsdl|
wsdl.document = wsdl_url
end

it seems the code was written for Savon 1.x The current version is 2.2.0
You could explicitely require the old version with
gem 'savon', '=1.2.0'
wsdl_url = 'http://www.example.com?wsdl'
#client = Savon::Client.new do
wsdl.document = wsdl_url
end
response = #client.request :wsdl, :your_method
print response.to_hash
What I would recommend though is to change the code so it will run with the current version. At http://savonrb.com/version2.html you'll find comprehensive documentation and examples.

Related

Ruby Error: koala no such file to load LoadError

I have installed Ruby (not Rails) on a machine and am trying to run some code based off the koala framework for Facebook.
When I run
gem list
koala is mentioned but when I run the file, I get this error. Rubygems is already installed, I'm not sure what else to do. Any ideas?
Edit:
require 'koala'
require 'json'
#graph = Koala::Facebook::API.new("CAACEdEose0cBANb7YuygrBflSkBrpdalb4e70T5lJgdLPYEh0Uxy5JLPVdukKSiwZAK8g27DnwscSUWNaC0s53ogq6h562LETjYO4sB5lZAMAy8tC0SM9UzqXkk7GKYpaLrkQlgj1oLTdOJhBfq5KJtFxZBkOkpz8HaVPLYp66OnuGkaGOogVseR1tUNXVxToKl6ZCmwHL0i5RHNvMnd")
url = File.open("urls.txt","r")
url.each_line do |line|
id = /[\d]+/.match(line)
begin
temp = #graph.get_object(id)
list = File.open("working.txt", "a")
list.write(id)
list.write("\n")
puts "Worked for #{id}."
rescue
puts "Didn't work for #{id}."
end
end

getting a Twitter::Cursor object with the Twitter gem

I'm playing around with the Ruby Twitter gem and wish to use the methods that are available on the Cursor object. For example, using the Twitter::Cursor I'm supposed to be able to get a an array of all friends by doing
client.friends.to_a
or get a most recent follower with
client.friends.first
However, in my attempt below, when tried to do client.friends.first for kanyewest, I got an error which showed that I'm using the Twitter::User object, not the Twitter::Cursor
undefined methodfriends' for #
How can I use the gem to get a cursor object that will allow me to query Kanye's friends.
client.friends.to_a
Note, I read the documentation for creating a new cursor object but I found it a little abstract. I'm not sure if you're supposed to call the constructor directly? If so, please show me how I'd do that
- (Twitter::Cursor) initialize(attrs, key, klass, request)
My Failing code
#!/usr/bin/env ruby
require 'rubygems'
require 'twitter'
client = Twitter::REST::Client.new do |config|
config.consumer_key = "8nwa....."
config.consumer_secret = "Wj20r....."
config.access_token = "363......"
config.access_token_secret = "7eydU2n....."
end
kanyewest = client.user("kanyewest")
puts kanyewest.friends.first
client.friends("kanyewest").first should work. friends is a method on client not on Twitter::User

Resque + Airbrake, not seeing exceptions

I'm using the Airbrake support that comes with Resque:
require 'resque/failure/multiple'
require 'resque/failure/airbrake'
require 'resque/failure/redis'
Resque::Failure::Airbrake.configure do |config|
config.api_key = 'xxxxx'
end
Resque::Failure::Multiple.classes = [Resque::Failure::Redis, Resque::Failure::Airbrake]
Resque::Failure.backend = Resque::Failure::Multiple
And trying out a simple job:
class Fail
#queue = :propagation
def self.perform
raise "Fail!"
end
end
I see the error in resque-web, but it never shows up in Airbrake. What could I be doing wrong?
Yeah, this is not that clear from the gem readme...
If you're raising errors in development, you should try adding config.development_environments = [] to your Airbrake configuration.
Cheers from Airbrake support! :)

Ruby namespacing issues

I'm attempting to build a gem for interacting w/ the Yahoo Placemaker API but I'm running into an issue. When I attempt to run the following code I get:
NameError: uninitialized constant Yahoo::Placemaker::Net
from /Users/Kyle/.rvm/gems/ruby-1.9.2-p290/gems/yahoo-placemaker-0.0.1/lib/yahoo-placemaker.rb:17:in `extract'
from (irb):4
from /Users/Kyle/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'
yahoo-placemaker.rb
require "yahoo-placemaker/version"
require 'json'
require 'ostruct'
require 'net/http'
module Yahoo
module Placemaker
def self.extract (text = '')
host = 'wherein.yahooapis.com'
payload = {
'documentContent' => text,
'appid' => APP_ID,
'outputType' => 'json',
'documentType' => 'text/plain'
}
req = Net::HTTP::Post.new('/v1/document')
req.body = to_url_params(payload)
response = Net::HTTP.new(host).start do |http|
http.request(req)
end
json = JSON.parse(response.body)
Yahoo::Placemaker::Result.new(json)
end
end
end
I have yet to figure out how exactly constant name resolution works in Ruby (I think the rules are a bit messy here), but from my experience it could well be that Net is looked up in the current namespace instead of the global one. Try using the fully qualified name:
::Net::HTTP::Post.new
A similar problem could occur in this line:
Yahoo::Placemaker::Result
You should replace it with either ::Yahoo::Placemaker::Result or better Result (as it lives in the current namespace).
Try requiring net/http before. Ruby is falling back to find it in the module if it isn't defined.
require 'net/http'

How to perform simple web service client with Ruby and Savon

I'm trying to develop a simple example of a web service client in Ruby using Savon.
This is what I got so far:
class WebServiceController < ApplicationController
def index
puts "web_service: IN"
client = Savon::Client.new do
wsdl.document = "http://www.webservicex.com/CurrencyConvertor.asmx?wsdl"
end
response = client.request :conversion_rate do
soap.body = {
:from_currency => 'USD',
:to_currency => 'EUR'
}
end
puts response.to_hash;
render :text => response.to_hash.to_s
end
end
However, when I run that code I get:
uninitialized constant Savon::Client
I guess I have to add some reference to Savon? (I already installed the corresponding gem).
In addition: am I doing the right thing in that web service? Should it work?
Thank you for your time!
If this is a Rails 3 application, add this onto your Gemfile:
gem 'savon'
Then, run bundle install and restart your development server.
I suppose you've added
require 'savon'
somewhere in your file?

Resources