Skip to content

Instantly share code, notes, and snippets.

@vagnerzampieri
Created May 22, 2012 13:20
Show Gist options
  • Select an option

  • Save vagnerzampieri/2769000 to your computer and use it in GitHub Desktop.

Select an option

Save vagnerzampieri/2769000 to your computer and use it in GitHub Desktop.
api
require 'rubygems'
require 'active_record'
require 'json'
require 'net/http'
require 'uri'
ActiveRecord::Base.establish_connection(
:adapter => "postgresql",
:host => "localhost",
:username => "postgres",
:password => "postgres",
:database => "shoes_post"
)
#require File.expand_path('../post', __FILE__)
require 'post'
class Api
=begin
url = 'http://localhost:3000/posts.json'
uri = URI.parse(url)
response = Net::HTTP.get_response(uri)
data = response.body
result = JSON.parse(data)
puts result
=end
host = 'localhost:3000'
url = 'http://localhost:3000/posts.json'
uri = URI.parse(url)
post_ws = '/posts.json'
posts = Post.all
posts.each do |p|
post = {'title' => p.title, 'body' => p.body}.to_json
req = Net::HTTP::Post.new(post_ws, initheader = {'Content-Type' => 'application/json'})
req.body = post
response = Net::HTTP.new(uri.host, uri.port).start {|http| http.request(req)}
puts "Response #{response.code} #{response.message}: #{response.body}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment