Class: Umbreo::Models::Blueprint

Inherits:
Object
  • Object
show all
Defined in:
lib/umbreo/models/blueprint.rb

Constant Summary collapse

OPERATING_SYSTEM =
%w(linux windows)

Instance Method Summary collapse

Constructor Details

#initialize(credentials = {}, name_or_id = nil, status = false) ⇒ Blueprint

Returns a new instance of Blueprint.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/umbreo/models/blueprint.rb', line 7

def initialize(credentials = {}, name_or_id = nil, status = false)
	if credentials.present?
		@email    = credentials[:email]
		@api_key  = credentials[:api_key]
		@endpoint = credentials[:end_point] || SERVER_END_POINT
	end

	@name_or_id = name_or_id
	@status     = status
	@errors     = []
end

Instance Method Details

#all(filter = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/umbreo/models/blueprint.rb', line 40

def all(filter = {})
	@errors << "Please choose correct one of os |#{OPERATING_SYSTEM.join(", ")}|" if OPERATING_SYSTEM.exclude?(filter[:os].to_s) && filter[:os].present?
	@errors << "Search keyword is required." if filter[:keyword].blank? && filter[:search]
	
	if valid?
		retrieve_data(filter)

   	if @data['roles'].present?
   		page   = " | Page: #{filter[:page]} of #{@data['total_page']} pages"
   		header =  if @status
				    			"List My Custom Blueprint#{ page if @data['total_page'] > 1 }"
				    		else
				    			"List Blueprint#{ page if @data['total_page'] > 1 }"
				    		end

        Helpers::Table.show_table(@data['roles'], header, ['ID', 'Name', 'Slug', 'OS', 'Description'])
   	else
   		Helpers::AlertMessage.show_error_message(@data["message"])
   	end
	else
		Helpers::AlertMessage.show_error_message(error)
	end
end

#create!(attributes = {}) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/umbreo/models/blueprint.rb', line 112

def create!(attributes = {})
	@create_name        = attributes[:name]
	@create_description = attributes[:description]
	@create_os          = attributes[:os]
	@create_system      = attributes[:system]
	@create_profiles    = attributes[:profiles]

	create_validation!

	if valid?
		Helpers::ErrorException.rescue do
			data = Typhoeus.post(
            "#{@endpoint}/api/v1/blueprints",
            body: {
              authenticate_token:   @api_key,
              email:                @email,
              user_role: {           
                name:               @create_name,
                description:        @create_description,
                os:                 @create_os,
                umbreo_profile_ids: @create_profiles
              }
            }
          )

			@data = JSON.parse data.response_body

			if @data["success"]
				Helpers::AlertMessage.show_success_message(@data['message'])
			else
				Helpers::AlertMessage.show_error_message(@data['message'])
			end
		end
	else
		Helpers::AlertMessage.show_error_message(error)
	end
end

#create_validation!Object



150
151
152
153
154
155
# File 'lib/umbreo/models/blueprint.rb', line 150

def create_validation!
	@errors << "Name of blueprint is required" if @create_name.blank?
	@errors << "Operating system is required" if @create_os.blank?
	@errors << "System is required" if @create_system.blank?
	@errors << "Profiles is required" if @create_profiles.blank?
end

#deleteObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/umbreo/models/blueprint.rb', line 94

def delete
	data = Typhoeus.delete(
       "#{@endpoint}/api/v1/blueprints/#{@name_or_id}",
       body: {
         authenticate_token: @api_key,
         email:              @email
       }
     )

     @data = JSON.parse data.response_body

     if @data["success"]
     	Helpers::AlertMessage.show_success_message(@data["message"])
     else
     	Helpers::AlertMessage.show_error_message(@data["message"])
     end
end

#errorObject



231
232
233
# File 'lib/umbreo/models/blueprint.rb', line 231

def error
	@errors.first
end

#exportObject

get json files params of blueprint



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/umbreo/models/blueprint.rb', line 160

def export
	@errors << "ID or Slug is required." if @name_or_id.blank?

	if valid?
		Helpers::ErrorException.rescue do
			data = Typhoeus.get(
           "#{@endpoint}/api/v1/blueprints/export",
           body: {
             authenticate_token:   @api_key,
             email:                @email,
             id:                   @name_or_id,
             status:               @status
           }
         )

       @data = JSON.parse data.response_body rescue nil
       role = @data["user_role"] || @data["umbreo_role"]

			if role.present?
       	name_file = role["name"].downcase.titleize.delete(" ").underscore
       	Helpers::FileGenerator.create(name_file, role)
         Helpers::AlertMessage.show_success_message("Success export blueprint. your blueprint is saved on json file with name #{name_file}.json")
       else
       	Helpers::AlertMessage.show_error_message(@data["message"])
       end
		end
	else
		Helpers::AlertMessage.show_error_message(error)
	end
end

#retrieve_data(filter = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/umbreo/models/blueprint.rb', line 19

def retrieve_data(filter = {})
	Helpers::ErrorException.rescue do
		url  =  if @status
							"#{@endpoint}/api/v1/blueprints/custom"
						else
							"#{@endpoint}/api/v1/blueprints"
						end

		data = Typhoeus.get(
			          url,
			          body: {
			            authenticate_token: @api_key,
			            email:              @email,
			            filter:             filter
			          }
			        )

   	@data = JSON.parse data.response_body
	end
end

#showObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/umbreo/models/blueprint.rb', line 64

def show
     Helpers::ErrorException.rescue do
   		url = if @status
						"#{@endpoint}/api/v1/blueprints/show_custom"
					else
						"#{@endpoint}/api/v1/blueprints/show"
					end

		response = Typhoeus.get(
        url,
        body: {
          authenticate_token: @api_key,
          email:              @email,
          id:                 @name_or_id
        }
      )

      response = JSON.parse response.response_body rescue nil

       if response['success']
       	Helpers::AlertMessage.(response['id']) { "ID: #message" }
       	Helpers::AlertMessage.(response['name']) { "Name: #message" }
       	Helpers::AlertMessage.(response['description']) { "Description: #message" }
       	Helpers::AlertMessage.(response['os']) { "OS: #message" }
       else
       	Helpers::AlertMessage.show_error_message(response['message'])
       end
     end
end

#valid?Boolean

Returns:

  • (Boolean)


235
236
237
# File 'lib/umbreo/models/blueprint.rb', line 235

def valid?
	@errors.blank?
end

#validate(file_path) ⇒ Object

check valid json params



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/umbreo/models/blueprint.rb', line 194

def validate(file_path)
	@errors << "File directory is required." if file_path.blank?
	begin
		file    = File.open(file_path, "r")
		content = file.read
      json    = JSON.parse(content)
      encode  = Helpers::JsonBaseConvert.encode(json)
	rescue
		@errors << "Error on file. Please check again."
	end
	@errors << "Content of file is required." if content.blank?

	if valid?
		Helpers::ErrorException.rescue do

        response = Typhoeus.get(
          "#{@endpoint}/api/v1/blueprints/validate",
          body: {
            authenticate_token: @api_key,
            email:              @email,
            content:            encode
          }
        )

        @response = JSON.parse response.response_body

        if @response["success"]
				Helpers::AlertMessage.show_success_message(@response["message"])
        else
				Helpers::AlertMessage.show_error_message(@response["message"])
        end
		end
	else
		Helpers::AlertMessage.show_error_message(error)
	end
end