tiqav 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gemtest ADDED
File without changes
data/History.txt ADDED
@@ -0,0 +1,3 @@
1
+ === 0.0.1 2012-06-09
2
+
3
+ * Search Images, Random Image.
data/Manifest.txt ADDED
@@ -0,0 +1,17 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.rdoc
4
+ Rakefile
5
+ lib/tiqav.rb
6
+ lib/tiqav/alpha_num.rb
7
+ lib/tiqav/image.rb
8
+ lib/tiqav/search.rb
9
+ lib/tiqav/tiqav.rb
10
+ samples/sample.rb
11
+ samples/search.rb
12
+ script/console
13
+ script/destroy
14
+ script/generate
15
+ test/test_alpha_num.rb
16
+ test/test_helper.rb
17
+ test/test_tiqav.rb
data/README.rdoc ADDED
@@ -0,0 +1,55 @@
1
+ = tiqav
2
+
3
+ * http://github.com/shokai/ruby-tiqav
4
+
5
+ == DESCRIPTION:
6
+
7
+ Search Images on Tiqav.com
8
+
9
+ == SYNOPSIS:
10
+
11
+ require 'rubygems'
12
+ require 'tiqav'
13
+ images = Tiqav.search 'ちくわ'
14
+
15
+ images.each do |img|
16
+ puts "(id:#{img.id}) #{img.permalink} => #{img.url}"
17
+ img.save(img.filename)
18
+ puts "saved!! => #{img.filename}"
19
+ end
20
+
21
+ img = Tiqav.random
22
+ puts img.url
23
+
24
+ == REQUIREMENTS:
25
+
26
+ * Ruby 1.8.7+ or 1.9.3+
27
+
28
+ == INSTALL:
29
+
30
+ * gem install tiqav
31
+
32
+ == LICENSE:
33
+
34
+ (The MIT License)
35
+
36
+ Copyright (c) 2012 Sho Hashimoto
37
+
38
+ Permission is hereby granted, free of charge, to any person obtaining
39
+ a copy of this software and associated documentation files (the
40
+ 'Software'), to deal in the Software without restriction, including
41
+ without limitation the rights to use, copy, modify, merge, publish,
42
+ distribute, sublicense, and/or sell copies of the Software, and to
43
+ permit persons to whom the Software is furnished to do so, subject to
44
+ the following conditions:
45
+
46
+ The above copyright notice and this permission notice shall be
47
+ included in all copies or substantial portions of the Software.
48
+
49
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
50
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
51
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
52
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
53
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
54
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
55
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ require './lib/tiqav'
6
+
7
+ Hoe.plugin :newgem
8
+ # Hoe.plugin :website
9
+ # Hoe.plugin :cucumberfeatures
10
+
11
+ # Generate all the Rake tasks
12
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
13
+ $hoe = Hoe.spec 'tiqav' do
14
+ self.developer 'Sho Hashimoto', 'hashimoto@shokai.org'
15
+ # self.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
16
+ self.rubyforge_name = self.name # TODO this is default value
17
+ self.extra_deps = [['nokogiri','>= 1.5.2']]
18
+
19
+ end
20
+
21
+ require 'newgem/tasks'
22
+ Dir['tasks/**/*.rake'].each { |t| load t }
23
+
24
+ # TODO - want other tests/tasks run by default? Add them to the list
25
+ # remove_task :default
26
+ # task :default => [:spec, :features]
@@ -0,0 +1,24 @@
1
+
2
+ class AlphaNum
3
+ def self.table
4
+ @@table ||= '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('')
5
+ end
6
+
7
+ def self.encode(num)
8
+ num > 61 ? self.encode(num/62)+self.table[num%62] : self.table[num%62]
9
+ end
10
+
11
+ def self.decode(str)
12
+ arr = str.split('').map{|i| self.table.index i }
13
+ for i in 0...arr.size do
14
+ return arr[i] if i == arr.size-1
15
+ arr[i+1] += arr[i]*62
16
+ end
17
+ end
18
+ end
19
+
20
+
21
+ if __FILE__ == $0
22
+ puts AlphaNum.decode 'Ab'
23
+ puts AlphaNum.encode 365
24
+ end
@@ -0,0 +1,39 @@
1
+
2
+ module Tiqav
3
+
4
+ class Image
5
+ attr_reader :id, :url, :permalink, :filename
6
+
7
+ def initialize(id)
8
+ @id = id
9
+ @filename = "#{@id}.jpg"
10
+ @url = URI.parse "http://tiqav.com/#{@filename}"
11
+ @permalink = URI.parse "http://tiqav.com/#{@id}"
12
+ end
13
+
14
+ def save(fname)
15
+ res = Net::HTTP.start(url.host, url.port).
16
+ request(Net::HTTP::Get.new url.path)
17
+ unless res.code.to_i == 200
18
+ raise Error, "HTTP Status #{res.code} - #{url}"
19
+ end
20
+ open(fname,'w+') do |f|
21
+ f.write res.body
22
+ end
23
+ fname
24
+ end
25
+
26
+ def exists?
27
+ case code = Net::HTTP.start(url.host, url.port).
28
+ request(Net::HTTP::Head.new url.path).
29
+ code.to_i
30
+ when 200
31
+ return true
32
+ when 404
33
+ return false
34
+ end
35
+ raise Error, "HTTP Status #{code} - Bad Response from #{url}"
36
+ end
37
+ end
38
+
39
+ end
@@ -0,0 +1,25 @@
1
+
2
+ module Tiqav
3
+ def self.search(word)
4
+ uri = URI.parse "http://tiqav.com/search/#{URI.encode word}"
5
+ res = Net::HTTP.start(uri.host, uri.port).request(Net::HTTP::Get.new uri.path)
6
+ raise Error, "HTTP Status #{res.code} at #{uri}" unless res.code.to_i == 200
7
+ doc = Nokogiri::HTML res.body
8
+ doc.xpath('//a').map{|a|
9
+ a['href']
10
+ }.reject{|a|
11
+ !(a =~ /^\/[a-zA-Z0-9]+$/)
12
+ }.map{|a|
13
+ Tiqav::Image.new a.scan(/([a-zA-Z0-9]+)/)[0][0]
14
+ }
15
+ end
16
+
17
+ def self.random
18
+ loop do
19
+ res = Image.new AlphaNum.encode rand 10000
20
+ return res if res.exists?
21
+ sleep 1
22
+ end
23
+ end
24
+
25
+ end
@@ -0,0 +1,9 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'nokogiri'
4
+
5
+ module Tiqav
6
+ class Error < StandardError
7
+ end
8
+ end
9
+
data/lib/tiqav.rb ADDED
@@ -0,0 +1,11 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'tiqav/tiqav'
5
+ require 'tiqav/image'
6
+ require 'tiqav/search'
7
+ require 'tiqav/alpha_num'
8
+
9
+ module Tiqav
10
+ VERSION = '0.0.1'
11
+ end
data/samples/sample.rb ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+ $:.unshift File.expand_path 'lib', File.dirname(__FILE__)+'/../'
4
+ require 'rubygems'
5
+ require 'tiqav'
6
+
7
+ img = Tiqav::Image.new 'ae'
8
+ puts img.exists?
9
+ puts img.url
10
+
11
+ img = Tiqav.random
12
+ puts img.id
13
+ puts img.permalink
14
+ puts img.url
15
+ img.save img.filename
16
+ puts "saved!! => #{img.filename}"
data/samples/search.rb ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+ $:.unshift File.expand_path 'lib', File.dirname(__FILE__)+'/../'
4
+ require 'rubygems'
5
+ require 'tiqav'
6
+
7
+ search_word = ARGV.empty? ? 'ちくわ' : ARGV.first
8
+
9
+ images = Tiqav.search search_word
10
+ images.each do |img|
11
+ puts "#{img.permalink} => #{img.url}"
12
+ end
13
+ puts "#{images.size} results"
14
+
15
+ puts "search word : #{search_word}"
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/tiqav.rb'}"
9
+ puts "Loading tiqav gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,23 @@
1
+ require File.expand_path 'test_helper', File.dirname(__FILE__)
2
+
3
+ class TestAlphaNum < Test::Unit::TestCase
4
+ def setup
5
+ end
6
+
7
+ def test_encode
8
+ assert AlphaNum.encode(365) == '5T'
9
+ end
10
+
11
+ def test_decode
12
+ assert AlphaNum.decode('Ab') == 2243
13
+ end
14
+
15
+ def test_encode_decode
16
+ assert AlphaNum.decode(AlphaNum.encode(1024)) == 1024
17
+ end
18
+
19
+ def test_decode_encode
20
+ assert AlphaNum.encode(AlphaNum.decode('AxCY53Ef')) == 'AxCY53Ef'
21
+ end
22
+
23
+ end
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/tiqav'
@@ -0,0 +1,43 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.expand_path 'test_helper', File.dirname(__FILE__)
3
+ require 'tmpdir'
4
+
5
+ class TestTiqav < Test::Unit::TestCase
6
+
7
+ def setup
8
+ @img = Tiqav.random
9
+ end
10
+
11
+ def test_search
12
+ assert Tiqav.search('ちくわ').size > 0
13
+ end
14
+
15
+ def test_image_class
16
+ assert @img.class == Tiqav::Image
17
+ end
18
+
19
+ def test_image_exists?
20
+ assert @img.exists? == true
21
+ end
22
+
23
+ def test_image_url
24
+ assert @img.url.kind_of? URI::HTTP
25
+ end
26
+
27
+ def test_image_permalink
28
+ assert @img.permalink.kind_of? URI::HTTP
29
+ end
30
+
31
+ def test_image_filename
32
+ assert @img.filename =~ /.+\.jpe?g/i
33
+ end
34
+
35
+ def test_image_save
36
+ Dir.mktmpdir do |dir|
37
+ fpath = File.expand_path @img.filename, dir
38
+ @img.save(fpath)
39
+ assert File.exists?(fpath) and File.stat(fpath).size > 0
40
+ end
41
+ end
42
+
43
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tiqav
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Sho Hashimoto
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-06-09 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: nokogiri
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 7
29
+ segments:
30
+ - 1
31
+ - 5
32
+ - 2
33
+ version: 1.5.2
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: rdoc
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 19
45
+ segments:
46
+ - 3
47
+ - 10
48
+ version: "3.10"
49
+ type: :development
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: newgem
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 5
60
+ segments:
61
+ - 1
62
+ - 5
63
+ - 3
64
+ version: 1.5.3
65
+ type: :development
66
+ version_requirements: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ name: hoe
69
+ prerelease: false
70
+ requirement: &id004 !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ hash: 29
76
+ segments:
77
+ - 2
78
+ - 15
79
+ version: "2.15"
80
+ type: :development
81
+ version_requirements: *id004
82
+ description: Search Images on Tiqav.com
83
+ email:
84
+ - hashimoto@shokai.org
85
+ executables: []
86
+
87
+ extensions: []
88
+
89
+ extra_rdoc_files:
90
+ - History.txt
91
+ - Manifest.txt
92
+ - README.rdoc
93
+ files:
94
+ - History.txt
95
+ - Manifest.txt
96
+ - README.rdoc
97
+ - Rakefile
98
+ - lib/tiqav.rb
99
+ - lib/tiqav/alpha_num.rb
100
+ - lib/tiqav/image.rb
101
+ - lib/tiqav/search.rb
102
+ - lib/tiqav/tiqav.rb
103
+ - samples/sample.rb
104
+ - samples/search.rb
105
+ - script/console
106
+ - script/destroy
107
+ - script/generate
108
+ - test/test_alpha_num.rb
109
+ - test/test_helper.rb
110
+ - test/test_tiqav.rb
111
+ - .gemtest
112
+ homepage: http://github.com/shokai/ruby-tiqav
113
+ licenses: []
114
+
115
+ post_install_message:
116
+ rdoc_options:
117
+ - --main
118
+ - README.rdoc
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ hash: 3
127
+ segments:
128
+ - 0
129
+ version: "0"
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ none: false
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ hash: 3
136
+ segments:
137
+ - 0
138
+ version: "0"
139
+ requirements: []
140
+
141
+ rubyforge_project: tiqav
142
+ rubygems_version: 1.8.17
143
+ signing_key:
144
+ specification_version: 3
145
+ summary: Search Images on Tiqav.com
146
+ test_files:
147
+ - test/test_alpha_num.rb
148
+ - test/test_helper.rb
149
+ - test/test_tiqav.rb