class NaranyaId::User
Attributes
date_of_birth[RW]
email[RW]
first_name[RW]
gender[RW]
id[R]
last_name[RW]
screen_name[RW]
Public Class Methods
find_one(options={})
click to toggle source
# File lib/naranya_id/models/user.rb, line 101 def self.find_one(options={}) options = options.with_indifferent_access raise "Token/Secret not provided" unless options.has_key?(:token) and options.has_key?(:secret) NaranyaId.logger.info "Consumer key: '#{NaranyaId.consumer.key}', Consumer secret: '#{NaranyaId.consumer.secret}'" NaranyaId.logger.info "Access token key: '#{options[:token]}', Access token secret: '#{options[:secret]}'" api_access_token = ::OAuth::AccessToken.new NaranyaId.consumer, options[:token], options[:secret] user_info_path = NaranyaId.config.client_options['api_path'] + '/user' user_info_response = api_access_token.get(user_info_path) if Net::HTTPSuccess === user_info_response response_data = ActiveSupport::JSON.decode(user_info_response.body).with_indifferent_access translated_data = {}.with_indifferent_access translated_data[:first_name] = response_data[:vc_name] unless response_data[:vc_name].nil? translated_data[:last_name] = response_data[:vc_lastname] unless response_data[:vc_lastname].nil? translated_data[:gender] = response_data[:i_gender] unless response_data[:i_gender].nil? translated_data[:date_of_birth] = response_data[:dt_birthday] unless response_data[:dt_birthday].nil? translated_data[:email] = response_data[:vc_email] unless response_data[:vc_email].nil? translated_data[:screen_name] = response_data[:vc_screenname] unless response_data[:vc_screenname].nil? user = self.new translated_data user.instance_variable_set :@id, response_data[:uuid] user.instance_variable_set :@access_token, api_access_token.token user.instance_variable_set :@access_secret, api_access_token.secret user else nil end end
Public Instance Methods
attributes()
click to toggle source
# File lib/naranya_id/models/user.rb, line 94 def attributes %w(id first_name last_name email gender date_of_birth screen_name).inject({}.with_indifferent_access) do |attrs, attr_name| attrs[attr_name] = instance_variable_get("@#{attr_name}") attrs end end
date_of_birth=(given_date_of_birth)
click to toggle source
# File lib/naranya_id/models/user.rb, line 26 def date_of_birth=(given_date_of_birth) @date_of_birth = if given_date_of_birth.respond_to? :monday? given_date_of_birth elsif given_date_of_birth.respond_to? :downcase Date.parse(given_date_of_birth) end end
gender=(given_gender)
click to toggle source
# File lib/naranya_id/models/user.rb, line 14 def gender=(given_gender) @gender = case given_gender.to_s.downcase when "male", "m" then :male else :female end end
inspect()
click to toggle source
Override de inspect
para evitar mostrar el access_token y el access_secret
# File lib/naranya_id/models/user.rb, line 81 def inspect inspection = "<#{self.class}:0x#{("%x" % (object_id * 2)).rjust(14,"0")}" inspection += " @id=#{@id.inspect}" unless @id.nil? inspection += " @first_name=#{@first_name.inspect}" unless @first_name.nil? inspection += " @last_name=#{@last_name.inspect}" unless @last_name.nil? inspection += " @email=#{@email.inspect}" unless @email.nil? inspection += " @gender=#{@gender.inspect}" unless @gender.nil? inspection += " @date_of_birth=#{@date_of_birth.inspect}" unless @date_of_birth.nil? inspection += " @screen_name=#{@screen_name.inspect}" unless @screen_name.nil? "#{inspection}>" end
persisted?()
click to toggle source
# File lib/naranya_id/models/user.rb, line 10 def persisted? @persisted === true end
save()
click to toggle source
# File lib/naranya_id/models/user.rb, line 34 def save return false unless self.valid? api_token = OAuth::AccessToken.new NaranyaId.consumer, @access_token, @access_secret nid_hash = {}.with_indifferent_access nid_hash[:name] = @first_name unless @first_name.nil? nid_hash[:lastname] = @last_name unless @last_name.nil? nid_hash[:gender] = case @gender when :male then 1 when :female then 0 end unless @gender.nil? nid_hash[:birthday] = @date_of_birth.to_s unless @date_of_birth.nil? nid_hash[:screenname] = @screen_name unless @screen_name.nil? put_path = NaranyaId.config.client_options['api_path'] + '/user' response = api_token.put put_path, nid_hash response_data = ActiveSupport::JSON.decode(response.body).with_indifferent_access if response.body =~ /\A{.*}/i if Net::HTTPSuccess elsif Net::HTTPClientError === response case response_data[:message] when "ERROR missing parametres" self.errors.add :base, "Missing parameters" when "ERROR date not valid", "ERROR invalid date format" self.errors.add :date_of_birth, "is invalid" when "ERROR value not valid for gender" self.errors.add :gender, "is invalid" else self.errors.add :base, "Naranya ID responded with a client error (#{response_data[:message]})" end elsif Net::HTTPServerError === response self.errors.add :base, "An error ocurred at Naranya ID server" else self.errors.add :base, "Naranya ID Server responded with an unexpected HTTP status code" end @persisted = self.errors.empty? @persisted end
update(given_attributes)
click to toggle source
# File lib/naranya_id/models/user.rb, line 21 def update(given_attributes) given_attributes.each { |attr, value| self.public_send("#{attr}=", value) } if given_attributes save end