Skip to content

Instantly share code, notes, and snippets.

@edwardsharp
Created September 20, 2016 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edwardsharp/d94ba90be8979f75f34db2c1a42b21e5 to your computer and use it in GitHub Desktop.
Save edwardsharp/d94ba90be8979f75f34db2c1a42b21e5 to your computer and use it in GitHub Desktop.
httparty cookie
# 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