Class: QiitaExport::Image
- Inherits:
-
Object
- Object
- QiitaExport::Image
- Defined in:
- lib/qiita-export/image.rb
Constant Summary collapse
- IMAGE_DIR =
"images"
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
Instance Method Summary collapse
- #filename ⇒ Object
-
#initialize(key, url) ⇒ Image
constructor
A new instance of Image.
- #request_header ⇒ Object
- #require_auth? ⇒ Boolean
- #save(path) ⇒ Object
Constructor Details
#initialize(key, url) ⇒ Image
Returns a new instance of Image.
11 12 13 14 |
# File 'lib/qiita-export/image.rb', line 11 def initialize(key, url) @key = key @url = url end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
7 8 9 |
# File 'lib/qiita-export/image.rb', line 7 def key @key end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
7 8 9 |
# File 'lib/qiita-export/image.rb', line 7 def url @url end |
Class Method Details
.extract_urls(key, markdown) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/qiita-export/image.rb', line 16 def self.extract_urls(key, markdown) image_urls = markdown.scan(/!\[.*?\]\((.+?)(?: \".*?\")?\)/).flatten image_urls.map do |image_url| new(key, image_url) end end |
Instance Method Details
#filename ⇒ Object
31 32 33 |
# File 'lib/qiita-export/image.rb', line 31 def filename "#{File.basename(url)}" end |
#request_header ⇒ Object
27 28 29 |
# File 'lib/qiita-export/image.rb', line 27 def request_header require_auth? ? Config.auth_header : Config.default_header end |
#require_auth? ⇒ Boolean
23 24 25 |
# File 'lib/qiita-export/image.rb', line 23 def require_auth? @url !~ /qiita-image-store.s3.amazonaws.com/ end |
#save(path) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/qiita-export/image.rb', line 35 def save(path) image_dir = File.join(File.(path), IMAGE_DIR) FileUtils.makedirs(image_dir) unless Dir.exists?(image_dir) file_path = File.join(image_dir, filename) open(file_path, 'wb') do |out| open(@url, request_header) do |image| out.write(image.read) end end end |