shortpixel 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rubocop.yml +13 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +15 -0
- data/Gemfile.lock +118 -0
- data/LICENSE.txt +21 -0
- data/README.md +68 -0
- data/Rakefile +18 -0
- data/bin/console +24 -0
- data/bin/setup +8 -0
- data/lib/shortpixel/client.rb +44 -0
- data/lib/shortpixel/error.rb +3 -0
- data/lib/shortpixel/object.rb +20 -0
- data/lib/shortpixel/objects/post_reducer.rb +5 -0
- data/lib/shortpixel/objects/reducer.rb +4 -0
- data/lib/shortpixel/resource.rb +122 -0
- data/lib/shortpixel/resources/post_reducer.rb +11 -0
- data/lib/shortpixel/resources/reducer.rb +17 -0
- data/lib/shortpixel/version.rb +5 -0
- data/lib/shortpixel.rb +16 -0
- data/short_pixel-0.1.0.gem +0 -0
- data/short_pixel-0.1.1.gem +0 -0
- metadata +108 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c39a8e8c81bca8089085e8670f44bee393f493dac88d53e7170c406214d94998
|
4
|
+
data.tar.gz: 9423d5e8c449eac5b6daa815516019261bc24c423c7a6946f802002fff70ed41
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5ba2b98f0d013ec29ae91b1e1e793df6d10be47c345efbfe000eb2e8d6d52b2616159b68fbff903ea10b5234be64b91f5b6c7a3a8f68f2eb41187be412a49c73
|
7
|
+
data.tar.gz: f3abb0d9cb76724cbee27c96c32f7a68720aabbeb186b8f94f747a6a79486ee4d24d873d594fa1a5c5cdf4ddf54d08dc1730835fa80c7cb94fab2f60ed230e63
|
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at jaredmswanson@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in shortpixel.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem "rake", "~> 13.0"
|
9
|
+
|
10
|
+
gem "rubocop", "~> 1.7"
|
11
|
+
|
12
|
+
gem "solargraph", "~> 0.44.0"
|
13
|
+
|
14
|
+
gem "pry", "~> 0.14.1"
|
15
|
+
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
shortpixel (0.1.1)
|
5
|
+
activesupport (~> 6.1)
|
6
|
+
faraday (~> 1.8)
|
7
|
+
faraday_middleware (~> 1.1)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
activesupport (6.1.4.1)
|
13
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
14
|
+
i18n (>= 1.6, < 2)
|
15
|
+
minitest (>= 5.1)
|
16
|
+
tzinfo (~> 2.0)
|
17
|
+
zeitwerk (~> 2.3)
|
18
|
+
ast (2.4.2)
|
19
|
+
backport (1.2.0)
|
20
|
+
benchmark (0.1.1)
|
21
|
+
coderay (1.1.3)
|
22
|
+
concurrent-ruby (1.1.9)
|
23
|
+
diff-lcs (1.4.4)
|
24
|
+
e2mmap (0.1.0)
|
25
|
+
faraday (1.8.0)
|
26
|
+
faraday-em_http (~> 1.0)
|
27
|
+
faraday-em_synchrony (~> 1.0)
|
28
|
+
faraday-excon (~> 1.1)
|
29
|
+
faraday-httpclient (~> 1.0.1)
|
30
|
+
faraday-net_http (~> 1.0)
|
31
|
+
faraday-net_http_persistent (~> 1.1)
|
32
|
+
faraday-patron (~> 1.0)
|
33
|
+
faraday-rack (~> 1.0)
|
34
|
+
multipart-post (>= 1.2, < 3)
|
35
|
+
ruby2_keywords (>= 0.0.4)
|
36
|
+
faraday-em_http (1.0.0)
|
37
|
+
faraday-em_synchrony (1.0.0)
|
38
|
+
faraday-excon (1.1.0)
|
39
|
+
faraday-httpclient (1.0.1)
|
40
|
+
faraday-net_http (1.0.1)
|
41
|
+
faraday-net_http_persistent (1.2.0)
|
42
|
+
faraday-patron (1.0.0)
|
43
|
+
faraday-rack (1.0.0)
|
44
|
+
faraday_middleware (1.1.0)
|
45
|
+
faraday (~> 1.0)
|
46
|
+
i18n (1.8.10)
|
47
|
+
concurrent-ruby (~> 1.0)
|
48
|
+
jaro_winkler (1.5.4)
|
49
|
+
kramdown (2.3.1)
|
50
|
+
rexml
|
51
|
+
kramdown-parser-gfm (1.1.0)
|
52
|
+
kramdown (~> 2.0)
|
53
|
+
method_source (1.0.0)
|
54
|
+
minitest (5.14.4)
|
55
|
+
multipart-post (2.1.1)
|
56
|
+
nokogiri (1.12.5-x86_64-linux)
|
57
|
+
racc (~> 1.4)
|
58
|
+
parallel (1.21.0)
|
59
|
+
parser (3.0.2.0)
|
60
|
+
ast (~> 2.4.1)
|
61
|
+
pry (0.14.1)
|
62
|
+
coderay (~> 1.1)
|
63
|
+
method_source (~> 1.0)
|
64
|
+
racc (1.5.2)
|
65
|
+
rainbow (3.0.0)
|
66
|
+
rake (13.0.6)
|
67
|
+
regexp_parser (2.1.1)
|
68
|
+
reverse_markdown (2.0.0)
|
69
|
+
nokogiri
|
70
|
+
rexml (3.2.5)
|
71
|
+
rubocop (1.21.0)
|
72
|
+
parallel (~> 1.10)
|
73
|
+
parser (>= 3.0.0.0)
|
74
|
+
rainbow (>= 2.2.2, < 4.0)
|
75
|
+
regexp_parser (>= 1.8, < 3.0)
|
76
|
+
rexml
|
77
|
+
rubocop-ast (>= 1.9.1, < 2.0)
|
78
|
+
ruby-progressbar (~> 1.7)
|
79
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
80
|
+
rubocop-ast (1.11.0)
|
81
|
+
parser (>= 3.0.1.1)
|
82
|
+
ruby-progressbar (1.11.0)
|
83
|
+
ruby2_keywords (0.0.5)
|
84
|
+
solargraph (0.44.0)
|
85
|
+
backport (~> 1.2)
|
86
|
+
benchmark
|
87
|
+
bundler (>= 1.17.2)
|
88
|
+
diff-lcs (~> 1.4)
|
89
|
+
e2mmap
|
90
|
+
jaro_winkler (~> 1.5)
|
91
|
+
kramdown (~> 2.3)
|
92
|
+
kramdown-parser-gfm (~> 1.1)
|
93
|
+
parser (~> 3.0)
|
94
|
+
reverse_markdown (>= 1.0.5, < 3)
|
95
|
+
rubocop (>= 0.52)
|
96
|
+
thor (~> 1.0)
|
97
|
+
tilt (~> 2.0)
|
98
|
+
yard (~> 0.9, >= 0.9.24)
|
99
|
+
thor (1.1.0)
|
100
|
+
tilt (2.0.10)
|
101
|
+
tzinfo (2.0.4)
|
102
|
+
concurrent-ruby (~> 1.0)
|
103
|
+
unicode-display_width (2.1.0)
|
104
|
+
yard (0.9.26)
|
105
|
+
zeitwerk (2.4.2)
|
106
|
+
|
107
|
+
PLATFORMS
|
108
|
+
x86_64-linux
|
109
|
+
|
110
|
+
DEPENDENCIES
|
111
|
+
pry (~> 0.14.1)
|
112
|
+
rake (~> 13.0)
|
113
|
+
rubocop (~> 1.7)
|
114
|
+
shortpixel!
|
115
|
+
solargraph (~> 0.44.0)
|
116
|
+
|
117
|
+
BUNDLED WITH
|
118
|
+
2.2.27
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Jared
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# ShortPixel
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/shortpixel`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'shortpixel'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install shortpixel
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
### Reducer
|
25
|
+
The ShortPixel Reducer API expects an array of publically available urls for your images. (If you need to upload the urls from the local machine to ShortPixel use the PostReducer API.)
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
pngs = ['https://fakeurl.com/first_image.png', 'https://fakeurl.com/second_image.png']
|
29
|
+
|
30
|
+
client = ShortPixel::Client.new(api_key: <<SHORTPIXEL_API_KEY>>)
|
31
|
+
|
32
|
+
reducer = client.reducer
|
33
|
+
initial_request = reducer.call((lossy: 1, urllist: pngs)
|
34
|
+
|
35
|
+
# Wait for images to process
|
36
|
+
|
37
|
+
follow_up_request = reducer.recall
|
38
|
+
|
39
|
+
# Or repeat the original request
|
40
|
+
|
41
|
+
alternate_follow_up = reducer.call((lossy: 1, urllist: pngs)
|
42
|
+
```
|
43
|
+
|
44
|
+
For a list of parameters to use with the Reducer API, check out ShortPixel's [official API docs](https://shortpixel.com/api-docs).
|
45
|
+
|
46
|
+
For example, you could modify "initial_request" above like this:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
initial_request = reducer.call(lossy: 1, urllist: pngs, wait: 5, convertto: '+webp')
|
50
|
+
```
|
51
|
+
|
52
|
+
## Development
|
53
|
+
|
54
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
55
|
+
|
56
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
57
|
+
|
58
|
+
## Contributing
|
59
|
+
|
60
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jaredswanson/shortpixel. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/jaredswanson/shortpixel/blob/master/CODE_OF_CONDUCT.md).
|
61
|
+
|
62
|
+
## License
|
63
|
+
|
64
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
65
|
+
|
66
|
+
## Code of Conduct
|
67
|
+
|
68
|
+
Everyone interacting in the ShortPixel project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/jaredswanson/shortpixel/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
# require "rubocop/rake_task"
|
6
|
+
|
7
|
+
|
8
|
+
Rake::TestTask.new(:test) do |t|
|
9
|
+
t.libs << "test"
|
10
|
+
t.libs << "lib"
|
11
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
12
|
+
end
|
13
|
+
|
14
|
+
task default: :test
|
15
|
+
|
16
|
+
# RuboCop::RakeTask.new
|
17
|
+
|
18
|
+
# task default: :rubocop
|
data/bin/console
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "shortpixel"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
client = ShortPixel::Client.new(api_key: ENV["SHORTPIXEL_API_KEY"])
|
15
|
+
|
16
|
+
puts
|
17
|
+
puts "'client' variable created as a ShortPixel::Client using ENV['SHORTPIXEL_API_KEY'] as the API key."
|
18
|
+
puts
|
19
|
+
# png1 = 'https://mapright.s3.us-east-1.amazonaws.com/public/mapfish/maps/f9b5ffde-75bc-437d-b974-f071376a7a33.png'
|
20
|
+
# pngs = [png1]
|
21
|
+
# reducer = client.reducer.info(lossy: 1, urllist: pngs)
|
22
|
+
|
23
|
+
require "irb"
|
24
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# client.connection.post('reducer.php', {})
|
2
|
+
|
3
|
+
require 'active_support/core_ext/hash'
|
4
|
+
require 'faraday'
|
5
|
+
require 'faraday_middleware'
|
6
|
+
require 'json'
|
7
|
+
|
8
|
+
module ShortPixel
|
9
|
+
class Client
|
10
|
+
BASE_URL = 'https://api.shortpixel.com/v2/'
|
11
|
+
|
12
|
+
attr_reader :api_key, :adapter
|
13
|
+
|
14
|
+
def initialize(api_key:, adapter: Faraday.default_adapter, stubs: nil)
|
15
|
+
@api_key = api_key
|
16
|
+
@adapter = adapter
|
17
|
+
@stubs = stubs
|
18
|
+
end
|
19
|
+
|
20
|
+
def post_reducer
|
21
|
+
PostReducerResource.new(self)
|
22
|
+
end
|
23
|
+
|
24
|
+
# pngs = ["https://someurl.com/someimage.png"]
|
25
|
+
# reducer = client.reducer.info(lossy: 1, urllist: pngs)
|
26
|
+
# result = reducer.results.first
|
27
|
+
# result.lossy_url
|
28
|
+
# result.lossy_size
|
29
|
+
def reducer
|
30
|
+
ReducerResource.new(self)
|
31
|
+
end
|
32
|
+
|
33
|
+
def connection
|
34
|
+
@connection ||= Faraday.new do |conn|
|
35
|
+
conn.url_prefix = BASE_URL
|
36
|
+
conn.request :json
|
37
|
+
|
38
|
+
conn.response :json, content_type: "application/json"
|
39
|
+
|
40
|
+
conn.adapter adapter, @stubs
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
module ShortPixel
|
4
|
+
class Object < OpenStruct
|
5
|
+
def initialize(attributes)
|
6
|
+
super to_ostruct(attributes)
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_ostruct(obj)
|
10
|
+
if obj.is_a?(Hash)
|
11
|
+
OpenStruct.new(obj.map { |key, val| [key, to_ostruct(val)] }.to_h)
|
12
|
+
elsif obj.is_a?(Array)
|
13
|
+
results = obj.map { |o| to_ostruct(o) }
|
14
|
+
OpenStruct.new({ results: results })
|
15
|
+
else
|
16
|
+
obj
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
module ShortPixel
|
2
|
+
class Resource
|
3
|
+
attr_reader :client
|
4
|
+
|
5
|
+
def initialize(client)
|
6
|
+
@client = client
|
7
|
+
end
|
8
|
+
|
9
|
+
def post_request(url, body)
|
10
|
+
client.connection.post(url, body.merge(key: client.api_key))
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def extract_body(response)
|
16
|
+
body = transform_keys(JSON.parse(response.body))
|
17
|
+
handle_response(body)
|
18
|
+
end
|
19
|
+
|
20
|
+
def transform_keys(obj)
|
21
|
+
return obj.collect { |item| underscore_keys(item) } if obj.is_a? Array
|
22
|
+
|
23
|
+
return underscore_keys(obj) if obj.is_a? Hash
|
24
|
+
|
25
|
+
obj
|
26
|
+
end
|
27
|
+
|
28
|
+
def underscore_keys(hash)
|
29
|
+
hash.deep_transform_keys(&:underscore)
|
30
|
+
end
|
31
|
+
|
32
|
+
def handle_response(body)
|
33
|
+
responses = body.collect { |png_response| png_response['status'] }.uniq
|
34
|
+
responses.each { |response| check_for_errors(response) }
|
35
|
+
body
|
36
|
+
end
|
37
|
+
|
38
|
+
def check_for_errors(response)
|
39
|
+
code = response['code'].to_i
|
40
|
+
message = response['message']
|
41
|
+
|
42
|
+
case code
|
43
|
+
when -102
|
44
|
+
explanation = "#{message}; Invalid URL. Please make sure the URL is properly urlencoded and points to a valid image file."
|
45
|
+
raise Error, explanation
|
46
|
+
when -105
|
47
|
+
explanation = "#{message}; URL is missing for the call."
|
48
|
+
raise Error, explanation
|
49
|
+
when -106
|
50
|
+
explanation = "#{message}; URL is inaccessible from our server(s) due to access restrictions."
|
51
|
+
raise Error, explanation
|
52
|
+
when -107
|
53
|
+
explanation = "#{message}; Too many URLs in a POST, maximum allowed has been exceeded."
|
54
|
+
raise Error, explanation
|
55
|
+
when -108
|
56
|
+
explanation = "#{message}; Invalid user used for optimizing images from a particular domain."
|
57
|
+
raise Error, explanation
|
58
|
+
when -109
|
59
|
+
explanation = "#{message}; The uploaded files are not present. If the urllist is present instead, the message asks to use the Reducer API URL. The error will also be returned in case neither file_paths nor file_urls parameter is present"
|
60
|
+
raise Error, explanation
|
61
|
+
when -110
|
62
|
+
explanation = "#{message}; Upload error"
|
63
|
+
raise Error, explanation
|
64
|
+
when -111
|
65
|
+
explanation = "#{message}; File too big"
|
66
|
+
raise Error, explanation
|
67
|
+
when -112
|
68
|
+
explanation = "#{message}; Generic server error (details in the message field) "
|
69
|
+
raise Error, explanation
|
70
|
+
when -113
|
71
|
+
explanation = "#{message}; Too many inaccessible URLs from the same domain, please check accessibility and try again."
|
72
|
+
raise Error, explanation
|
73
|
+
when -115
|
74
|
+
explanation = "#{message}; Uploaded files are missing."
|
75
|
+
raise Error, explanation
|
76
|
+
when -201
|
77
|
+
explanation = "#{message}; Invalid image format."
|
78
|
+
raise Error, explanation
|
79
|
+
when -202
|
80
|
+
explanation = "#{message}; Invalid image or unsupported image format."
|
81
|
+
raise Error, explanation
|
82
|
+
when -203
|
83
|
+
explanation = "#{message}; Could not download file."
|
84
|
+
raise Error, explanation
|
85
|
+
when -204
|
86
|
+
explanation = "#{message}; The file couldn't be optimized, possibly timedout."
|
87
|
+
raise Error, explanation
|
88
|
+
when -205
|
89
|
+
explanation = "#{message}; The file's width and/or height is too big."
|
90
|
+
raise Error, explanation
|
91
|
+
when -301
|
92
|
+
explanation = "#{message}; The file is larger than the remaining quota."
|
93
|
+
raise Error, explanation
|
94
|
+
when -302
|
95
|
+
explanation = "#{message}; The file is no longer available."
|
96
|
+
raise Error, explanation
|
97
|
+
when -303
|
98
|
+
explanation = "#{message}; Internal API error: the file was not written on disk."
|
99
|
+
raise Error, explanation
|
100
|
+
when -304
|
101
|
+
explanation = "#{message}; Internal API Error - could not create user upload space. Please contact support if you encounter this error."
|
102
|
+
raise Error, explanation
|
103
|
+
when -305
|
104
|
+
explanation = "#{message}; Internal API error: Unknown, details usually in message."
|
105
|
+
raise Error, explanation
|
106
|
+
when -401
|
107
|
+
explanation = "#{message}; Invalid API key. Please check that the API key is the one provided to you."
|
108
|
+
raise Error, explanation
|
109
|
+
when -403
|
110
|
+
explanation = "#{message}; Quota exceeded. You need to subscribe to a larger plan or to buy an additional one time package to increase your quota."
|
111
|
+
raise Error, explanation
|
112
|
+
when -404
|
113
|
+
explanation = "#{message}; The maximum number of URLs in the optimization queue reached. Please try again in a minute."
|
114
|
+
raise Error, explanation
|
115
|
+
when -500
|
116
|
+
explanation = "#{message}; API is in maintenance mode. Please come back later."
|
117
|
+
raise Error, explanation
|
118
|
+
end
|
119
|
+
false # No Errors if this point reached without raised error
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# The Post Reducer API shrinks images you upload. Alternatively, the Reducer API shrinks images from a publically available url.
|
2
|
+
module ShortPixel
|
3
|
+
class PostReducerResource < Resource
|
4
|
+
def call(**params)
|
5
|
+
raise "PostReducer not built yet. Reach out if this is a feature you need."
|
6
|
+
# response = post_request('post-reducer.php')
|
7
|
+
# body = extract_body(response)
|
8
|
+
# PostReducer.new(body)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# The Reducer API shrinks images based on a publically available url. In contrast, the Post Reducer API by uploading the images.
|
2
|
+
module ShortPixel
|
3
|
+
class ReducerResource < Resource
|
4
|
+
attr_reader :params
|
5
|
+
|
6
|
+
def call(**params)
|
7
|
+
@params = params
|
8
|
+
response = post_request('reducer.php', @params)
|
9
|
+
body = extract_body(response)
|
10
|
+
Reducer.new(body)
|
11
|
+
end
|
12
|
+
|
13
|
+
def recall
|
14
|
+
call(**params)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/shortpixel.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "shortpixel/version"
|
4
|
+
|
5
|
+
module ShortPixel
|
6
|
+
autoload :Client, 'shortpixel/client'
|
7
|
+
autoload :Error, 'shortpixel/error'
|
8
|
+
autoload :Object, 'shortpixel/object'
|
9
|
+
autoload :Resource, 'shortpixel/resource'
|
10
|
+
|
11
|
+
autoload :Reducer, 'shortpixel/objects/reducer'
|
12
|
+
autoload :PostReducer, 'shortpixel/objects/post_reducer'
|
13
|
+
|
14
|
+
autoload :ReducerResource, 'shortpixel/resources/reducer'
|
15
|
+
autoload :PostReducerResource, 'shortpixel/resources/post_reducer'
|
16
|
+
end
|
Binary file
|
Binary file
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: shortpixel
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jared
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-10-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '6.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '6.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: faraday
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.8'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.8'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: faraday_middleware
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.1'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.1'
|
55
|
+
description: 'Ruby bindings for ShortPixel Api. ShortPixel Api docs can be found here:
|
56
|
+
https://shortpixel.com/api-docs'
|
57
|
+
email:
|
58
|
+
- jaredmswanson@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".rubocop.yml"
|
64
|
+
- CHANGELOG.md
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- Gemfile.lock
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- lib/shortpixel.rb
|
74
|
+
- lib/shortpixel/client.rb
|
75
|
+
- lib/shortpixel/error.rb
|
76
|
+
- lib/shortpixel/object.rb
|
77
|
+
- lib/shortpixel/objects/post_reducer.rb
|
78
|
+
- lib/shortpixel/objects/reducer.rb
|
79
|
+
- lib/shortpixel/resource.rb
|
80
|
+
- lib/shortpixel/resources/post_reducer.rb
|
81
|
+
- lib/shortpixel/resources/reducer.rb
|
82
|
+
- lib/shortpixel/version.rb
|
83
|
+
- short_pixel-0.1.0.gem
|
84
|
+
- short_pixel-0.1.1.gem
|
85
|
+
homepage: https://github.com/jaredswanson/shortpixel
|
86
|
+
licenses:
|
87
|
+
- MIT
|
88
|
+
metadata: {}
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: 2.4.0
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubygems_version: 3.2.22
|
105
|
+
signing_key:
|
106
|
+
specification_version: 4
|
107
|
+
summary: Ruby bindings for ShortPixel Api
|
108
|
+
test_files: []
|