Last active
December 10, 2015 06:58
-
-
Save nowlinuxing/4397640 to your computer and use it in GitHub Desktop.
https://gist.github.com/4397599 は誤って作成してしまったもの
This file contains 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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'mechanize' | |
require 'uri' | |
class HttpClient | |
def initialize(base_url) | |
@client = Mechanize.new | |
#@client.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
@base_url = base_url | |
end | |
def login(login, password) | |
@login = login | |
@password = password | |
page = @client.get(URI.join(@base_url, "/session/new")) | |
page.form { |f| | |
f["user[login]"] = @login | |
f["user[password]"] = @password | |
}.click_button | |
end | |
def get(path) | |
@client.get(URI.join(@base_url, path)) | |
end | |
def post(path, body) | |
@client.post(URI.join(@base_url, path), body) | |
end | |
end | |
base_url = "http://localhost:3000/" | |
login = "user@example.com" | |
password = "password" | |
http_client = HttpClient.new(base_url) | |
http_client.login(login, password) | |
p http_client.get("/posts") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment