tinplate 2.0.2 → 2.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +2 -2
- data/README.md +6 -3
- data/lib/tinplate/configuration.rb +1 -7
- data/lib/tinplate/errors.rb +25 -11
- data/lib/tinplate/tineye.rb +8 -8
- data/lib/tinplate/version.rb +1 -1
- data/lib/tinplate.rb +0 -1
- data/tinplate.gemspec +2 -2
- metadata +15 -17
- data/lib/tinplate/request_authenticator.rb +0 -60
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6c74f1bc36e6e016c34542b3ec3b489566d8f72a176945428ab7976cd9043524
|
4
|
+
data.tar.gz: 7b3c0d39b2332801d355b4b38ddc2c5cbdd96c5c99430e38700253d5c743f8fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '03899e5fbb07db090225c108c9c6a5db00ba4801819d405a174069e4a9a2dcfccc00ac344c42db806050dca3e124fbf9b29f9db57a8a741ef63700ed81fd72d2'
|
7
|
+
data.tar.gz: ac14fe344b6c537f8daacc3a9afe0d581e34c4a21bdd9188b02681c317126d233c4b33e6b06f7bb9186c18fd7d26cbcaf340f9cd83045532b03864cf70d8e442
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -12,11 +12,15 @@ gem 'tinplate'
|
|
12
12
|
|
13
13
|
And then execute:
|
14
14
|
|
15
|
-
|
15
|
+
```bash
|
16
|
+
$ bundle
|
17
|
+
```
|
16
18
|
|
17
19
|
Or install it yourself as:
|
18
20
|
|
19
|
-
|
21
|
+
```bash
|
22
|
+
$ gem install tinplate
|
23
|
+
```
|
20
24
|
|
21
25
|
## Configuration
|
22
26
|
|
@@ -24,7 +28,6 @@ First you've got to let Tinplate know what your TinEye API keys are. In a Rails
|
|
24
28
|
|
25
29
|
```ruby
|
26
30
|
Tinplate.configure do |config|
|
27
|
-
config.public_key = "YOUR PUBLIC API KEY"
|
28
31
|
config.private_key = "YOUR PRIVATE API KEY"
|
29
32
|
config.test = false
|
30
33
|
end
|
@@ -2,10 +2,8 @@ module Tinplate # :nodoc:
|
|
2
2
|
class Configuration # :nodoc:
|
3
3
|
|
4
4
|
# https://services.tineye.com/developers/tineyeapi/sandbox.html
|
5
|
-
SANDBOX_PUBLIC_KEY = "LCkn,2K7osVwkX95K4Oy"
|
6
5
|
SANDBOX_PRIVATE_KEY = "6mm60lsCNIB,FwOWjJqA80QZHh9BMwc-ber4u=t^"
|
7
6
|
|
8
|
-
attr_writer :public_key
|
9
7
|
attr_writer :private_key
|
10
8
|
attr_writer :test
|
11
9
|
|
@@ -17,13 +15,9 @@ module Tinplate # :nodoc:
|
|
17
15
|
!!@test
|
18
16
|
end
|
19
17
|
|
20
|
-
def public_key
|
21
|
-
test? ? SANDBOX_PUBLIC_KEY : @public_key
|
22
|
-
end
|
23
|
-
|
24
18
|
def private_key
|
25
19
|
test? ? SANDBOX_PRIVATE_KEY : @private_key
|
26
20
|
end
|
27
21
|
|
28
22
|
end
|
29
|
-
end
|
23
|
+
end
|
data/lib/tinplate/errors.rb
CHANGED
@@ -1,20 +1,34 @@
|
|
1
1
|
module Tinplate
|
2
2
|
class Error < StandardError
|
3
|
-
def self.from_response(code,
|
4
|
-
|
5
|
-
when /503 Service Unavailable/ then Tinplate::ServiceUnavailableError
|
6
|
-
when /service is busy due to high load/ then Tinplate::ServiceUnavailableError
|
7
|
-
when /Image too simple/ then Tinplate::NoSignatureError
|
8
|
-
when /purchase another bundle/ then Tinplate::NoCreditsRemainingError
|
9
|
-
else
|
10
|
-
Tinplate::Error
|
11
|
-
end
|
3
|
+
def self.from_response(code, messages)
|
4
|
+
return UnauthorizedError.new(messages.first) if code == 403
|
12
5
|
|
13
|
-
|
6
|
+
messages.each do |message|
|
7
|
+
klass = class_from_message(message)
|
8
|
+
return klass.new(messages.join("\n")) if klass
|
9
|
+
end
|
10
|
+
|
11
|
+
Tinplate::Error.new(messages.join("\n"))
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.class_from_message(message)
|
15
|
+
case message
|
16
|
+
when /503 Service Unavailable/ then Tinplate::ServiceUnavailableError
|
17
|
+
when /service is busy due to high load/ then Tinplate::ServiceUnavailableError
|
18
|
+
when /Image too simple/ then Tinplate::NoSignatureError
|
19
|
+
when /purchase another bundle/ then Tinplate::NoCreditsRemainingError
|
20
|
+
when /Could not download/ then Tinplate::InaccessibleURLError
|
21
|
+
when /Please supply an image/ then Tinplate::InvalidSearchError
|
22
|
+
when /Error reading image data/ then Tinplate::InvalidImageDataError
|
23
|
+
end
|
14
24
|
end
|
15
25
|
end
|
16
26
|
|
17
27
|
class ServiceUnavailableError < Error; end
|
18
28
|
class NoSignatureError < Error; end
|
19
29
|
class NoCreditsRemainingError < Error; end
|
20
|
-
end
|
30
|
+
class UnauthorizedError < Error; end
|
31
|
+
class InaccessibleURLError < Error; end
|
32
|
+
class InvalidSearchError < Error; end
|
33
|
+
class InvalidImageDataError < Error; end
|
34
|
+
end
|
data/lib/tinplate/tineye.rb
CHANGED
@@ -47,15 +47,15 @@ module Tinplate
|
|
47
47
|
Faraday::UploadIO.new(params.delete(:image_path), "image/jpeg")
|
48
48
|
end
|
49
49
|
|
50
|
-
auth = Tinplate::RequestAuthenticator.new(action, params, upload && upload.original_filename)
|
51
|
-
params.merge!(auth.params)
|
52
|
-
|
53
50
|
params.merge!(image_upload: upload) if upload
|
54
51
|
|
55
|
-
|
52
|
+
headers = { "x-api-key" => Tinplate.configuration.private_key }
|
53
|
+
|
54
|
+
response = connection.send(http_verb, "#{action}/", params, headers)
|
55
|
+
response = ::JSON.parse(response.body)
|
56
56
|
|
57
57
|
if response["code"] != 200
|
58
|
-
raise Tinplate::Error.from_response(response["code"], response["messages"]
|
58
|
+
raise Tinplate::Error.from_response(response["code"], response["messages"])
|
59
59
|
end
|
60
60
|
|
61
61
|
response
|
@@ -63,9 +63,9 @@ module Tinplate
|
|
63
63
|
|
64
64
|
def connection
|
65
65
|
@conn ||= Faraday.new(url: "https://api.tineye.com/rest/") do |faraday|
|
66
|
-
faraday.request
|
67
|
-
faraday.request
|
68
|
-
faraday.adapter
|
66
|
+
faraday.request :multipart
|
67
|
+
faraday.request :url_encoded
|
68
|
+
faraday.adapter Faraday.default_adapter
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
data/lib/tinplate/version.rb
CHANGED
data/lib/tinplate.rb
CHANGED
data/tinplate.gemspec
CHANGED
@@ -21,8 +21,8 @@ Gem::Specification.new do |spec|
|
|
21
21
|
|
22
22
|
spec.add_dependency "faraday", [">= 0.9.2", "< 2.0.0"]
|
23
23
|
|
24
|
-
spec.add_development_dependency "bundler", "
|
25
|
-
spec.add_development_dependency "rake", "
|
24
|
+
spec.add_development_dependency "bundler", ">= 2.2.10"
|
25
|
+
spec.add_development_dependency "rake", ">= 12.3.3"
|
26
26
|
spec.add_development_dependency "rspec", "~> 3.4.0"
|
27
27
|
spec.add_development_dependency "pry"
|
28
28
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tinplate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aaron Klaassen
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-05-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -34,30 +34,30 @@ dependencies:
|
|
34
34
|
name: bundler
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
|
-
- - "
|
37
|
+
- - ">="
|
38
38
|
- !ruby/object:Gem::Version
|
39
|
-
version:
|
39
|
+
version: 2.2.10
|
40
40
|
type: :development
|
41
41
|
prerelease: false
|
42
42
|
version_requirements: !ruby/object:Gem::Requirement
|
43
43
|
requirements:
|
44
|
-
- - "
|
44
|
+
- - ">="
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version:
|
46
|
+
version: 2.2.10
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rake
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
50
50
|
requirements:
|
51
|
-
- - "
|
51
|
+
- - ">="
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version:
|
53
|
+
version: 12.3.3
|
54
54
|
type: :development
|
55
55
|
prerelease: false
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
57
57
|
requirements:
|
58
|
-
- - "
|
58
|
+
- - ">="
|
59
59
|
- !ruby/object:Gem::Version
|
60
|
-
version:
|
60
|
+
version: 12.3.3
|
61
61
|
- !ruby/object:Gem::Dependency
|
62
62
|
name: rspec
|
63
63
|
requirement: !ruby/object:Gem::Requirement
|
@@ -86,7 +86,7 @@ dependencies:
|
|
86
86
|
- - ">="
|
87
87
|
- !ruby/object:Gem::Version
|
88
88
|
version: '0'
|
89
|
-
description:
|
89
|
+
description:
|
90
90
|
email:
|
91
91
|
- aaron@unsplash.com
|
92
92
|
executables: []
|
@@ -105,7 +105,6 @@ files:
|
|
105
105
|
- lib/tinplate.rb
|
106
106
|
- lib/tinplate/configuration.rb
|
107
107
|
- lib/tinplate/errors.rb
|
108
|
-
- lib/tinplate/request_authenticator.rb
|
109
108
|
- lib/tinplate/search_results.rb
|
110
109
|
- lib/tinplate/tineye.rb
|
111
110
|
- lib/tinplate/version.rb
|
@@ -114,7 +113,7 @@ homepage: https://github.com/unsplash/tinplate
|
|
114
113
|
licenses:
|
115
114
|
- MIT
|
116
115
|
metadata: {}
|
117
|
-
post_install_message:
|
116
|
+
post_install_message:
|
118
117
|
rdoc_options: []
|
119
118
|
require_paths:
|
120
119
|
- lib
|
@@ -129,9 +128,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
129
128
|
- !ruby/object:Gem::Version
|
130
129
|
version: '0'
|
131
130
|
requirements: []
|
132
|
-
|
133
|
-
|
134
|
-
signing_key:
|
131
|
+
rubygems_version: 3.1.4
|
132
|
+
signing_key:
|
135
133
|
specification_version: 4
|
136
134
|
summary: A wrapper around the TinEye API.
|
137
135
|
test_files: []
|
@@ -1,60 +0,0 @@
|
|
1
|
-
# https://services.tineye.com/developers/tineyeapi/authentication.html
|
2
|
-
|
3
|
-
require "securerandom"
|
4
|
-
require "uri"
|
5
|
-
require "openssl"
|
6
|
-
|
7
|
-
module Tinplate
|
8
|
-
class RequestAuthenticator
|
9
|
-
|
10
|
-
def initialize(action, params = {}, image_name = "")
|
11
|
-
@action = action
|
12
|
-
@params = params
|
13
|
-
@image_name = image_name || ""
|
14
|
-
@nonce = SecureRandom.hex
|
15
|
-
@date = Time.now.to_i
|
16
|
-
end
|
17
|
-
|
18
|
-
def params
|
19
|
-
{
|
20
|
-
api_key: Tinplate.configuration.public_key,
|
21
|
-
api_sig: signature,
|
22
|
-
nonce: @nonce,
|
23
|
-
date: @date
|
24
|
-
}
|
25
|
-
end
|
26
|
-
|
27
|
-
def verb
|
28
|
-
@image_name.empty? ? "GET" : "POST"
|
29
|
-
end
|
30
|
-
|
31
|
-
def content_type
|
32
|
-
verb == "GET" ? "" : "multipart/form-data; boundary=-----------RubyMultipartPost"
|
33
|
-
end
|
34
|
-
|
35
|
-
def signature_components
|
36
|
-
[
|
37
|
-
Tinplate.configuration.private_key,
|
38
|
-
verb,
|
39
|
-
content_type,
|
40
|
-
URI.encode_www_form_component(@image_name).downcase,
|
41
|
-
@date.to_i,
|
42
|
-
@nonce,
|
43
|
-
"https://api.tineye.com/rest/#{@action}/",
|
44
|
-
hash_to_sorted_query_string(@params),
|
45
|
-
]
|
46
|
-
end
|
47
|
-
|
48
|
-
def signature
|
49
|
-
OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"),
|
50
|
-
Tinplate.configuration.private_key,
|
51
|
-
signature_components.join)
|
52
|
-
end
|
53
|
-
|
54
|
-
def hash_to_sorted_query_string(params)
|
55
|
-
Hash[params.sort].map do |key, value|
|
56
|
-
"#{key}=#{URI.encode_www_form_component(value)}"
|
57
|
-
end.join("&")
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|