plyom_user 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/plyom_user.rb +200 -3
- data/lib/plyom_user/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c958b690c33b6768b1a60584b012be5ee1b008b8
|
4
|
+
data.tar.gz: b785fe32bd94f9f276ff5cdb4ae1ee5820bea8f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 64d2f3e3bc2ded05f0cbae1929470ba7f74fdbdec81e00abea078252c907da13fa99be2048aa6356df2ad48ff664a3deb306b6463233a8dfea607b4e12e08313
|
7
|
+
data.tar.gz: 623531d0324cbc48dcb5ff24e1a939bc2cb523745f20bb151733ae5a138b75f729b7dd0958f80afc0bc5747445fb411018a906dd771388f53347f923ba74ca6d
|
data/lib/plyom_user.rb
CHANGED
@@ -1,5 +1,202 @@
|
|
1
1
|
require "plyom_user/version"
|
2
|
-
require "json"
|
3
2
|
require "httparty"
|
4
|
-
require "
|
5
|
-
|
3
|
+
require "json"
|
4
|
+
|
5
|
+
module Plyom
|
6
|
+
class PlyomUser
|
7
|
+
class_attribute :id, :name, :email, :username, :password, :password_confirmation, :password_digest, :password_reset_token, :password_reset_sent_at, :status, :token, :product_ids, :created_at, :updated_at
|
8
|
+
|
9
|
+
def initialize(params={})
|
10
|
+
params.each { |var, val| public_send("#{var}=", val) }
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.all
|
14
|
+
list = []
|
15
|
+
response = action_by("read")
|
16
|
+
objs = JSON.parse(response.body)
|
17
|
+
objs.each { |obj| list << self.new(obj) }
|
18
|
+
list
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.find(id)
|
22
|
+
response = action_by("read", id: id)
|
23
|
+
obj = JSON.parse(response.body)
|
24
|
+
self.new(obj)
|
25
|
+
end
|
26
|
+
|
27
|
+
def save
|
28
|
+
param_names = ["name", "email", "username", "password", "password_confirmation", "status", "product_ids"]
|
29
|
+
data = filter_params(param_names)
|
30
|
+
if data.length > 0
|
31
|
+
paramters = { user: data }
|
32
|
+
paramters.update({ id: @id }) if @id.to_i > 0
|
33
|
+
action = @id.to_i > 0 ? "update" : "add"
|
34
|
+
response = PlyomUser.action_by(action, paramters)
|
35
|
+
@id = response['id'] if action == "add"
|
36
|
+
response
|
37
|
+
else
|
38
|
+
nil
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def remove
|
43
|
+
PlyomUser.action_by("del", id: @id)
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
def filter_params(names=[])
|
48
|
+
params = {}
|
49
|
+
self.instance_variables.each do |var|
|
50
|
+
var_name = var.to_s.delete "@"
|
51
|
+
var_value = self.instance_variable_get var
|
52
|
+
params.update({var_name => var_value}) if names.include?(var_name)
|
53
|
+
end
|
54
|
+
params
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.action_by(action, params={})
|
58
|
+
auth_token = { "Authorization" => "Token token=\"#{ENV['plyom_user_token']}\"" }
|
59
|
+
|
60
|
+
case action
|
61
|
+
when "read"
|
62
|
+
HTTParty.get("#{uri}/#{params[:id]}", headers: auth_token)
|
63
|
+
when "add"
|
64
|
+
HTTParty.post("#{uri}", query: params, headers: auth_token)
|
65
|
+
when "del"
|
66
|
+
HTTParty.delete("#{uri}/#{params[:id]}", headers: auth_token)
|
67
|
+
when "update"
|
68
|
+
HTTParty.patch("#{uri}/#{params[:id]}", query: params, headers: auth_token) if params.size > 1
|
69
|
+
HTTParty.put("#{uri}/#{params[:id]}", query: params, headers: auth_token) if params.size == 1
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.uri
|
74
|
+
host = ENV["plyom_user_host"]
|
75
|
+
path = "/api/users"
|
76
|
+
host + path
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
class PlyomProduct
|
81
|
+
class_attribute :id, :name, :status, :token, :created_at, :updated_at
|
82
|
+
|
83
|
+
def initialize(params={})
|
84
|
+
params.each { |var, val| public_send("#{var}=", val) }
|
85
|
+
end
|
86
|
+
|
87
|
+
def self.all
|
88
|
+
list = []
|
89
|
+
response = action_by("read")
|
90
|
+
objs = JSON.parse(response.body)
|
91
|
+
objs.each { |obj| list << self.new(obj) }
|
92
|
+
list
|
93
|
+
end
|
94
|
+
|
95
|
+
def self.find(id)
|
96
|
+
response = action_by("read", id: id)
|
97
|
+
obj = JSON.parse(response.body)
|
98
|
+
self.new(obj)
|
99
|
+
end
|
100
|
+
|
101
|
+
def save
|
102
|
+
param_names = ["name", "status"]
|
103
|
+
data = filter_params(param_names)
|
104
|
+
if data.length > 0
|
105
|
+
paramters = { user: data }
|
106
|
+
paramters.update({ id: @id }) if @id.to_i > 0
|
107
|
+
action = @id.to_i > 0 ? "update" : "add"
|
108
|
+
response = PlyomUser.action_by(action, paramters)
|
109
|
+
@id = response['id'] if action == "add"
|
110
|
+
response
|
111
|
+
else
|
112
|
+
nil
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def remove
|
117
|
+
PlyomUser.action_by("del", id: @id)
|
118
|
+
end
|
119
|
+
|
120
|
+
private
|
121
|
+
def filter_params(names=[])
|
122
|
+
params = {}
|
123
|
+
self.instance_variables.each do |var|
|
124
|
+
var_name = var.to_s.delete "@"
|
125
|
+
var_value = self.instance_variable_get var
|
126
|
+
params.update({var_name => var_value}) if names.include?(var_name)
|
127
|
+
end
|
128
|
+
params
|
129
|
+
end
|
130
|
+
|
131
|
+
def self.action_by(action, params={})
|
132
|
+
auth_token = { "Authorization" => "Token token=\"#{ENV['plyom_user_token']}\"" }
|
133
|
+
|
134
|
+
case action
|
135
|
+
when "read"
|
136
|
+
HTTParty.get("#{uri}/#{params[:id]}", headers: auth_token)
|
137
|
+
when "add"
|
138
|
+
HTTParty.post("#{uri}", query: params, headers: auth_token)
|
139
|
+
when "del"
|
140
|
+
HTTParty.delete("#{uri}/#{params[:id]}", headers: auth_token)
|
141
|
+
when "update"
|
142
|
+
HTTParty.patch("#{uri}/#{params[:id]}", query: params, headers: auth_token) if params.size > 1
|
143
|
+
HTTParty.put("#{uri}/#{params[:id]}", query: params, headers: auth_token) if params.size == 1
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def self.uri
|
148
|
+
host = ENV["plyom_user_host"]
|
149
|
+
path = "/api/products"
|
150
|
+
host + path
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
class Authentication
|
155
|
+
class_attribute :id, :token, :validation
|
156
|
+
|
157
|
+
def initialize(params)
|
158
|
+
@params = params
|
159
|
+
end
|
160
|
+
|
161
|
+
def authenticated?(token)
|
162
|
+
auth_token = { "Authorization" => "Token token=\"#{token}\"" }
|
163
|
+
paramters = {username_email: @params[:username], password: @params[:password]}
|
164
|
+
response = HTTParty.get("#{self.uri}authentication", headers: auth_token, query: paramters)
|
165
|
+
result = JSON.parse(response.body)
|
166
|
+
if result["success"]
|
167
|
+
@token = result["token"]
|
168
|
+
@validation = 1
|
169
|
+
@id = result["id"]
|
170
|
+
true
|
171
|
+
else
|
172
|
+
false
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
def validation
|
177
|
+
@validation
|
178
|
+
end
|
179
|
+
|
180
|
+
def check_token(token)
|
181
|
+
paramters = {token: @token}
|
182
|
+
auth_token = { "Authorization" => "Token token=\"#{token}\"" }
|
183
|
+
response = HTTParty.get("#{self.uri}token_validation", headers: auth_token, query: paramters)
|
184
|
+
response.body
|
185
|
+
end
|
186
|
+
|
187
|
+
def self.validates(validation)
|
188
|
+
if validation == 1
|
189
|
+
true
|
190
|
+
else
|
191
|
+
false
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
def uri
|
196
|
+
host = ENV["plyom_user_host"]
|
197
|
+
path = "/api/"
|
198
|
+
host + path
|
199
|
+
end
|
200
|
+
|
201
|
+
end
|
202
|
+
end
|
data/lib/plyom_user/version.rb
CHANGED