google_image_api 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/Gemfile +4 -0
- data/Rakefile +2 -0
- data/google_image_api.gemspec +21 -0
- data/lib/google_image_api.rb +37 -0
- data/lib/google_image_api/client.rb +62 -0
- data/lib/google_image_api/configuration.rb +25 -0
- data/lib/google_image_api/result.rb +26 -0
- data/lib/google_image_api/url.rb +13 -0
- data/lib/google_image_api/version.rb +3 -0
- metadata +55 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "google_image_api/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "google_image_api"
|
7
|
+
s.version = GoogleImageApi::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["sb4m"]
|
10
|
+
s.email = ["sb4m.01@gmail.com"]
|
11
|
+
s.homepage = "https://github.com/sb4m/google_image_api"
|
12
|
+
s.summary = %q{Access to google image api}
|
13
|
+
s.description = %q{simplify access to google image api}
|
14
|
+
|
15
|
+
s.rubyforge_project = "google_image_api"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
Dir[File.dirname(__FILE__) + '/google_image_api/**/*.rb'].each { |f| require f }
|
2
|
+
|
3
|
+
module GoogleImageApi
|
4
|
+
|
5
|
+
# @search = "term"
|
6
|
+
#
|
7
|
+
# result = GoogleImageApi.find(@search, {
|
8
|
+
# :imgsz => "medium",
|
9
|
+
# :rsz => 8,
|
10
|
+
# :start => 8,
|
11
|
+
# :imgtype => "face",
|
12
|
+
# :as_filetype => "jpg"
|
13
|
+
# })
|
14
|
+
#
|
15
|
+
# result.images.each do |img|
|
16
|
+
# puts img['url']
|
17
|
+
# end
|
18
|
+
|
19
|
+
def self.Configure(&block)
|
20
|
+
Configuration.instance.instance_eval(&block)
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.find( query, options = {} )
|
24
|
+
client.find( query, options )
|
25
|
+
end
|
26
|
+
|
27
|
+
Configure {
|
28
|
+
key nil
|
29
|
+
}
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def self.client
|
34
|
+
@client ||= GoogleImageApi::Client.new
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module GoogleImageApi
|
2
|
+
class Client
|
3
|
+
BASE_URL = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0"
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
end
|
7
|
+
|
8
|
+
def find( query, options = {} )
|
9
|
+
query = Url.new(query).title rescue query
|
10
|
+
Result.new( request_image( query, options ) )
|
11
|
+
end
|
12
|
+
|
13
|
+
def request( options )
|
14
|
+
require 'open-uri'
|
15
|
+
URI.parse( url_for( options ) ).read( "User-Agent" => "Ruby/#{RUBY_VERSION}" )
|
16
|
+
end
|
17
|
+
|
18
|
+
def request_image( query, options = {} )
|
19
|
+
request({:q => query}.merge( options ) )
|
20
|
+
end
|
21
|
+
|
22
|
+
protected
|
23
|
+
def configuration_options
|
24
|
+
{
|
25
|
+
:key => Configuration[:key],
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
def url_for( options )
|
30
|
+
url = BASE_URL.dup
|
31
|
+
options = configuration_options.delete_if {|key, value| value == nil}.merge( options )
|
32
|
+
options.each do |key, val|
|
33
|
+
value = urlify_value( val )
|
34
|
+
if url.include?( ":#{key}" )
|
35
|
+
url.sub! ":#{key}", value
|
36
|
+
else
|
37
|
+
url << "&#{key}=#{value}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
puts url
|
41
|
+
url
|
42
|
+
end
|
43
|
+
|
44
|
+
def urlify_value( val )
|
45
|
+
case val
|
46
|
+
when Array
|
47
|
+
encode( val.flatten.join( '|' ) )
|
48
|
+
else
|
49
|
+
encode( val )
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def encode( val )
|
54
|
+
case val
|
55
|
+
when String
|
56
|
+
URI.encode( val ).gsub( '&', '%26' )
|
57
|
+
else
|
58
|
+
val
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
|
3
|
+
module GoogleImageApi
|
4
|
+
class Configuration
|
5
|
+
include Singleton
|
6
|
+
|
7
|
+
def self.directives(*directives)
|
8
|
+
directives.each do |directive|
|
9
|
+
define_method directive do |*args|
|
10
|
+
if args.empty?
|
11
|
+
return instance_variable_get("@#{directive}")
|
12
|
+
else
|
13
|
+
instance_variable_set("@#{directive}", args.first)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.[](directive)
|
20
|
+
instance.send(directive)
|
21
|
+
end
|
22
|
+
|
23
|
+
directives :key
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module GoogleImageApi
|
2
|
+
class Result
|
3
|
+
def initialize(json)
|
4
|
+
require 'json'
|
5
|
+
@json = json
|
6
|
+
@data = JSON::load(json)
|
7
|
+
end
|
8
|
+
|
9
|
+
def page
|
10
|
+
@data.first
|
11
|
+
end
|
12
|
+
|
13
|
+
def images
|
14
|
+
@data["responseData"]["results"]
|
15
|
+
end
|
16
|
+
|
17
|
+
def raw_data
|
18
|
+
@data
|
19
|
+
end
|
20
|
+
|
21
|
+
def json
|
22
|
+
@json
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: google_image_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- sb4m
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-04-29 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: simplify access to google image api
|
15
|
+
email:
|
16
|
+
- sb4m.01@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- Rakefile
|
24
|
+
- google_image_api.gemspec
|
25
|
+
- lib/google_image_api.rb
|
26
|
+
- lib/google_image_api/client.rb
|
27
|
+
- lib/google_image_api/configuration.rb
|
28
|
+
- lib/google_image_api/result.rb
|
29
|
+
- lib/google_image_api/url.rb
|
30
|
+
- lib/google_image_api/version.rb
|
31
|
+
homepage: https://github.com/sb4m/google_image_api
|
32
|
+
licenses: []
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project: google_image_api
|
51
|
+
rubygems_version: 1.7.2
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: Access to google image api
|
55
|
+
test_files: []
|