Class: Castle::Keep
- Inherits:
-
Object
- Object
- Castle::Keep
- Defined in:
- lib/castle/keep.rb
Defined Under Namespace
Classes: Error
Class Method Summary collapse
Instance Method Summary collapse
- #approve_auth(auth_id) ⇒ Object
- #create_auth(user_id) ⇒ Object
- #deny_auth(auth_id) ⇒ Object
- #events(user_id, page: 1, page_size: 200) ⇒ Object
-
#initialize(cookie_id, ip, headers) ⇒ Keep
constructor
A new instance of Keep.
-
#track(event_name, user_id: nil, details: nil) ⇒ Object
Available events: $login.succeeded: Record when a user attempts to log in.
Constructor Details
#initialize(cookie_id, ip, headers) ⇒ Keep
Returns a new instance of Keep.
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/castle/keep.rb', line 24 def initialize(, ip, headers) @http = Net::HTTP.new "api.castle.io", 443 @http.use_ssl = true @http.verify_mode = OpenSSL::SSL::VERIFY_PEER @headers = { "Content-Type" => "application/json", "X-Castle-Cookie-Id" => , "X-Castle-Ip" => ip, "X-Castle-Headers" => headers.to_json, } end |
Class Method Details
.create_context(request) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/castle/keep.rb', line 15 def self.create_context(request) Castle::Keep.new(request.['__cid'], request.ip, request.env.keys.grep(/^HTTP_/).map do |header| name = header.gsub(/^HTTP_/, '').split('_').map(&:capitalize).join('-') unless name == "Cookie" { name => request.env[header] } end end.compact.inject(:merge)) end |
Instance Method Details
#approve_auth(auth_id) ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/castle/keep.rb', line 84 def approve_auth(auth_id) req = Net::HTTP::Post.new("/v1/authentications/#{auth_id}/approve", @headers) req.basic_auth("", Castle.api_key) response = @http.request(req) unless response.code.to_i == 204 fail Error, "Response code: #{response.code}\nResponse body: #{response.body}" end end |
#create_auth(user_id) ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/castle/keep.rb', line 73 def create_auth(user_id) req = Net::HTTP::Post.new("/v1/authentications", @headers) req.basic_auth("", Castle.api_key) req.body = { user_id: user_id }.to_json response = @http.request(req) unless response.code.to_i == 201 fail Error, "Response code: #{response.code}\nResponse body: #{response.body}" end JSON.parse response.body end |
#deny_auth(auth_id) ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'lib/castle/keep.rb', line 93 def deny_auth(auth_id) req = Net::HTTP::Post.new("/v1/authentications/#{auth_id}/deny", @headers) req.basic_auth("", Castle.api_key) response = @http.request(req) unless response.code.to_i == 204 fail Error, "Response code: #{response.code}\nResponse body: #{response.body}" end end |
#events(user_id, page: 1, page_size: 200) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/castle/keep.rb', line 64 def events(user_id, page: 1, page_size: 200) req = Net::HTTP::Get.new("/v1/events?query=user_id:#{user_id}&page=#{page}&page_size=#{page_size}", @headers) req.basic_auth("", Castle.api_key) response = @http.request(req) unless response.code.to_i == 200 fail Error, "Response code: #{response.code}\nResponse body: #{response.body}" end end |
#track(event_name, user_id: nil, details: nil) ⇒ Object
Available events: $login.succeeded: Record when a user attempts to log in. $login.failed: Record when a user login failed. $logout.succeeded: Record when a user logs out. $registration.succeeded: Capture account creation, both when a user signs up as well as when created manually by an administrator. $registration.failed: Record when an account failed to be created. $email_change.requested: An attempt was made to change a user’s email. $email_change.succeeded: The user completed all of the steps in the email address change process and the email was successfully changed. $email_change.failed: Use to record when a user failed to change their email address. $password_reset.requested: An attempt was made to reset a user’s password. $password_reset.succeeded: The user completed all of the steps in the password reset process and the password was successfully reset. Password resets do not required knowledge of the current password. $password_reset.failed: Use to record when a user failed to reset their password. $password_change.succeeded: Use to record when a user changed their password. This event is only logged when users change their own password. $password_change.failed: Use to record when a user failed to change their password.
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/castle/keep.rb', line 50 def track(event_name, user_id: nil, details: nil) if user_id.nil? && details.nil? fail ArgumentError, "Missing both user_id and details" end req = Net::HTTP::Post.new("/v1/events", @headers) req.basic_auth("", Castle.api_key) req.body = { name: event_name, user_id: user_id, details: details }.to_json response = @http.request(req) unless response.code.to_i == 204 fail Error, "Response code: #{response.code}\nResponse body: #{response.body}" end end |