Class: Warehaus::Getter

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/warehaus.rb

Constant Summary collapse

ENTITY_PATH =
'/3dw/GetEntity'
KMZ_PATH =
'/warehouse/getpubliccontent'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url_or_id, dir = './', name = 'warehouse-model') ⇒ Getter

Returns a new instance of Getter.



19
20
21
22
23
24
25
26
27
# File 'lib/warehaus.rb', line 19

def initialize(url_or_id, dir='./', name='warehouse-model')
	@name = name
	@path = dir
	@options = {
		:query => {
			:id => parse_input(url_or_id)
		}
	}
end

Class Method Details

.from_hash(hash) ⇒ Object



134
135
136
137
138
139
# File 'lib/warehaus.rb', line 134

def self.from_hash(hash)
	hash[:models].each do |k, v|
		getter = self.new(v, "#{hash[:dir]}/#{k}", k)
		getter.unbox
	end
end

Instance Method Details

#cleanupObject



106
107
108
109
# File 'lib/warehaus.rb', line 106

def cleanup
	log("🛀  Cleaning up")
	FileUtils.rm_rf("#{@path}")
end

#download_kmzObject



64
65
66
67
# File 'lib/warehaus.rb', line 64

def download_kmz
	log("📥  Downloading KMZ file")
	@kmz = self.class.get(KMZ_PATH, @kmz_options).parsed_response
end

#eraseObject



122
123
124
# File 'lib/warehaus.rb', line 122

def erase
	FileUtils.rm_rf "#{@path}/#{@name}"
end

#fetch_entityObject



33
34
35
36
# File 'lib/warehaus.rb', line 33

def fetch_entity
	log("📠  Fetching JSON representation of model")
	@response = JSON.parse(self.class.get(ENTITY_PATH, @options), :symbolize_names => true)
end

#get_kmz_idObject



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
# File 'lib/warehaus.rb', line 38

def get_kmz_id
	log("🔍  Checking for KMZ id in JSON")

	if !@response[:binaries] 
		return log("No KMZ file available for this object 💣")
	end
		
	if @response[:binaries][:ks]
		kmz_id = @response[:binaries][:ks][:id]
	else
		@response[:binaries].each{ |k, bin|
			kmz_id = bin[:id] if (bin[:originalFileName] =~ /kmz$/) != nil
		}
		if !kmz_id
			return log("No KMZ file available for this object 💣")
		end
	end

	@kmz_options = {
		:query =>{
			:contentId => kmz_id,
			:fn => "#{@name}.kmz"
		}
	}
end

#log(msg) ⇒ Object



130
131
132
# File 'lib/warehaus.rb', line 130

def log(msg)
	puts("Warehaus: #{msg}") if $mode == "verbose"
end

#parse_input(input) ⇒ Object



29
30
31
# File 'lib/warehaus.rb', line 29

def parse_input(input)
	(input =~ /^[\w|\-|\d]+$/) ? input : input.match(/id=([\w|\-|\d]+)/)[1]
end

#raise_error(msg) ⇒ Object



126
127
128
# File 'lib/warehaus.rb', line 126

def raise_error(msg)
	raise "Warehaus Error: #{msg}"
end

#unboxObject



111
112
113
114
115
116
117
118
119
120
# File 'lib/warehaus.rb', line 111

def unbox
	log("📦  Beginning unbox!")
	fetch_entity
	get_kmz_id
	download_kmz
	write_kmz
	unzip_kmz
	cleanup
	"#{@path}/#{@name}/"
end

#unzip_kmzObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/warehaus.rb', line 82

def unzip_kmz
	log("📁  Creating model directory")
	FileUtils::mkdir_p "#{@path}/#{@name}/untitled"

	log("🎊  Cracking open the ZIP file")
	Zip::File.open("#{@path}/#{@name}/.tmp/#{@name}.zip") do |zip_file|
		
		valid_paths = zip_file.entries.select do |entry|
			(entry.name =~ /models\//) != nil
		end
		
		valid_paths.each do |entry|

			if entry.name =~ /\.dae$/
				dest = "#{@path}/#{@name}/#{@name}.dae"
			else
				dest = "#{@path}/#{@name}/#{entry.name.match(/models\/(.*)$/)[1]}"
			end

			entry.extract(dest)
		end
	end
end

#write_kmzObject



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/warehaus.rb', line 69

def write_kmz
	FileUtils::mkdir_p "#{@path}/#{@name}/.tmp"
	log("📝  Writing KMZ file")
	begin
		File.open("#{@path}/#{@name}/.tmp/#{@name}.kmz", "wb") do |f|
			f.write @kmz
		end
		FileUtils::mv "#{@path}/#{@name}/.tmp/#{@name}.kmz", "#{@path}/#{@name}/.tmp/#{@name}.zip"
	rescue Exception => e
		return raise_error e.message
	end
end