tinplate 1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8ab343df2b39b5fab64740c6801bd8be576f5822
4
+ data.tar.gz: e0c73ac143009fb9f614cdb7820f0d8aeee09e30
5
+ SHA512:
6
+ metadata.gz: e3046ddc36bc333a2b5b9caf1ed924c80a368a13d6b8bf78017cb7a55992f3fc71debdfa4d713e836acaea14fb18aa9b6a5fd79b785fcd1917e17bd89dffbd2d
7
+ data.tar.gz: 868ac2863f91150367d8760083f35df787b47b6f04404a8a98778b37386089b7878bdd2c664228e4352b138828fba15b670f2015873cb1e72c90dc32b6805d9f
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers 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. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+ ruby '2.1.6'
3
+
4
+ # Specify your gem's dependencies in tinplate.gemspec
5
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Crew
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.
@@ -0,0 +1,90 @@
1
+ # Tinplate
2
+
3
+ Wrapper 'round the [TinEye API](https://services.tineye.com/developers/tineyeapi/).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'tinplate'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install tinplate
20
+
21
+ ## Usage
22
+
23
+ There are only three API actions available: `search`, `remaining_searches` (to check the status of your account), and `image_count` (if you're curious how many total images TinEye has indexed).
24
+
25
+ `remaining_searches` returns an `OpenStruct` object with three attributes: `remaining_searches`, `start_date`, and `expire_date`.
26
+
27
+ `image_count` returns a plain old integer.
28
+
29
+ ### Search example
30
+
31
+ *Note: this gem does not (yet) support searching by image upload, only by image URL.*
32
+
33
+ ```ruby
34
+ tineye = Tinplate::TinEye.new
35
+ results = tineye.search(image_url: "http://example.com/photo.jpg")
36
+
37
+ results.total_results # => 2
38
+ results.total_backlinks # => 3
39
+ results.matches # => an Array of matched images (see below)
40
+
41
+ results.matches.each do |match|
42
+ # Do what you like with this matched image. The world is your oyster.
43
+ end
44
+ ```
45
+
46
+ #### Optional search parameters
47
+
48
+ `offset`: Default 0
49
+ `limit`: Default 100
50
+ `sort`: "score", "size", or "crawl_date". Default "score".
51
+ `order`: "asc" or "desc". Default "desc".
52
+
53
+ #### Example matched image
54
+
55
+ An `OpenStruct` object with the following attributes (/w example values):
56
+
57
+ ```ruby
58
+ width: 400
59
+ height: 300
60
+ size: 50734
61
+ image_url: "http://images.tineye.com/result/0f1e84b7b7538e8e7de048f4d45eb8f579e3e999941b3341ed9a754eb447ebb1",
62
+ format: "JPEG",
63
+ contributor: true,
64
+ overlay: "overlay/507bb6bf9a397284e2330be7c0671aadc7319b4b/0f1e84b7b7538e8e7de048f4d45eb8f579e3e999941b3341ed9a754eb447ebb1?m21=-9.06952e-05&m22=0.999975&m23=0.0295591&m11=0.999975&m13=-0.0171177&m12=9.06952e-05",
65
+ backlinks:
66
+ # These are also an OpenStruct objects, not Hashes.
67
+ [
68
+ {
69
+ url: "http://example-copier.com/photo.jpg",
70
+ crawl_date: "2012-06-30",
71
+ backlink: "http://example-copier.com/photo.jpg"
72
+ }
73
+ ]
74
+ ```
75
+
76
+ ## Development
77
+
78
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
79
+
80
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
81
+
82
+ ## Contributing
83
+
84
+ Bug reports and pull requests are welcome on GitHub at https://github.com/CrewLabs/tinplate. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
85
+
86
+
87
+ ## License
88
+
89
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
90
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tinplate"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,23 @@
1
+ require "faraday"
2
+ require "json"
3
+
4
+ require "tinplate/version"
5
+ require "tinplate/configuration"
6
+ require "tinplate/tineye"
7
+ require "tinplate/request_authenticator"
8
+ require "tinplate/search_results"
9
+ require "tinplate/errors"
10
+
11
+ module Tinplate
12
+ class << self
13
+ attr_accessor :configuration
14
+ end
15
+
16
+ def self.configuration
17
+ @configuration ||= Configuration.new
18
+ end
19
+
20
+ def self.configure
21
+ yield(configuration)
22
+ end
23
+ end
@@ -0,0 +1,29 @@
1
+ module Tinplate # :nodoc:
2
+ class Configuration # :nodoc:
3
+
4
+ # https://services.tineye.com/developers/tineyeapi/sandbox.html
5
+ SANDBOX_PUBLIC_KEY = "LCkn,2K7osVwkX95K4Oy"
6
+ SANDBOX_PRIVATE_KEY = "6mm60lsCNIB,FwOWjJqA80QZHh9BMwc-ber4u=t^"
7
+
8
+ attr_writer :public_key
9
+ attr_writer :private_key
10
+ attr_writer :test
11
+
12
+ def initialize
13
+ @test = true
14
+ end
15
+
16
+ def test?
17
+ !!@test
18
+ end
19
+
20
+ def public_key
21
+ test? ? SANDBOX_PUBLIC_KEY : @public_key
22
+ end
23
+
24
+ def private_key
25
+ test? ? SANDBOX_PRIVATE_KEY : @private_key
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,13 @@
1
+ module Tinplate
2
+ class Error < StandardError
3
+ attr_reader :code
4
+ attr_reader :type
5
+ attr_reader :message
6
+
7
+ def initialize(code, type, message)
8
+ @code = code
9
+ @type = @type
10
+ @message = message
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,48 @@
1
+ # https://services.tineye.com/developers/tineyeapi/authentication.html
2
+
3
+ module Tinplate
4
+ class RequestAuthenticator
5
+
6
+ def initialize(action, params = {}, image_name = "")
7
+ @action = action
8
+ @params = params
9
+ @image_name = image_name
10
+ @nonce = SecureRandom.hex
11
+ @date = Time.now.to_i
12
+ end
13
+
14
+ def params
15
+ {
16
+ api_key: Tinplate.configuration.public_key,
17
+ api_sig: signature,
18
+ nonce: @nonce,
19
+ date: @date
20
+ }
21
+ end
22
+
23
+ def signature_components
24
+ [
25
+ Tinplate.configuration.private_key,
26
+ "GET",
27
+ "", # Content-Type for GET requests is blank
28
+ URI.encode(@image_name).downcase,
29
+ @date.to_i,
30
+ @nonce,
31
+ "http://api.tineye.com/rest/#{@action}/",
32
+ hash_to_sorted_query_string(@params),
33
+ ]
34
+ end
35
+
36
+ def signature
37
+ OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha1"),
38
+ Tinplate.configuration.private_key,
39
+ signature_components.join)
40
+ end
41
+
42
+ def hash_to_sorted_query_string(params)
43
+ Hash[params.sort].map do |key, value|
44
+ "#{key}=#{URI.encode_www_form_component(value)}"
45
+ end.join("&")
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,18 @@
1
+ module Tinplate
2
+ class SearchResults < OpenStruct
3
+ def initialize(data)
4
+ super total_backlinks: data["total_backlinks"],
5
+ total_results: data["total_results"],
6
+ matches: parsed_matches(data["matches"])
7
+ end
8
+
9
+ private
10
+
11
+ def parsed_matches(matches_data)
12
+ matches_data.map do |match|
13
+ backlinks = { backlinks: match["backlinks"].map { |links| OpenStruct.new(links) } }
14
+ OpenStruct.new(match.merge(backlinks))
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,52 @@
1
+ module Tinplate
2
+ class TinEye
3
+ SORTS = ["score", "size", "crawl_date"]
4
+ ORDERS = ["asc", "desc"]
5
+
6
+ def search(image_url: nil, offset: 0, limit: 100, sort: "score", order: "desc")
7
+ raise ArgumentError.new("You must supply an image_url") if !image_url
8
+ raise ArgumentError.new("sort must be one of #{SORTS.join(', ')}") if !SORTS.include?(sort)
9
+ raise ArgumentError.new("order must be one of #{ORDERS.join(', ')}") if !ORDERS.include?(order)
10
+
11
+ response = request "search", image_url: image_url, offset: offset.to_s, limit: limit.to_s, sort: sort, order: order
12
+ Tinplate::SearchResults.new(response["results"])
13
+ end
14
+
15
+ def remaining_searches
16
+ results = request("remaining_searches")["results"]
17
+ OpenStruct.new(remaining_searches: results["remaining_searches"],
18
+ start_date: DateTime.parse(results["start_date"]),
19
+ expire_date: DateTime.parse(results["expire_date"]))
20
+ end
21
+
22
+ def image_count
23
+ request("image_count")["results"]
24
+ end
25
+
26
+
27
+ private
28
+
29
+ def request(action, params = {})
30
+ auth = Tinplate::RequestAuthenticator.new(action, params)
31
+ params.merge!(auth.params)
32
+
33
+ response = ::JSON.parse(connection.get("#{action}/", params).body)
34
+
35
+ if response["code"] != 200
36
+ raise Tinplate::Error.new(response["code"], response["messages"][0], response["messages"][1])
37
+ end
38
+
39
+ response
40
+ end
41
+
42
+ def connection
43
+ @conn ||= Faraday.new(url: "http://api.tineye.com/rest/") do |faraday|
44
+ faraday.request :url_encoded
45
+ faraday.response :logger
46
+ faraday.adapter Faraday.default_adapter
47
+ end
48
+ end
49
+
50
+ end
51
+
52
+ end
@@ -0,0 +1,3 @@
1
+ module Tinplate
2
+ VERSION = "1.0"
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tinplate/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tinplate"
8
+ spec.version = Tinplate::VERSION
9
+ spec.authors = ["Aaron Klaassen"]
10
+ spec.email = ["aaron@crew.co"]
11
+
12
+ spec.summary = %q{A wrapper around the TinEye API.}
13
+ spec.homepage = "https://github.com/CrewLabs/tinplate"
14
+ spec.license = "MIT"
15
+
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "faraday", "~> 0.9.2"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.10"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.4.0"
27
+ spec.add_development_dependency "pry"
28
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tinplate
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Aaron Klaassen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.4.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.4.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - aaron@crew.co
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - CODE_OF_CONDUCT.md
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - lib/tinplate.rb
100
+ - lib/tinplate/configuration.rb
101
+ - lib/tinplate/errors.rb
102
+ - lib/tinplate/request_authenticator.rb
103
+ - lib/tinplate/search_results.rb
104
+ - lib/tinplate/tineye.rb
105
+ - lib/tinplate/version.rb
106
+ - tinplate.gemspec
107
+ homepage: https://github.com/CrewLabs/tinplate
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.4.8
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: A wrapper around the TinEye API.
131
+ test_files: []