google_images 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 58fccef6cbf2dfdf07ce8b028d1be47b063a5275
4
+ data.tar.gz: 229f61f0a687cee0bda8af736d2e8b996c09c602
5
+ SHA512:
6
+ metadata.gz: 35bd546f3ce9f6ad433eed60d7c758a80890063871741cd8df3a806689c07bd263636800c785b0921d0b0d906ee135f940401bbab0039afffdc3b2ba37c82dd2
7
+ data.tar.gz: 88171e3c84ebe9647abe830594eec51871b4674d971949c87c644cb8accc723df402aa62be2e2981bebe18c92abb5683a158a98a0056fe71bb81790b8ec6ef83
data/.gitignore ADDED
@@ -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
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.4
@@ -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,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Sharipov Ruslan
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,33 @@
1
+ # GoogleImages
2
+
3
+ Simple Google Images API library for ruby
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'google_images'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install google_images
20
+
21
+ ## Usage
22
+
23
+ GoogleImages.search('ruby', img_size: 'xlarge')
24
+
25
+ ## Contributing
26
+
27
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sharipov-ru/google_images. 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.
28
+
29
+
30
+ ## License
31
+
32
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
33
+
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'google_images'
5
+ require 'irb'
6
+
7
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,5 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'google_images/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "google_images"
8
+ spec.version = GoogleImages::VERSION
9
+ spec.authors = ["Sharipov Ruslan"]
10
+ spec.email = ["sharipov.ru@gmail.com"]
11
+
12
+ spec.summary = %q{Google Images Custom API client }
13
+ spec.description = %q{Simple and reliable solution for interacting with Google Images Search API}
14
+ spec.homepage = "http://github.com/sharipov-ru/google-images"
15
+ spec.license = "MIT"
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "http", "~> 2.0"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.10"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.4"
26
+ spec.add_development_dependency "pry", "~> 0.10"
27
+ end
@@ -0,0 +1,46 @@
1
+ require 'google_images/version'
2
+ require 'google_images/auth'
3
+ require 'google_images/query'
4
+ require 'google_images/query_executor'
5
+ require 'google_images/mapper'
6
+
7
+ # Namespace for classes and modules that handle Google Images search
8
+ module GoogleImages
9
+ # Google Images Lookup
10
+ #
11
+ # @param [String] text to perform search with
12
+ # @param [Hash] options the options perform search with
13
+ #
14
+ # @example Search images by `ruby` keyword
15
+ # GoogleImages.search('ruby')
16
+ #
17
+ # @example Search images with keyword and optional parameters
18
+ # GoogleImages.search('ruby', img_size: 'large', safe: 'high')
19
+ #
20
+ # @raise [GoogleImages::Errors::GeneralError]
21
+ # @raise [GoogleImages::Errors::NetworkError]
22
+ # @raise [GoogleImages::Errors::URLGenerationError]
23
+ # @raise [GoogleImages::Errors::ResponseParsingError]
24
+ # @raise [GoogleImages::Errors::ImageMappingError]
25
+ #
26
+ # @return [Array<GoogleImages::Image] array of found images
27
+ def self.search(text, options = {})
28
+ query = Query.new(text, options)
29
+ images_hash = QueryExecutor.run(query, authentication)
30
+ Mapper.build_images(images_hash)
31
+ end
32
+
33
+ private
34
+
35
+ def self.authentication
36
+ Auth.new(key, cx)
37
+ end
38
+
39
+ def self.key
40
+ ENV['GOOGLE_IMAGES_KEY']
41
+ end
42
+
43
+ def self.cx
44
+ ENV['GOOGLE_IMAGES_CX']
45
+ end
46
+ end
@@ -0,0 +1,11 @@
1
+ module GoogleImages
2
+ # A value-object which stores Google API authentication information
3
+ class Auth
4
+ attr_reader :key, :cx
5
+
6
+ def initialize(key, cx)
7
+ @key = key
8
+ @cx = cx
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ module GoogleImages
2
+ module Errors
3
+ end
4
+ end
@@ -0,0 +1,5 @@
1
+ module GoogleImages
2
+ module Errors
3
+ class GeneralError < StandardError; end;
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ require 'google_images/errors/general_error'
2
+
3
+ module GoogleImages
4
+ module Errors
5
+ class ImageMappingError < GeneralError; end;
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'google_images/errors/general_error'
2
+
3
+ module GoogleImages
4
+ module Errors
5
+ class NetworkError < GeneralError; end;
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'google_images/errors/general_error'
2
+
3
+ module GoogleImages
4
+ module Errors
5
+ class ResponseParsingError < GeneralError; end;
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'google_images/errors/general_error'
2
+
3
+ module GoogleImages
4
+ module Errors
5
+ class URLGenerationError < GeneralError; end;
6
+ end
7
+ end
@@ -0,0 +1,21 @@
1
+ module GoogleImages
2
+ class Image
3
+ attr_reader :title, :link, :context_link, :snippet, :mime, :byte_size,
4
+ :thumbnail_link, :thumbnail_height, :thumbnail_width,
5
+ :height, :width
6
+
7
+ def initialize(items_hash = {})
8
+ @title = items_hash['title']
9
+ @link = items_hash['link']
10
+ @context_link = items_hash['image']['context_link']
11
+ @snippet = items_hash['snippet']
12
+ @mime = items_hash['mime']
13
+ @height = items_hash['image']['height']
14
+ @width = items_hash['image']['width']
15
+ @byte_size = items_hash['image']['byte_size']
16
+ @thumbnail_link = items_hash['image']['thumbnailLink']
17
+ @thumbnail_height = items_hash['image']['thumbnailHeight']
18
+ @thumbnail_width = items_hash['image']['thumbnailWidth']
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ require 'google_images/image'
2
+ require 'google_images/errors/image_mapping_error'
3
+
4
+ module GoogleImages
5
+ module Mapper
6
+ def self.build_images(json)
7
+ items = json['items']
8
+ items.map { |item_hash| Image.new(item_hash) }
9
+ rescue => e
10
+ raise GoogleImages::Errors::ImageMappingError
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,23 @@
1
+ require 'http'
2
+ require 'google_images/errors/network_error'
3
+
4
+ module GoogleImages
5
+ # This class acts as a facade between http client library `http.rb` and
6
+ # our source code for abstracting library's entities and exceptions and using
7
+ # only needed part of the http client library API
8
+ module NetworkRequester
9
+ # Performs json GET request
10
+ #
11
+ # @param [String] url
12
+ #
13
+ # @example Get data from url
14
+ # NetworkRequester.get("http://url")
15
+ #
16
+ # @raise [GoogleImages::Errors::NetworkError]
17
+ def self.get(url)
18
+ HTTP.headers(accept: 'application/json').get(url)
19
+ rescue => e
20
+ raise GoogleImages::Errors::NetworkError, 'Network Error'
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,39 @@
1
+ require 'http'
2
+
3
+ module GoogleImages
4
+ # Class which represent google_images gem's query API
5
+ class Query
6
+ SUPPORTED_API_PARAMS = [
7
+ :c2coff,
8
+ :cr,
9
+ :date_restrict,
10
+ :filter,
11
+ :googlehost,
12
+ :img_color_type,
13
+ :img_dominant_color,
14
+ :img_size,
15
+ :img_type,
16
+ :num,
17
+ :rights,
18
+ :safe
19
+ ]
20
+
21
+ attr_reader :search_term, :api_params
22
+
23
+ # Initialize a new Query keeping only those api params which are supported
24
+ #
25
+ # @param [String] search_term
26
+ # @param [Hash] api_params
27
+ #
28
+ # @return [GoogleImages::Query] query object
29
+ def initialize(search_term, api_params = {})
30
+ @search_term = search_term
31
+ @api_params = supported_api_params(api_params)
32
+ end
33
+
34
+ private
35
+ def supported_api_params(api_params)
36
+ api_params.select { |key, _| SUPPORTED_API_PARAMS.include?(key) }
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,30 @@
1
+ require 'google_images/url_builder'
2
+ require 'google_images/network_requester'
3
+ require 'google_images/errors/response_parsing_error'
4
+
5
+ module GoogleImages
6
+ class QueryExecutor
7
+ # Execute the given query
8
+ #
9
+ # @param [GoogleImages::Query] query
10
+ # @param [GoogleImages::Auth] auth
11
+ #
12
+ # @raise [GoogleImages::Errors::URLGenerationError]
13
+ # @raise [GoogleImages::Errors::NetworkError]
14
+ # @raise [GoogleImages::Errors::ResponseParsingError]
15
+ #
16
+ # @return [Hash] response hash
17
+ def self.run(query, auth)
18
+ url = URLBuilder.build(query, auth)
19
+ response = NetworkRequester.get(url)
20
+ parse_json(response)
21
+ end
22
+
23
+ private
24
+ def self.parse_json(response)
25
+ JSON.parse(response)
26
+ rescue JSON::ParserError
27
+ raise GoogleImages::Errors::ResponseParsingError
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,43 @@
1
+ require 'google_images/errors/url_generation_error'
2
+
3
+ module GoogleImages
4
+ # Builds Google Images API - compatible URL
5
+ class URLBuilder
6
+ class << self
7
+ API_URL = 'https://www.googleapis.com/customsearch/v1'
8
+
9
+ def build(query, auth)
10
+ "#{API_URL}?q=#{query.search_term}&#{api_keys(auth)}&#{parameters(query)}"
11
+ rescue => e
12
+ raise GoogleImages::Errors::URLGenerationError
13
+ end
14
+
15
+ private
16
+ def api_keys(auth)
17
+ "key=#{auth.key}&cx=#{auth.cx}"
18
+ end
19
+
20
+ def parameters(query)
21
+ google_api_params = {
22
+ searchType: query.api_params[:search_type] || 'image',
23
+ alt: query.api_params[:alt] || 'json',
24
+ safe: query.api_params[:safe] || 'off',
25
+ num: query.api_params[:num] || nil,
26
+ rights: query.api_params[:rights] || nil,
27
+ imgSize: query.api_params[:img_size] || nil,
28
+ imgType: query.api_params[:img_type] || nil,
29
+ imgDominantColor: query.api_params[:img_dominant_color] || nil,
30
+ imgColorType: query.api_params[:img_color_type] || nil,
31
+ c2coff: query.api_params[:c2coff] || nil,
32
+ cr: query.api_params[:cr] || nil,
33
+ filter: query.api_params[:filter] || nil,
34
+ googlehost: query.api_params[:googlehost] || nil
35
+ }
36
+
37
+ google_api_params.map do |key, value|
38
+ value ? "#{key}=#{value}" : nil
39
+ end.compact.join("&")
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module GoogleImages
2
+ VERSION = '1.0.0'
3
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google_images
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Sharipov Ruslan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-06-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
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'
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'
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.10'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.10'
83
+ description: Simple and reliable solution for interacting with Google Images Search
84
+ API
85
+ email:
86
+ - sharipov.ru@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - CODE_OF_CONDUCT.md
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - google_images.gemspec
102
+ - lib/google_images.rb
103
+ - lib/google_images/auth.rb
104
+ - lib/google_images/errors.rb
105
+ - lib/google_images/errors/general_error.rb
106
+ - lib/google_images/errors/image_mapping_error.rb
107
+ - lib/google_images/errors/network_error.rb
108
+ - lib/google_images/errors/response_parsing_error.rb
109
+ - lib/google_images/errors/url_generation_error.rb
110
+ - lib/google_images/image.rb
111
+ - lib/google_images/mapper.rb
112
+ - lib/google_images/network_requester.rb
113
+ - lib/google_images/query.rb
114
+ - lib/google_images/query_executor.rb
115
+ - lib/google_images/url_builder.rb
116
+ - lib/google_images/version.rb
117
+ homepage: http://github.com/sharipov-ru/google-images
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.4.5
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: Google Images Custom API client
141
+ test_files: []
142
+ has_rdoc: