Class: ReleaseImage::Generator
- Inherits:
-
Object
- Object
- ReleaseImage::Generator
- Defined in:
- lib/release_image.rb
Constant Summary collapse
- BASE_URL =
"https://api.unsplash.com/photos/random"
Instance Method Summary collapse
-
#add_background_and_text(image) ⇒ Object
rubocop:disable Metrics/MethodLength.
- #add_logo(image) ⇒ Object
- #api_key ⇒ Object
- #create_folder ⇒ Object
- #create_release_image ⇒ Object
- #download_image ⇒ Object
- #generate ⇒ Object
- #image_path ⇒ Object
-
#initialize(version:, date: nil, skip_download: false) ⇒ Generator
constructor
A new instance of Generator.
- #query ⇒ Object
- #release_image_path ⇒ Object
- #resize_image(image) ⇒ Object
- #season ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(version:, date: nil, skip_download: false) ⇒ Generator
Returns a new instance of Generator.
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/release_image.rb', line 28 def initialize(version:, date: nil, skip_download: false) @version = version @date = date ? Date.parse(date) : Date.today @skip_download = skip_download @folder_path = ReleaseImage.config.folder_path @logo_path = ReleaseImage.config.logo_path @keywords = ReleaseImage.config.keywords @api_key = ReleaseImage.config.api_key @seasons = ReleaseImage.config.seasons end |
Instance Method Details
#add_background_and_text(image) ⇒ Object
rubocop:disable Metrics/MethodLength
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/release_image.rb', line 84 def add_background_and_text(image) # rubocop:disable Metrics/MethodLength image. do |cmd| cmd.geometry "+0+0" cmd.gravity "center" cmd.fill "rgba(0, 0, 0, 0.5)" cmd.draw "rectangle 0,0 #{image.width},#{image.height}" cmd.fill "white" cmd.font "Helvetica" cmd.pointsize 80 cmd.draw "text 0, -100 'v#{@version}'" cmd.pointsize 60 cmd.draw "text 0, -10 '#{@date.strftime("%d. %B %Y.")}'" end end |
#add_logo(image) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/release_image.rb', line 103 def add_logo(image) logo = MiniMagick::Image.open(@logo_path) image.composite(logo) do |c| c.geometry "+#{(image.width / 2) - (logo.width / 2)}+300" end end |
#api_key ⇒ Object
134 135 136 |
# File 'lib/release_image.rb', line 134 def api_key ReleaseImage.config.api_key end |
#create_folder ⇒ Object
47 48 49 |
# File 'lib/release_image.rb', line 47 def create_folder FileUtils.mkdir_p(@folder_path) unless File.directory?(@folder_path) end |
#create_release_image ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/release_image.rb', line 61 def create_release_image image = MiniMagick::Image.open(image_path) resize_image(image) add_background_and_text(image) result = add_logo(image) result.write release_image_path release_image_path end |
#download_image ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/release_image.rb', line 51 def download_image uri = URI(url) response = Net::HTTP.get_response(uri) data = JSON.parse(response.body) image_url = data["urls"]["regular"] image = Down.download(image_url) File.binwrite(image_path, image.read) end |
#generate ⇒ Object
41 42 43 44 45 |
# File 'lib/release_image.rb', line 41 def generate create_folder download_image unless @skip_download create_release_image end |
#image_path ⇒ Object
111 112 113 |
# File 'lib/release_image.rb', line 111 def image_path "#{@folder_path}/image.jpg" end |
#query ⇒ Object
119 120 121 122 123 |
# File 'lib/release_image.rb', line 119 def query result = @keywords result += [season] if @seasons result.join(" ") end |
#release_image_path ⇒ Object
73 74 75 76 |
# File 'lib/release_image.rb', line 73 def release_image_path underscored_version = @version.tr(".", "_") "#{@folder_path}/release_#{underscored_version}.png" end |
#resize_image(image) ⇒ Object
78 79 80 81 82 |
# File 'lib/release_image.rb', line 78 def resize_image(image) image.resize "1920x1200^" image.gravity "center" image.extent "1920x1200" end |
#season ⇒ Object
125 126 127 128 129 130 131 132 |
# File 'lib/release_image.rb', line 125 def season case @date.strftime("%m").to_i when 3..5 then "Spring" when 6..8 then "Summer" when 9..11 then "Autumn" else "Winter" end end |
#url ⇒ Object
115 116 117 |
# File 'lib/release_image.rb', line 115 def url "#{BASE_URL}?query=#{query}&client_id=#{@api_key}" end |