Discussion: Lecture 12
Add comments here with questions and notes regarding Lecture 12.
Here’s the complete REST client; I’m still working on cleaning up the server, but this is the key bit:
# Quick REST client demo
# To clear out old data:
# rake db:drop db:migrate
#
require 'rubygems'
require 'activeresource'
class Publication < ActiveResource::Base
self.site = 'http://localhost:3000'
self.user = 'john@7fff.com'
self.password = 'aaaa'
end
class Annotation < ActiveResource::Base
self.site = 'http://localhost:3000'
self.user = 'john@7fff.com'
self.password = 'aaaa'
end
def dump_publications
Publication.find(:all).each_with_index { |p, i| puts " #{i}. #{p.title}" }
end
puts "Publications at start:"
dump_publications
c_book_title = 'The C Programming Language'
p = Publication.new(:title => c_book_title)
p.save
puts "Publications after adding one:"
dump_publications
if Publication.find(:first, :params => { :title => c_book_title})
puts "There is a book named #{c_book_title}"
end
annotations = Annotation.find(:all, :from => '/publications/1/annotations.xml')
annotations.each_with_index { |a, i| puts " #{i}. #{a.title}" }
And here is the monkeypath to get Clearance to use basic auth for REST requests; put this in your config/initializers directory under any name; I used resful_clearance.rb. This is “the simplest thing that could actually work”; we don’t do anything special to produce a “nice” error for the client that fails to authenticate.
module Clearance
module Authentication
module InstanceMethods
def authenticate_with_rest
if params[:format] == 'xml'
authenticate_or_request_with_http_basic do |email, password|
if current_user = ::User.authenticate(email, password)
return
end
end
end
authenticate_without_rest
end
alias_method_chain :authenticate, :rest
end
end
end
What am I doing wrong here?
@Ron Newman
From your Mac?
Um . . . I know that on Linux it is possible to get your Net::HTTP screwed up, but I haven't seen this on a Mac. I would resort to the Google . . .
@Ron Newman
Haven't tried it from irb, but this works on my Mac as a separate program:
I get the exact same error if I run that program (whether or not I change the password to the correct one).
Oddly, it works fine if I switch to Ruby 1.8.7.
@Ron Newman
Works for me under 1.8.6, 1.9.1.
I think I'm having this problem, and wonder why you aren't ...
https://rails.lighthouseapp.com/projects/8994/tickets/1272-ruby19-incompatibility-activeresourceconnectionhttp-use_ssl
https://rails.lighthouseapp.com/projects/8994/tickets/2971-activeresource-and-ruby-191
https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1272-ruby19-incompatibility-activeresourceconnectionhttp-use_ssl
@Ron Newman
No idea, but that should be a very easy monkeypatch . . .
Apparently it's a very bad idea to specify :skip_types =>true when calling .to_xml on an array of ActiveRecord model objects. Doing so causes the ActiveResource client to complain cryptically:
@Ron Newman
Wow.
If you want to get info from a resource in a "typeless" fashion, you should check out HttParty.
By the way, here's the monkeypatch for the use_ssl problem. Still don't understand why I needed it and you didn't ....