Created
February 6, 2013 12:35
-
-
Save snaka/4722288 to your computer and use it in GitHub Desktop.
rails console から web api を気軽に叩けるようにする ref: http://qiita.com/items/ad52d32a26643174a8eb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| login |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| get "/entries", { :user => "hoge" } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| post "/entries", { :user => "hoge", :body => "ほげほげ" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment