ruboty-image 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9b4bc3abb93dda1ff8171070aaec1468d29092fb
4
+ data.tar.gz: 83326a2f4739249d21e01a84152165168d60232b
5
+ SHA512:
6
+ metadata.gz: 2f30878a80c8d82d183ad83be8ee76b3754d83339648b545800e4b07efb643ddc5015a2b372abea839079fcfc95a9a99a243b5d5ea9dc9bb6e9d6979a493dae9
7
+ data.tar.gz: 628a994d113236409864475e6e030ac5e238aaa4a2e13194f09db476b70eec1de0816b426e8d21d5767c05bd7f5ebebf312f5a7e092eba6101087f798a1fbe83
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruboty-image.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 kaihar4
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,18 @@
1
+ # Ruboty::Image
2
+
3
+ Ruboty handler to search a image from Google image.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ruboty-image'
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```
16
+ @ruboty img top <keyword> - Search a image from Google image
17
+ @ruboty img rand <keyword> - Search a random image from Google image
18
+ ```
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,19 @@
1
+ module Ruboty
2
+ module Handlers
3
+ class Image < Base
4
+ on /img (?<mode>.+?) (?<keyword>.+)/, name: 'img', description: 'Search a image from Google image'
5
+
6
+ def img(message)
7
+ if url = search(message[:keyword], message[:mode].to_sym)
8
+ message.reply(url)
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def search(query, mode)
15
+ Ruboty::Image::Client.new(query, mode).get
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,54 @@
1
+ require 'json'
2
+
3
+ require 'faraday'
4
+
5
+ module Ruboty
6
+ module Image
7
+ class Client
8
+ GOOGLE_API_URL = "http://ajax.googleapis.com"
9
+
10
+ def initialize(query, mode)
11
+ @conn = Faraday.new(url: GOOGLE_API_URL) do |faraday|
12
+ faraday.request :url_encoded
13
+ faraday.adapter Faraday.default_adapter
14
+ end
15
+ @query = query
16
+ @mode = mode
17
+ end
18
+
19
+ def get
20
+ response = @conn.get do |req|
21
+ req.url '/ajax/services/search/images', params
22
+ end.body
23
+ images = JSON.parse(response)['responseData']['results']
24
+ # when @mode is :top -> images.length is 1
25
+ images.sample['unescapedUrl']
26
+ rescue
27
+ nil
28
+ end
29
+
30
+ private
31
+
32
+ def params
33
+ case @mode
34
+ when :top
35
+ {
36
+ v: '1.0',
37
+ safe: 'off',
38
+ filter: 1,
39
+ rsz: 1,
40
+ q: @query
41
+ }
42
+ when :rand
43
+ {
44
+ v: '1.0',
45
+ safe: 'off',
46
+ filter: 1,
47
+ rsz: 8,
48
+ q: @query
49
+ }
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,5 @@
1
+ module Ruboty
2
+ module Image
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ require 'ruboty'
2
+ require 'ruboty/image/client'
3
+ require 'ruboty/handlers/image'
@@ -0,0 +1,24 @@
1
+ $LOAD_PATH << File.expand_path('../lib', __FILE__)
2
+ require 'ruboty/image/version'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = 'ruboty-image'
6
+ spec.version = Ruboty::Image::VERSION
7
+ spec.authors = ['kaihar4']
8
+ spec.email = ['kaihar4@gmail.com']
9
+ spec.summary = 'Search a image from Google image'
10
+ spec.description = spec.summary
11
+ spec.homepage = 'https://github.com/kaihar4/ruboty-image'
12
+ spec.license = 'MIT'
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ['lib']
18
+
19
+ spec.add_dependency 'ruboty'
20
+ spec.add_dependency 'faraday'
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.7'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruboty-image
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - kaihar4
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruboty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description: Search a image from Google image
70
+ email:
71
+ - kaihar4@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/ruboty/handlers/image.rb
82
+ - lib/ruboty/image.rb
83
+ - lib/ruboty/image/client.rb
84
+ - lib/ruboty/image/version.rb
85
+ - ruboty-image.gemspec
86
+ homepage: https://github.com/kaihar4/ruboty-image
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.2.2
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Search a image from Google image
110
+ test_files: []