23 Nov 2010, 17:35
Img_0370_pragsmall

Bill Tihen (8 posts)

I would like to learn to elegantly read in data using a json feed into the aside server - instead of using multiple -d fields.

I have changed aside into a quote server & the

curl -i –url http://localhost:4567/quotes -d author=’Nike’ -d quote=’Just do it’ -X POST

this works just fine, but I find the below code ugly & the curl command too.

I would also like to use something like:

curl -i –url http://localhost:4567/quotes.json -H “Content-Type: application/json” -X POST -d ‘{“quote”:{“author”:”Buddha”,”quote”:”Just Be”}}’ -X POST

but I get:

127.0.0.1 - - [23/Nov/2010 18:26:15] "GET /quotes:1 HTTP/1.1" 404 26 0.0005
127.0.0.1 - - [23/Nov/2010 18:26:23] "GET /quotes/1 HTTP/1.1" 200 137 0.0222
TypeError - can't convert nil into String:
 /opt/local/lib/ruby/gems/1.8/gems/json-1.4.6/lib/json/common.rb:146:in `initialize'
 /opt/local/lib/ruby/gems/1.8/gems/json-1.4.6/lib/json/common.rb:146:in `new'
 /opt/local/lib/ruby/gems/1.8/gems/json-1.4.6/lib/json/common.rb:146:in `parse'
 quotes.rb:72:in `POST /quotes.json'

` require ‘rubygems’ require ‘sinatra’ require ‘active_record’

configure do

ActiveRecord::Base.establish_connection( :adapter => ‘sqlite3’, :database => ‘quotes.sqlite3’)

begin ActiveRecord::Schema.define do create_table :quotes do |t| t.text :quote, :null => false t.text :author, :null => false t.timestamps end end rescue ActiveRecord::StatementInvalid # Do nothing, since the schema already exists end end

class Quote < ActiveRecord::Base validates_uniqueness_of :quote

scope :recent, {:limit => 10, :order => ‘updated_at DESC’} end

helpers do

def base_url if Sinatra::Application.port == 80 “http://#{Sinatra::Application.bind}/” else “http://#{Sinatra::Application.bind}:#{Sinatra::Application.port}/” end end

def quote_url(quote) “#{base_url}quotes/#{quote.id}” end

def rfc_3339(timestamp) timestamp.strftime(“%Y-%m-%dT%H:%M:%SZ”) end

end

add a new quote to DB

post ‘/quotes’ do quote = Quote.new(:author => params[:author], :quote => params[:quote] ) if quote.save status(201) response[‘Location’] = quote_url(quote)

"Created quote #{quote.id} by #{quote.author} with text \"#{quote.quote}\"\n #{
params.inspect} \n"   else
status(412)

"Error: Duplicate body\n"   end end

add a new quote to DB

post ‘/quotes.json’ do quote_array = JSON.parse(params[:data]) quote = Quote.new( quote_array ) if quote.save status(201) response[‘Location’] = quote_url(quote)

"Created quote #{quote.id} by #{quote.author} with text \"#{quote.quote}\""   else
status(412)       "Error: Duplicate body\n"   end end

not_found do status(404) @msg || “I do not know about that!\n” end `

If any one has ideas, I would appreciate that.

Thanks,

Bill

23 Nov 2010, 17:36
Img_0370_pragsmall

Bill Tihen (8 posts)

Here is the error to trying to load via json:

127.0.0.1 - - [23/Nov/2010 18:26:15] “GET /quotes:1 HTTP/1.1” 404 26 0.0005 127.0.0.1 - - [23/Nov/2010 18:26:23] “GET /quotes/1 HTTP/1.1” 200 137 0.0222 TypeError - can’t convert nil into String: /opt/local/lib/ruby/gems/1.8/gems/json-1.4.6/lib/json/common.rb:146:in initialize' /opt/local/lib/ruby/gems/1.8/gems/json-1.4.6/lib/json/common.rb:146:in new’ /opt/local/lib/ruby/gems/1.8/gems/json-1.4.6/lib/json/common.rb:146:in parse' quotes.rb:72:in POST /quotes.json’

Here is the code I was using:

require ‘rubygems’ require ‘sinatra’ require ‘active_record’

configure do

ActiveRecord::Base.establish_connection( :adapter => ‘sqlite3’, :database => ‘quotes.sqlite3’)

begin ActiveRecord::Schema.define do create_table :quotes do |t| t.text :quote, :null => false t.text :author, :null => false t.timestamps end end rescue ActiveRecord::StatementInvalid # Do nothing, since the schema already exists end end

class Quote < ActiveRecord::Base validates_uniqueness_of :quote

scope :recent, {:limit => 10, :order => ‘updated_at DESC’} end

helpers do

def base_url if Sinatra::Application.port == 80 “http://#{Sinatra::Application.bind}/” else “http://#{Sinatra::Application.bind}:#{Sinatra::Application.port}/” end end

def quote_url(quote) “#{base_url}quotes/#{quote.id}” end

def rfc_3339(timestamp) timestamp.strftime(“%Y-%m-%dT%H:%M:%SZ”) end

end

add a new quote to DB

post ‘/quotes’ do quote = Quote.new(:author => params[:author], :quote => params[:quote] ) if quote.save status(201) response[‘Location’] = quote_url(quote)

"Created quote #{quote.id} by #{quote.author} with text \"#{quote.quote}\"\n #{
params.inspect} \n"   else
status(412)

"Error: Duplicate body\n"   end end

add a new quote to DB

post ‘/quotes.json’ do quote_array = JSON.parse(params[:data]) quote = Quote.new( quote_array ) if quote.save status(201) response[‘Location’] = quote_url(quote)

"Created quote #{quote.id} by #{quote.author} with text \"#{quote.quote}\""   else
status(412)       "Error: Duplicate body\n"   end end

not_found do status(404) @msg || “I do not know about that!\n” end

  You must be logged in to comment