class Togglehq::User
Attributes
identifier[RW]
Public Class Methods
find_by_identifier(identifier)
click to toggle source
Finds an app user by the given identifier. If no record is found, returns nil.
# File lib/togglehq/user.rb, line 10 def self.find_by_identifier(identifier) response = Togglehq::Request.new("/users/#{identifier}").get! if response.status == 404 return nil elsif response.status == 200 json = JSON.parse(response.body) user = Togglehq::User.new(:identifier => identifier) user.persisted! return user else raise "Unexpected error finding user" end end
find_by_identifier!(identifier)
click to toggle source
Like find_by_identifier
, except that if no record is found, raises an RuntimeError.
# File lib/togglehq/user.rb, line 25 def self.find_by_identifier!(identifier) user = self.find_by_identifier(identifier) raise "Could not find user with identifier #{identifier}" if user.nil? return user end
new(params = {})
click to toggle source
# File lib/togglehq/user.rb, line 5 def initialize(params = {}) @identifier = params[:identifier] end
Public Instance Methods
persisted!()
click to toggle source
@private
# File lib/togglehq/user.rb, line 48 def persisted! @persisted = true end
save()
click to toggle source
Saves a new user
# File lib/togglehq/user.rb, line 32 def save response = Togglehq::Request.new("/users", {"user" => {"identifier" => self.identifier}}).post! if response.status == 200 self.persisted! json = JSON.parse(response.body) if json.has_key?("message") && json["message"] == "user already exists" # load this user's preferences user = Togglehq::User.find_by_identifier(self.identifier) end return true else raise "unexpected error saving user" end end