Class: Fastlane::Helper::TinifymeHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/tinifyme/helper/tinifyme_helper.rb

Instance Method Summary collapse

Instance Method Details

#add_to_commit(images) ⇒ Object



47
48
49
50
# File 'lib/fastlane/plugin/tinifyme/helper/tinifyme_helper.rb', line 47

def add_to_commit(images)
  images_text = images.join(" ")
  system("git add #{images_text}")
end

#compress!(images) ⇒ Object



43
44
45
# File 'lib/fastlane/plugin/tinifyme/helper/tinifyme_helper.rb', line 43

def compress!(images)
  images.each { |image| Tinify.from_file(image).to_file(image) }
end

#format_output(text, is_step: false) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/fastlane/plugin/tinifyme/helper/tinifyme_helper.rb', line 30

def format_output(text, is_step: false)
  if is_step
    return " > #{text}"
  else
    return "   #{text}"
  end
end

#get_modified_images(image_extensions) ⇒ Object



38
39
40
41
# File 'lib/fastlane/plugin/tinifyme/helper/tinifyme_helper.rb', line 38

def get_modified_images(image_extensions)
  added_or_modified = `git diff --name-only --cached --diff-filter=d`
  added_or_modified.split("\n").select { |file| file.downcase.end_with?(*image_extensions) }
end

#has_connection?Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fastlane/plugin/tinifyme/helper/tinifyme_helper.rb', line 9

def has_connection?
  require 'net/http'
  url = URI('https://www.google.com')
  begin
    http = Net::HTTP.start(url.host, url.port, use_ssl: true)
    http.finish
    return true
  rescue SocketError
    return false
  end
end

#validate_credentials!(api_key) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/fastlane/plugin/tinifyme/helper/tinifyme_helper.rb', line 21

def validate_credentials!(api_key)
  UI.message(self.format_output('Checking TinyPNG credentials...', is_step: true))
  Tinify.key = api_key
  Tinify.validate!
  UI.success(self.format_output('Valid credentials!'))
rescue Tinify::Error => e
  UI.abort_with_message!(e)
end