Class: Wikidata::Property::CommonsMedia

Inherits:
Base
  • Object
show all
Defined in:
lib/wikidata/property/commons_media.rb

Constant Summary collapse

BASE_PAGE_URL =
"https://commons.wikimedia.org/wiki/File:%s.%s".freeze
IMAGE_URL =
"https://upload.wikimedia.org/wikipedia/commons/%s/%s/%s.%s"
THUMB_IMAGE_URL =
"https://upload.wikimedia.org/wikipedia/commons/thumb/%s/%s/%s.%s/%ipx-%s.%s"

Instance Attribute Summary

Attributes inherited from Base

#property

Instance Method Summary collapse

Methods inherited from Base

#initialize, #value

Constructor Details

This class inherits a constructor from Wikidata::Property::Base

Instance Method Details

#basenameObject



24
25
26
# File 'lib/wikidata/property/commons_media.rb', line 24

def basename
  @_basename ||= name.gsub ' ', '_'
end

#extensionObject



36
37
38
# File 'lib/wikidata/property/commons_media.rb', line 36

def extension
  @_ext ||= File.extname(value).tr '.', ''
end

#md5Object



20
21
22
# File 'lib/wikidata/property/commons_media.rb', line 20

def md5
  @_md5 ||= Digest::MD5.hexdigest "#{basename}.#{extension}"
end

#nameObject



28
29
30
# File 'lib/wikidata/property/commons_media.rb', line 28

def name
  @_name ||= File.basename(value, ".#{extension}")
end

#page_urlObject



8
9
10
# File 'lib/wikidata/property/commons_media.rb', line 8

def page_url
  @_page_url ||= sprintf BASE_PAGE_URL, basename, extension
end

#thumb_extensionObject



32
33
34
# File 'lib/wikidata/property/commons_media.rb', line 32

def thumb_extension
  @_thumb_ext ||= extension == 'svg' ? 'svg.png' : extension
end

#url(size = nil) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/wikidata/property/commons_media.rb', line 12

def url size = nil
  if size
    sprintf THUMB_IMAGE_URL, md5[0], md5[0..1], basename, extension, size, basename, extension, thumb_extension
  else
    sprintf IMAGE_URL, md5[0], md5[0..1], basename, extension
  end
end