Skip to content

Instantly share code, notes, and snippets.

@snaka
Created February 6, 2013 12:35
Show Gist options
  • Select an option

  • Save snaka/4722288 to your computer and use it in GitHub Desktop.

Select an option

Save snaka/4722288 to your computer and use it in GitHub Desktop.
rails console から web api を気軽に叩けるようにする ref: http://qiita.com/items/ad52d32a26643174a8eb
class Object
# login する
def login
@base_uri = "http://localhost:3000"
@agent = Mechanize.new
page = @agent.get("#{@base_uri}/users/sign_in")
page.form["user[email]"] = "user@example.com"
page.form["user[password]"] = "password"
page = @agent.submit(page.form)
@authenticity_token = page.form["authenticity_token"]
end
def get(url, params = {})
page = @agent.get "#{@base_uri}#{url}", params
pp JSON.parse(page.body) unless page.body.blank?
nil
end
def post(url, params = {})
params.merge!( :authenticity_token => @authenticity_token )
page = @agent.post "#{@base_uri}#{url}", params
pp JSON.parse(page.body) unless page.body.blank?
nil
end
end
get "/entries", { :user => "hoge" }
post "/entries", { :user => "hoge", :body => "ほげほげ" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment