Class: Marmot::Client
- Inherits:
-
Object
- Object
- Marmot::Client
- Includes:
- HTTMultiParty
- Defined in:
- lib/marmot/client.rb
Constant Summary collapse
- @@headers_set =
false
Instance Attribute Summary collapse
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Instance Method Summary collapse
-
#convert(input_io, options = {}) ⇒ String
Convert a font file to a webfont kit.
-
#initialize ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
14 15 16 17 |
# File 'lib/marmot/client.rb', line 14 def initialize logger = Logger.new("/dev/null") logger.close end |
Instance Attribute Details
#logger ⇒ Object
Returns the value of attribute logger.
10 11 12 |
# File 'lib/marmot/client.rb', line 10 def logger @logger end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
11 12 13 |
# File 'lib/marmot/client.rb', line 11 def params @params end |
Instance Method Details
#convert(input_io, options = {}) ⇒ String
Convert a font file to a webfont kit
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/marmot/client.rb', line 137 def convert input_io, ={} @exception = nil #1 iam "Retrieving cookies... ", false do response = self.class.get '/tools/webfont-generator' @cookies = (response.headers.get_fields("Set-Cookie") || []).join(";") fail "Failed to retrieve cookies" if @cookies.empty? self.class.headers({"Cookie" => @cookies}) @@headers_set = true response end unless @@headers_set #2 iam "Uploading font... " do response = self.class.post '/uploadify/fontfacegen_uploadify.php', :query => { "Filedata" => File.new(input_io) } @path_data = response.body @id, @original_filename = @path_data.split("|") fail "Failed to upload the file. Is it a valid font?" if @id.nil? || @original_filename.nil? response end #3 iam "Confirming upload... " do response = self.class.post "/tools/insert", :query => { "original_filename" => @original_filename, "path_data" => @path_data } json = JSON.parse response.body fail (json["message"] || "Failed to confirm the upload. Is it a valid font?") if !json["name"] || !json["id"] response end #4 iam "Generating webfont... " do = .delete :custom_options [:id] = @id @params = OptionsSanitizer.sanitize(, ) logger.debug "HTTP Params:\n#{@params.collect{|k,v| "#{k}: #{v.inspect}" }.join("\n")}" response = self.class.post "/tools/generate", :query => @params fail "Failed to generate webfont kit" if !response.body.empty? response end #5 counter = 0 while response = self.class.get("/tools/progress/#{@id}") do p = JSON.parse(response.body)["progress"].to_i logger.info "Progress: #{p} " if p == 100 break elsif p == 0 fail "Progress timeout" if (counter += 1) > 10 end sleep 2 end #6 iam "Downloading fonts... ", false do response = self.class.post "/tools/download", :query => @params end #7 if ![:output_io] filename = response.headers["Content-Disposition"].gsub(/attachment; filename=\"(.*)\"/,'\1') [:output_io] = File.new(filename, "wb") end [:output_io] << response.response.body end |