Class: Hull::Client
Instance Method Summary
collapse
Methods included from Request
#delete, #get, #post, #put
Constructor Details
#initialize(attrs = {}) ⇒ Hull::Client
Initializes a new API object
19
20
21
22
23
24
|
# File 'lib/hull/client.rb', line 19
def initialize(attrs={})
attrs = Hull.options.merge(attrs)
Config::VALID_OPTIONS_KEYS.each do |key|
instance_variable_set("@#{key}".to_sym, attrs[key])
end
end
|
Instance Method Details
#app ⇒ Object
33
34
35
36
|
# File 'lib/hull/client.rb', line 33
def app
return unless app_id
@app ||= get("/app", :app_id => app_id)
end
|
#authenticate_user(env) ⇒ Object
52
53
54
55
56
57
58
59
|
# File 'lib/hull/client.rb', line 52
def authenticate_user env
require 'rack/request'
request = Rack::Request.new(env)
cookie = request.cookies["hull_#{self.app_id}"]
user_auth = read_cookie(cookie)
return unless user_auth
current_user_id(user_auth['Hull-User-Id'], user_auth['Hull-User-Sig'])
end
|
#credentials ⇒ Object
26
27
28
29
30
31
|
# File 'lib/hull/client.rb', line 26
def credentials
{
:app_id => app_id,
:app_secret => app_secret
}
end
|
#current_user_id(user_id, user_sig) ⇒ Object
44
45
46
47
48
49
50
|
# File 'lib/hull/client.rb', line 44
def current_user_id user_id, user_sig
return unless user_id && user_sig
time, signature = user_sig.split(".")
data = [time, user_id].join("-")
digest = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha1'), self.app_secret, data)
return user_id if digest == signature
end
|
#read_cookie(str) ⇒ Object
39
40
41
42
|
# File 'lib/hull/client.rb', line 39
def read_cookie str
return if str.nil? || str.length == 0
JSON.parse(Base64.decode64(str)) rescue nil
end
|
#user_hash(user_infos) ⇒ Object
61
62
63
64
65
66
|
# File 'lib/hull/client.rb', line 61
def user_hash user_infos
timestamp = Time.now.to_i.to_s
message = Base64.encode64(user_infos.to_json).gsub("\n", "")
sig = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha1'), app_secret, [message, timestamp].join(" "))
[message, sig, timestamp].join(" ")
end
|