Created
September 20, 2016 03:21
-
-
Save edwardsharp/d94ba90be8979f75f34db2c1a42b21e5 to your computer and use it in GitHub Desktop.
httparty cookie
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
# redash api wrapper class, example: | |
# require 'redash' | |
# redash = Redash.new('email', 'password') | |
# redash.logged_in? | |
require 'httparty' | |
class MyClass | |
include HTTParty | |
def initialize(email, password) | |
@email = email | |
post_response = self.class.post('/login', | |
body: { | |
email: email, | |
password: password | |
} | |
) | |
@cookie = parse_cookie(post_response) | |
end | |
def users | |
self.class.get('/api/users', headerz).parsed_response | |
end | |
def logged_in? | |
users.any? { |r| r["email"] == @email } | |
end | |
private | |
def headerz | |
{ headers: | |
{ | |
'Cookie' => @cookie.to_cookie_string, | |
'Content-Type' => 'application/json', | |
'User-Agent' => 'test-app' | |
} | |
} | |
end | |
def parse_cookie(resp) | |
cookie_hash = CookieHash.new | |
resp.get_fields('Set-Cookie').each { |c| cookie_hash.add_cookies(c) } | |
cookie_hash | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment