Class: Document
- Inherits:
-
MLS::Model
- Object
- ActiveRecord::Base
- MLS::Model
- Document
- Defined in:
- lib/mls/models/document.rb
Class Method Summary collapse
Instance Method Summary collapse
- #aspect_ratio ⇒ Object
- #height ⇒ Object
- #partition(value) ⇒ Object
- #path(style = :original) ⇒ Object
- #url(style = :original) ⇒ Object
- #width ⇒ Object
Class Method Details
.create(file) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/mls/models/document.rb', line 6 def self.create(file) if doc = find_matching(file) doc else data, headers = Multipart::Post.prepare_query("document[file]" => file) req = Net::HTTP::Post.new("/documents") req.body = data req['Content-Type'] = headers['Content-Type'] res = Document.connection.instance_variable_get(:@connection).send_request(req) instantiate(JSON.parse(res.body).select{|k,v| column_names.include?(k.to_s) }) end end |
.find_matching(file) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/mls/models/document.rb', line 52 def self.find_matching(file) filename = file.original_filename if file.respond_to?(:original_filename) filename ||= File.basename(file.path) # If we can tell the possible mime-type from the filename, use the # first in the list; otherwise, use "application/octet-stream" content_type = file.content_type if file.respond_to?(:content_type) content_type ||= (MIME::Types.type_for(filename)[0] || MIME::Types["application/octet-stream"][0]).simplified matching_docs = Document.where(:filename => filename, :content_type => content_type, :size => file.size) if matching_docs.count > 0 matching_docs = matching_docs.where(:md5 => Digest::MD5.file(file.path).hexdigest) end matching_docs.first end |
Instance Method Details
#aspect_ratio ⇒ Object
47 48 49 50 |
# File 'lib/mls/models/document.rb', line 47 def aspect_ratio return nil if !width || !height return width.to_f / height.to_f end |
#height ⇒ Object
42 43 44 45 |
# File 'lib/mls/models/document.rb', line 42 def height return nil if !dimensions return dimensions.split('x')[1].to_i end |
#partition(value) ⇒ Object
32 33 34 35 |
# File 'lib/mls/models/document.rb', line 32 def partition(value) split = value.scan(/.{1,4}/) split.shift(4).join("/") + split.join("") end |
#path(style = :original) ⇒ Object
28 29 30 |
# File 'lib/mls/models/document.rb', line 28 def path(style=:original) File.join("documents", "#{partition(style == :original ? hash_key : "#{hash_key}-#{style}")}") end |
#url(style = :original) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/mls/models/document.rb', line 21 def url(style=:original) URI::HTTPS.build( host: MLS.config['document_host'].gsub(/\/$/, ''), path: path(style) ) end |
#width ⇒ Object
37 38 39 40 |
# File 'lib/mls/models/document.rb', line 37 def width return nil if !dimensions return dimensions.split('x')[0].to_i end |