omniauth-naranya_id 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f4fc44aec6bf352103357b19e5c6a45bc4aad04c
4
- data.tar.gz: 8e3a9bf5e82bb02c77b99341536173b05f9f6ef3
3
+ metadata.gz: a2282898f510b1a141d381ec7a9a13b7cb76e4b7
4
+ data.tar.gz: a976d4c379a3035da50030195be7f70a15145fe7
5
5
  SHA512:
6
- metadata.gz: 86850421cd48c60ec7f0194f83ded1298b471f4a135af4d94e12340fb430775f14899528b9aeec477a98d19f13e05d1e3224654ea0302ada689b31b0ce205abb
7
- data.tar.gz: 00b691d5859687c9783f6eb46325be64becd2af2900083869226d89df373f9c310f80deaaf2421f6b2139ff1d6375d86406f13dc6a0a586abc970824055f8854
6
+ metadata.gz: 5df77d967b4f20ae5d453b44a8bdf34c8a2e2b11646dd2054ea1fcb73115a32f148b759f7ff792d81e7637cf15a9843f4f1651923adedbff028e8f6d578a881a
7
+ data.tar.gz: 0c5563981947192f1b0f08cab79c58d2e2d5e4016d47e6719fcb268e699045007508f9c850c1a27e63e1cc84d66d60ae96b78f466189f3cd8d6021444ba7869b
@@ -4,49 +4,132 @@ module NaranyaId
4
4
  class User
5
5
  include ActiveModel::Model
6
6
 
7
- attr_accessor :id, :first_name, :last_name, :email, :gender, :date_of_birth, :screen_name
8
-
9
- def attributes
10
- Hash[to_yaml_properties.map { |attr| [attr.to_s[1..-1].to_sym, instance_variable_get(attr)] }]
7
+ attr_accessor :first_name, :last_name, :email, :gender, :date_of_birth, :screen_name
8
+ attr_reader :id
9
+
10
+ def persisted?
11
+ @persisted === true
11
12
  end
12
13
 
13
- class << self
14
+ def gender=(given_gender)
15
+ @gender = case given_gender.to_s.downcase
16
+ when "male", "m" then :male
17
+ else :female
18
+ end
19
+ end
14
20
 
15
- def translate_attributes(attributes)
16
- translated_attributes = {
17
- id: attributes[:uuid],
18
- first_name: attributes[:nombre],
19
- last_name: attributes[:apellido],
20
- email: attributes[:email],
21
- screen_name: attributes[:screenname]
22
- }
21
+ def update(given_attributes)
22
+ given_attributes.each { |attr, value| self.public_send("#{attr}=", value) } if given_attributes
23
+ save
24
+ end
23
25
 
24
- translated_attributes[:date_of_birth] = attributes[:nacimiento].to_date if attributes[:nacimiento].present?
25
- translated_attributes
26
+ def date_of_birth=(given_date_of_birth)
27
+ @date_of_birth = if given_date_of_birth.respond_to? :monday?
28
+ given_date_of_birth
29
+ elsif given_date_of_birth.respond_to? :downcase
30
+ Date.parse(given_date_of_birth)
26
31
  end
32
+ end
27
33
 
28
- def find_one(options={})
29
- options = options.with_indifferent_access
30
-
31
- raise "Token/Secret not provided" unless options.has_key?(:token) and options.has_key?(:secret)
34
+ def save
35
+ return false unless self.valid?
36
+
37
+ api_token = OAuth::AccessToken.new NaranyaId.consumer, @access_token, @access_secret
38
+
39
+ nid_hash = {}.with_indifferent_access
40
+ nid_hash[:name] = @first_name unless @first_name.nil?
41
+ nid_hash[:lastname] = @last_name unless @last_name.nil?
42
+
43
+ nid_hash[:gender] = case @gender
44
+ when :male then 1
45
+ when :female then 0
46
+ end unless @gender.nil?
32
47
 
33
- api_consumer = NaranyaId.consumer
34
-
35
- NaranyaId.logger.info "Consumer key: '#{api_consumer.key}', Consumer secret: '#{api_consumer.secret}'"
36
- NaranyaId.logger.info "Access token key: '#{options[:token]}', Access token secret: '#{options[:secret]}'"
37
- api_access_token = ::OAuth::AccessToken.new api_consumer, options[:token], options[:secret]
38
-
39
- user_info_response = api_access_token.get('/crmcapi/index.php/user')
40
-
41
- if user_info_response.code.to_i == 200
42
- user_info = ActiveSupport::JSON.decode(user_info_response.body).with_indifferent_access
43
- self.new translate_attributes(user_info)
48
+ nid_hash[:birthday] = @date_of_birth.to_s unless @date_of_birth.nil?
49
+ nid_hash[:screenname] = @screen_name unless @screen_name.nil?
50
+
51
+ response = api_token.put "/crmcapi/user", nid_hash
52
+ response_data = ActiveSupport::JSON.decode(response.body).with_indifferent_access if response.body =~ /\A{.*}/i
53
+
54
+ if Net::HTTPSuccess
55
+ elsif Net::HTTPClientError === response
56
+
57
+ case response_data[:message]
58
+ when "ERROR missing parametres"
59
+ self.errors.add :base, "Missing parameters"
60
+ when "ERROR date not valid", "ERROR invalid date format"
61
+ self.errors.add :date_of_birth, "is invalid"
62
+ when "ERROR value not valid for gender"
63
+ self.errors.add :gender, "is invalid"
44
64
  else
45
- nil
65
+ self.errors.add :base, "Naranya ID responded with a client error (#{response_data[:message]})"
46
66
  end
47
67
 
68
+ elsif Net::HTTPServerError === response
69
+ self.errors.add :base, "An error ocurred at Naranya ID server"
70
+
71
+ else
72
+ self.errors.add :base, "Naranya ID Server responded with an unexpected HTTP status code"
73
+ end
74
+
75
+ @persisted = self.errors.empty?
76
+ @persisted
77
+ end
78
+
79
+ # Override de #inspect para evitar mostrar el access_token y el access_secret
80
+ def inspect
81
+ inspection = "<#{self.class}:0x#{("%x" % (object_id * 2)).rjust(14,"0")}"
82
+ inspection += " @id=#{@id.inspect}" unless @id.nil?
83
+ inspection += " @first_name=#{@first_name.inspect}" unless @first_name.nil?
84
+ inspection += " @last_name=#{@last_name.inspect}" unless @last_name.nil?
85
+ inspection += " @email=#{@email.inspect}" unless @email.nil?
86
+ inspection += " @gender=#{@gender.inspect}" unless @gender.nil?
87
+ inspection += " @date_of_birth=#{@date_of_birth.inspect}" unless @date_of_birth.nil?
88
+ inspection += " @screen_name=#{@screen_name.inspect}" unless @screen_name.nil?
89
+
90
+ "#{inspection}>"
91
+ end
92
+
93
+ def attributes
94
+ %w(first_name last_name email gender date_of_birth screen_name).inject({}.with_indifferent_access) do |attrs, attr_name|
95
+ attrs[attr_name] = instance_variable_get("@#{attr_name}")
96
+ attrs
48
97
  end
98
+ end
49
99
 
100
+ def self.find_one(options={})
101
+ options = options.with_indifferent_access
102
+ raise "Token/Secret not provided" unless options.has_key?(:token) and options.has_key?(:secret)
103
+
104
+ NaranyaId.logger.info "Consumer key: '#{NaranyaId.consumer.key}', Consumer secret: '#{NaranyaId.consumer.secret}'"
105
+ NaranyaId.logger.info "Access token key: '#{options[:token]}', Access token secret: '#{options[:secret]}'"
106
+
107
+ api_access_token = ::OAuth::AccessToken.new NaranyaId.consumer, options[:token], options[:secret]
108
+
109
+ user_info_response = api_access_token.get('/crmcapi/index.php/user')
110
+
111
+ if Net::HTTPSuccess === user_info_response
112
+ response_data = ActiveSupport::JSON.decode(user_info_response.body).with_indifferent_access
113
+
114
+ translated_data = {}.with_indifferent_access
115
+ translated_data[:first_name] = response_data[:vc_name] unless response_data[:vc_name].nil?
116
+ translated_data[:last_name] = response_data[:vc_lastname] unless response_data[:vc_lastname].nil?
117
+ translated_data[:gender] = response_data[:i_gender] unless response_data[:i_gender].nil?
118
+ translated_data[:date_of_birth] = response_data[:dt_birthday] unless response_data[:dt_birthday].nil?
119
+ translated_data[:email] = response_data[:vc_email] unless response_data[:vc_email].nil?
120
+ translated_data[:screen_name] = response_data[:vc_screenname] unless response_data[:vc_screenname].nil?
121
+
122
+ user = self.new translated_data
123
+
124
+ user.instance_variable_set :@id, response_data[:uuid]
125
+ user.instance_variable_set :@access_token, api_access_token.token
126
+ user.instance_variable_set :@access_secret, api_access_token.secret
127
+
128
+ user
129
+ else
130
+ nil
131
+ end
132
+
50
133
  end
51
134
 
52
135
  end
@@ -1,5 +1,5 @@
1
1
  module Omniauth
2
2
  module NaranyaId
3
- VERSION = "0.0.10"
3
+ VERSION = "0.0.11"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,139 +1,139 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniauth-naranya_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roberto Quintanilla
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-03 00:00:00.000000000 Z
11
+ date: 2014-10-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '4'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '4'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activemodel
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '4'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '4'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: omniauth-oauth
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '1.0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: bundler
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ~>
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
61
  version: '1.6'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ~>
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1.6'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rake
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - '>='
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - '>='
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rspec
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - '>='
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - '>='
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: rack-test
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - '>='
101
+ - - ">="
102
102
  - !ruby/object:Gem::Version
103
103
  version: '0'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - '>='
108
+ - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: simplecov
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
- - - '>='
115
+ - - ">="
116
116
  - !ruby/object:Gem::Version
117
117
  version: '0'
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
- - - '>='
122
+ - - ">="
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: webmock
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
- - - '>='
129
+ - - ">="
130
130
  - !ruby/object:Gem::Version
131
131
  version: '0'
132
132
  type: :development
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
- - - '>='
136
+ - - ">="
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
139
  description: Omniauth provider for NaranyaID.
@@ -143,8 +143,8 @@ executables: []
143
143
  extensions: []
144
144
  extra_rdoc_files: []
145
145
  files:
146
- - .gitignore
147
- - .rspec
146
+ - ".gitignore"
147
+ - ".rspec"
148
148
  - Gemfile
149
149
  - LICENSE.txt
150
150
  - README.md
@@ -168,17 +168,17 @@ require_paths:
168
168
  - lib
169
169
  required_ruby_version: !ruby/object:Gem::Requirement
170
170
  requirements:
171
- - - '>='
171
+ - - ">="
172
172
  - !ruby/object:Gem::Version
173
173
  version: '0'
174
174
  required_rubygems_version: !ruby/object:Gem::Requirement
175
175
  requirements:
176
- - - '>='
176
+ - - ">="
177
177
  - !ruby/object:Gem::Version
178
178
  version: '0'
179
179
  requirements: []
180
180
  rubyforge_project:
181
- rubygems_version: 2.0.14
181
+ rubygems_version: 2.2.2
182
182
  signing_key:
183
183
  specification_version: 4
184
184
  summary: Omniauth provider for NaranyaID.