Class: ReferenceDocument

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/reference_document.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) actual_url



36
37
38
# File 'app/models/reference_document.rb', line 36

def actual_url
  hosted_by_us? ? s3_url : url
end

- (Object) add_protocol_to_url



58
59
60
# File 'app/models/reference_document.rb', line 58

def add_protocol_to_url
  self.url = "http://" + url if url.present? && url !~ %r{^http://}
end

- (Object) check_url



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/reference_document.rb', line 40

def check_url
  return if file_file_name.present? or url.blank?
  # this is to avoid authentication problems when a URL to one of "our" files is copied
  # to another reference (e.g., nested)
  return if url =~ /antcat/
  return if hosted_by_hol?
  # a URL with spaces is valid, but URI.parse rejects it
  uri = URI.parse url.gsub(/ /, '%20')
  response_code = Net::HTTP.new(uri.host, 80).request_head(uri.path).code.to_i
  errors.add :url, 'was not found' unless (200..399).include? response_code
rescue SocketError, URI::InvalidURIError, ArgumentError
  errors.add :url, 'is not in a valid format'
end

- (Boolean) downloadable_by?(user)

Returns:

  • (Boolean)


28
29
30
# File 'app/models/reference_document.rb', line 28

def downloadable_by? user
  url.present? && !hosted_by_hol? && (attributes["public"] || !hosted_by_us? || user.present?)
end

- (Object) host=(host)



23
24
25
26
# File 'app/models/reference_document.rb', line 23

def host= host
  return unless hosted_by_us?
  update_attribute :url, "http://#{host}/documents/#{id}/#{file_file_name}"
end

- (Boolean) hosted_by_hol?

Returns:

  • (Boolean)


32
33
34
# File 'app/models/reference_document.rb', line 32

def hosted_by_hol?
  url.present? && url =~ %r{^http://128.146.250.117}
end

- (Boolean) hosted_by_us?

Returns:

  • (Boolean)


54
55
56
# File 'app/models/reference_document.rb', line 54

def hosted_by_us?
  file_file_name.present?
end

- (Object) s3_url



62
63
64
# File 'app/models/reference_document.rb', line 62

def s3_url
  AWS::S3::S3Object.url_for file.path, file.bucket_name, :expires_in => 10
end

- (Object) transliterate_file_name



17
18
19
20
21
# File 'app/models/reference_document.rb', line 17

def transliterate_file_name
  extension = File.extname(file_file_name).gsub(/^\.+/, '')
  filename = file_file_name.gsub(/\.#{extension}$/, '')
  file.instance_write(:file_name, "#{ActiveSupport::Inflector.parameterize(filename)}.#{ActiveSupport::Inflector.parameterize(extension)}")
end