Class: Tinify::Source
- Inherits:
-
Object
- Object
- Tinify::Source
- Defined in:
- lib/tinify/source.rb
Class Method Summary collapse
Instance Method Summary collapse
- #convert(options) ⇒ Object
-
#initialize(url, commands = {}) ⇒ Source
constructor
A new instance of Source.
- #preserve(*options) ⇒ Object
- #resize(options) ⇒ Object
- #result ⇒ Object
- #store(options) ⇒ Object
- #to_buffer ⇒ Object
- #to_file(path) ⇒ Object
- #transform(options) ⇒ Object
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 |
Instance Method Details
#convert(options) ⇒ Object
32 33 34 |
# File 'lib/tinify/source.rb', line 32 def convert() self.class.new(@url, @commands.merge(convert: )) end |
#preserve(*options) ⇒ Object
23 24 25 26 |
# File 'lib/tinify/source.rb', line 23 def preserve(*) = Array().flatten self.class.new(@url, @commands.merge(preserve: )) end |
#resize(options) ⇒ Object
28 29 30 |
# File 'lib/tinify/source.rb', line 28 def resize() self.class.new(@url, @commands.merge(resize: )) 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() response = Tinify.client.request(:post, @url, @commands.merge(store: )) 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() self.class.new(@url, @commands.merge(transform: )) end |