Class: NaranyaId::User

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/naranya_id/models/user.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#date_of_birthObject

Returns the value of attribute date_of_birth.



7
8
9
# File 'lib/naranya_id/models/user.rb', line 7

def date_of_birth
  @date_of_birth
end

#emailObject

Returns the value of attribute email.



7
8
9
# File 'lib/naranya_id/models/user.rb', line 7

def email
  @email
end

#first_nameObject

Returns the value of attribute first_name.



7
8
9
# File 'lib/naranya_id/models/user.rb', line 7

def first_name
  @first_name
end

#genderObject

Returns the value of attribute gender.



7
8
9
# File 'lib/naranya_id/models/user.rb', line 7

def gender
  @gender
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/naranya_id/models/user.rb', line 8

def id
  @id
end

#last_nameObject

Returns the value of attribute last_name.



7
8
9
# File 'lib/naranya_id/models/user.rb', line 7

def last_name
  @last_name
end

#screen_nameObject

Returns the value of attribute screen_name.



7
8
9
# File 'lib/naranya_id/models/user.rb', line 7

def screen_name
  @screen_name
end

Class Method Details

.find_one(options = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# 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]

   = NaranyaId.config.client_options['api_path'] + '/user'
   = api_access_token.get()

  if Net::HTTPSuccess === 
    response_data = ActiveSupport::JSON.decode(.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

Instance Method Details

#attributesObject



94
95
96
97
98
99
# 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

#inspectObject

Override de #inspect para evitar mostrar el access_token y el access_secret



81
82
83
84
85
86
87
88
89
90
91
92
# 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?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/naranya_id/models/user.rb', line 10

def persisted?
  @persisted === true
end

#saveObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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) ⇒ Object



21
22
23
24
# 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