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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4e646b162936b034d5e402b3c47957942019644a844c40ce643ce8606355b366
4
- data.tar.gz: fb30461669218584c80330b21b1de6f0abb88686d687c2fef151a3bcbe573e66
3
+ metadata.gz: 6c74f1bc36e6e016c34542b3ec3b489566d8f72a176945428ab7976cd9043524
4
+ data.tar.gz: 7b3c0d39b2332801d355b4b38ddc2c5cbdd96c5c99430e38700253d5c743f8fd
5
5
  SHA512:
6
- metadata.gz: 20a758012a6f63ad26b536f5f8fe3b9efd96bf43febc8f9ee11dec11c2f66245ec6280e6989549fca37521a8ac69087cebb4b10dad6198018d27e6f9143a93ac
7
- data.tar.gz: da05c621d15de702343f62173df7f96e874b0ca3dfc0fa951403ec30648cf0d8d3f976335aa8b4f39a90ba63c95fa561f799f4a764b0e8c11d2af4a0bfdc67b4
6
+ metadata.gz: '03899e5fbb07db090225c108c9c6a5db00ba4801819d405a174069e4a9a2dcfccc00ac344c42db806050dca3e124fbf9b29f9db57a8a741ef63700ed81fd72d2'
7
+ data.tar.gz: ac14fe344b6c537f8daacc3a9afe0d581e34c4a21bdd9188b02681c317126d233c4b33e6b06f7bb9186c18fd7d26cbcaf340f9cd83045532b03864cf70d8e442
data/Gemfile CHANGED
@@ -1,5 +1,5 @@
1
- source 'https://rubygems.org'
2
- ruby '2.1.6'
1
+ source "https://rubygems.org"
2
+ ruby "2.7.2"
3
3
 
4
4
  # Specify your gem's dependencies in tinplate.gemspec
5
5
  gemspec
data/README.md CHANGED
@@ -12,11 +12,15 @@ gem 'tinplate'
12
12
 
13
13
  And then execute:
14
14
 
15
- $ bundle
15
+ ```bash
16
+ $ bundle
17
+ ```
16
18
 
17
19
  Or install it yourself as:
18
20
 
19
- $ gem install tinplate
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
@@ -1,20 +1,34 @@
1
1
  module Tinplate
2
2
  class Error < StandardError
3
- def self.from_response(code, type, message)
4
- klass = case message
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
- klass.new(message)
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
@@ -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
- response = ::JSON.parse(connection.send(http_verb, "#{action}/", params).body)
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"][0], response["messages"][1])
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 :multipart
67
- faraday.request :url_encoded
68
- faraday.adapter Faraday.default_adapter
66
+ faraday.request :multipart
67
+ faraday.request :url_encoded
68
+ faraday.adapter Faraday.default_adapter
69
69
  end
70
70
  end
71
71
 
@@ -1,3 +1,3 @@
1
1
  module Tinplate
2
- VERSION = "2.0.2"
2
+ VERSION = "2.1.2"
3
3
  end
data/lib/tinplate.rb CHANGED
@@ -4,7 +4,6 @@ require "json"
4
4
  require "tinplate/version"
5
5
  require "tinplate/configuration"
6
6
  require "tinplate/tineye"
7
- require "tinplate/request_authenticator"
8
7
  require "tinplate/search_results"
9
8
  require "tinplate/errors"
10
9
 
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", "~> 1.10"
25
- spec.add_development_dependency "rake", "~> 10.0"
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.0.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: 2020-10-08 00:00:00.000000000 Z
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: '1.10'
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: '1.10'
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: '10.0'
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: '10.0'
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
- rubyforge_project:
133
- rubygems_version: 2.7.10
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