Skip to content

Instantly share code, notes, and snippets.

@atombender
Created June 30, 2012 19:52
Show Gist options
  • Save atombender/3025268 to your computer and use it in GitHub Desktop.
Save atombender/3025268 to your computer and use it in GitHub Desktop.
# Først må du gjøre:
#
# gem install httpclient
#
require 'httpclient'
require 'json'
class KryssordWrapper
class APIError < StandardError; end
def initialize
@client = HTTPClient.new
# Slå på for å få debug-output:
#@client.debug_dev = $stdout
end
def login(username, password)
response = check_response(@client.request('POST', "http://www.kryssord.no/leksikon/index.php", nil, {
'username' => username,
'password' => password,
'page_id' => 48
}, {
"Content-Type" => "application/x-www-form-urlencoded",
"X-Requested-With" => "XMLHttpRequest"
}))
JSON.parse(response.body)
end
private
def check_response(response)
status = response.status.to_i
unless (200..299).include?(status)
raise APIError, "Failed with status #{status}"
end
response
end
end
k = KryssordWrapper.new
p k.login("foo", "bar")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment