Class: Warehaus::Getter

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

Constant Summary collapse

BASE_URI =
'3dwarehouse.sketchup.com'
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.



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

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

Class Method Details

.from_hash(hash) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/warehaus.rb', line 150

def self.from_hash(hash)
	hash[:models].each do |k, v|

		getter = self.new(v, "#{hash[:dir]}", k.to_s)
		getter.unbox
	end
end

Instance Method Details

#cleanupObject



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

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

#download_kmz(tries = 3, redirect = false) ⇒ Object



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
93
94
95
96
97
98
# File 'lib/warehaus.rb', line 66

def download_kmz(tries=3, redirect=false)
	return raise_error 'HTTP Redirect too deep' if tries == 0
	
	log("📥  Downloading KMZ file") unless redirect

	FileUtils::mkdir_p "#{@model_path}/.tmp"
	url = redirect ? URI.parse( redirect ) : URI.parse("http://" + BASE_URI + "#{KMZ_PATH}?contentId=#{@kmz_options[:query][:contentId]}&fn=#{@kmz_options[:query][:fn]}")
	
	begin
		resp = Net::HTTP.get_response(url)
	rescue Exception => e
		return raise_error e.message
	end

		
	case resp
	    when Net::HTTPSuccess then

    		File.open("#{@model_path}/.tmp/#{@name}.kmz", "wb") do |file|
        
        		file.write(resp.body)
        
        	end
    	
    		FileUtils.mv "#{@model_path}/.tmp/#{@name}.kmz", "#{@model_path}/.tmp/#{@name}.zip"
        
        when Net::HTTPRedirection then
   			download_kmz( tries-1, resp['location'])
   		else raise_error("Response neither successful nor redirecting")
    end
	

end

#eraseObject



138
139
140
# File 'lib/warehaus.rb', line 138

def erase
	FileUtils.rm_rf "#{@model_path}"
end

#fetch_entityObject



35
36
37
38
# File 'lib/warehaus.rb', line 35

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



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

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



146
147
148
# File 'lib/warehaus.rb', line 146

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

#parse_input(input) ⇒ Object



31
32
33
# File 'lib/warehaus.rb', line 31

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

#raise_error(msg) ⇒ Object



142
143
144
# File 'lib/warehaus.rb', line 142

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

#unboxObject



129
130
131
132
133
134
135
136
# File 'lib/warehaus.rb', line 129

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

#unzip_kmzObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/warehaus.rb', line 100

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

	log("🎊  Cracking open the ZIP file")
	Zip::File.open("#{@model_path}/.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 = "#{@model_path}/#{@name}.dae"
			else
				dest = "#{@model_path}/#{entry.name.match(/models\/(.*)$/)[1]}"
			end

			entry.extract(dest)
		end
	end
end