Class: Tinify::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/tinify/source.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, commands = {}) ⇒ Source

Returns a new instance of Source.



19
20
21
# File 'lib/tinify/source.rb', line 19

def initialize(url, commands = {})
  @url, @commands = url.freeze, commands.freeze
end

Class Method Details

.from_buffer(string) ⇒ Object



8
9
10
11
# File 'lib/tinify/source.rb', line 8

def from_buffer(string)
  response = Tinify.client.request(:post, "/shrink", string)
  new(response.headers["Location"]).freeze
end

.from_file(path) ⇒ Object



4
5
6
# File 'lib/tinify/source.rb', line 4

def from_file(path)
  from_buffer(File.open(path, "rb") { |file| file.read })
end

.from_url(url) ⇒ Object



13
14
15
16
# File 'lib/tinify/source.rb', line 13

def from_url(url)
  response = Tinify.client.request(:post, "/shrink", source: { url: url })
  new(response.headers["Location"]).freeze
end

Instance Method Details

#convert(options) ⇒ Object



32
33
34
# File 'lib/tinify/source.rb', line 32

def convert(options)
  self.class.new(@url, @commands.merge(convert: options))
end

#preserve(*options) ⇒ Object



23
24
25
26
# File 'lib/tinify/source.rb', line 23

def preserve(*options)
  options = Array(options).flatten
  self.class.new(@url, @commands.merge(preserve: options))
end

#resize(options) ⇒ Object



28
29
30
# File 'lib/tinify/source.rb', line 28

def resize(options)
  self.class.new(@url, @commands.merge(resize: options))
end

#result ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/tinify/source.rb', line 45

def result
  response = if @commands&.empty?
                Tinify.client.request(:get, @url)
              else
                Tinify.client.request(:post, @url, @commands)
              end

  Result.new(response.headers, response.body).freeze
end

#store(options) ⇒ Object



40
41
42
43
# File 'lib/tinify/source.rb', line 40

def store(options)
  response = Tinify.client.request(:post, @url, @commands.merge(store: options))
  ResultMeta.new(response.headers).freeze
end

#to_buffer ⇒ Object



59
60
61
# File 'lib/tinify/source.rb', line 59

def to_buffer
  result.to_buffer
end

#to_file(path) ⇒ Object



55
56
57
# File 'lib/tinify/source.rb', line 55

def to_file(path)
  result.to_file(path)
end

#transform(options) ⇒ Object



36
37
38
# File 'lib/tinify/source.rb', line 36

def transform(options)
  self.class.new(@url, @commands.merge(transform: options))
end