image_suckr 0.0.6 → 0.1.0

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.
data/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format doc
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  Description
2
2
  ---
3
- ImageSuckr is a ruby gem that allows you to get random images from Google for seeding purposes.
3
+ ImageSuckr is a ruby gem that allows you to get random images from the web for seeding and testing purposes.
4
4
 
5
- In a future it will support other sources.
5
+ By now, only Google is supported as images source.
6
6
 
7
7
  Installation
8
8
  ---
@@ -28,6 +28,10 @@ _All [Google Image Search API arguments](http://code.google.com/apis/imagesearch
28
28
  **To get the image content instead of the URL:**
29
29
 
30
30
  suckr.get_image_content
31
+
32
+ **To get a file reference to the image:**
33
+
34
+ suckr.get_image_file
31
35
 
32
36
  Other useful examples
33
37
  ---
data/image_suckr.gemspec CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
7
7
  s.version = ImageSuckr::VERSION
8
8
  s.platform = Gem::Platform::RUBY
9
9
  s.authors = ["Mauricio Miranda"]
10
- s.email = ["mmiranda@xoomcode.com"]
10
+ s.email = ["maurimiranda@gmail.com"]
11
11
  s.homepage = "https://github.com/maurimiranda/image_suckr"
12
12
  s.summary = %q{Gets images randomly from the web}
13
13
  s.description = %q{ImageSuckr is a ruby gem that allows you to get random images from Google for seeding purposes.}
@@ -18,4 +18,5 @@ Gem::Specification.new do |s|
18
18
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
+ s.add_development_dependency 'rspec'
21
22
  end
@@ -0,0 +1,42 @@
1
+ module ImageSuckr
2
+
3
+ class GoogleSuckr
4
+ attr_accessor :default_params
5
+
6
+ def initialize(default_params = {})
7
+ @default_params = {
8
+ :rsz => "8",
9
+ #"as_filetype" => "png",
10
+ #"imgc" => "color",
11
+ #"imgcolor" => "black",
12
+ #"imgsz" => "medium",
13
+ #"imgtype" => "photo",
14
+ #"safe" => "active",
15
+ :v => "1.0"
16
+ }.merge(default_params)
17
+ end
18
+
19
+ def get_image_url(params = {})
20
+ params = @default_params.merge(params)
21
+ params["q"] = rand(1000).to_s if params["q"].nil?
22
+
23
+ url = "http://ajax.googleapis.com/ajax/services/search/images?" + params.to_query
24
+
25
+ resp = Net::HTTP.get_response(URI.parse(url))
26
+ result = JSON.parse(resp.body)
27
+ response_data = result["responseData"]
28
+
29
+ result_size = response_data["results"].count
30
+ result["responseData"]["results"][rand(result_size)]["url"]
31
+ end
32
+
33
+ def get_image_content(params = {})
34
+ Net::HTTP.get_response(URI.parse(get_image_url(params))).body
35
+ end
36
+
37
+ def get_image_file(params = {})
38
+ open(URI.parse(get_image_url(params)))
39
+ end
40
+
41
+ end
42
+ end
@@ -1,3 +1,3 @@
1
1
  module ImageSuckr
2
- VERSION = "0.0.6"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/image_suckr.rb CHANGED
@@ -3,36 +3,10 @@ require 'json' unless defined?(JSON)
3
3
  require 'open-uri' unless defined?(OpenURI)
4
4
  require File.dirname(__FILE__) + '/support' unless "".respond_to? :to_query and [].respond_to? :to_query
5
5
 
6
- module ImageSuckr
7
- class GoogleSuckr
8
-
9
- DEFAULT_PARAMS = {
10
- "rsz" => "8",
11
- #"as_filetype" => "png",
12
- #"imgc" => "color",
13
- #"imgcolor" => "black",
14
- #"imgsz" => "medium",
15
- #"imgtype" => "photo",
16
- #"safe" => "active",
17
- "v" => "1.0"
18
- }
19
-
20
- def get_image_url(params = {})
21
- params = DEFAULT_PARAMS.merge(params)
22
- params["q"] = rand(1000).to_s if params["q"].nil?
23
- url = "http://ajax.googleapis.com/ajax/services/search/images?" + params.to_query
24
- resp = Net::HTTP.get_response(URI.parse(url))
25
- result = JSON.parse(resp.body)
26
- imageUrl = result["responseData"]["results"][rand(params["rsz"].to_i)]["url"]
27
- end
28
-
29
- def get_image_content(params = {})
30
- content = Net::HTTP.get_response(URI.parse(get_image_url(params))).body
31
- end
32
-
33
- def get_image_file(params = {})
34
- file = open(URI.parse(get_image_url(params)))
35
- end
6
+ require 'image_suckr/google_suckr'
36
7
 
8
+ module ImageSuckr
9
+
10
+ class << self
37
11
  end
38
12
  end
@@ -0,0 +1,48 @@
1
+ require File.dirname(__FILE__) + '/../lib/image_suckr'
2
+
3
+ describe "ImageSuckr" do
4
+ describe "GoogleSuckr" do
5
+ it "should have set the default parameters" do
6
+ image = ImageSuckr::GoogleSuckr.new
7
+
8
+ image.default_params.should_not be_nil
9
+ end
10
+
11
+ it "should have default values" do
12
+ image = ImageSuckr::GoogleSuckr.new
13
+
14
+ image.default_params[:rsz].should be_eql("8")
15
+ image.default_params[:v].should be_eql("1.0")
16
+ end
17
+
18
+ it "should provide access to override default params" do
19
+ params = {:rsz => "6", :v => "1.1", :as_filetype => "png"}
20
+ image = ImageSuckr::GoogleSuckr.new
21
+
22
+ image.default_params[:rsz].should be_eql "8"
23
+ image.default_params = params
24
+ image.default_params[:rsz].should be_eql "6"
25
+ end
26
+
27
+ it "should override the default values" do
28
+ params = {:rsz => "6", :v => "1.1", :as_filetype => "png"}
29
+ image = ImageSuckr::GoogleSuckr.new(params)
30
+
31
+ image.default_params[:v].should be_eql params[:v]
32
+ image.default_params[:rsz].should be_eql params[:rsz]
33
+ end
34
+
35
+ it "should return a valid URL" do
36
+ image = ImageSuckr::GoogleSuckr.new
37
+ open(URI.parse(image.get_image_url)).should be_a Tempfile
38
+ end
39
+
40
+ it "should return a temp file object" do
41
+ image = ImageSuckr::GoogleSuckr.new
42
+ file = image.get_image_file
43
+
44
+ file.should be_a Tempfile
45
+ end
46
+
47
+ end
48
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: image_suckr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,26 +9,43 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-05-09 00:00:00.000000000 -03:00
13
- default_executable:
14
- dependencies: []
12
+ date: 2012-11-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
15
30
  description: ImageSuckr is a ruby gem that allows you to get random images from Google
16
31
  for seeding purposes.
17
32
  email:
18
- - mmiranda@xoomcode.com
33
+ - maurimiranda@gmail.com
19
34
  executables: []
20
35
  extensions: []
21
36
  extra_rdoc_files: []
22
37
  files:
23
38
  - .gitignore
39
+ - .rspec
24
40
  - Gemfile
25
41
  - README.md
26
42
  - Rakefile
27
43
  - image_suckr.gemspec
28
44
  - lib/image_suckr.rb
45
+ - lib/image_suckr/google_suckr.rb
29
46
  - lib/image_suckr/version.rb
30
47
  - lib/support.rb
31
- has_rdoc: true
48
+ - spec/google_suckr_spec.rb
32
49
  homepage: https://github.com/maurimiranda/image_suckr
33
50
  licenses: []
34
51
  post_install_message:
@@ -49,8 +66,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
49
66
  version: '0'
50
67
  requirements: []
51
68
  rubyforge_project: image_suckr
52
- rubygems_version: 1.5.2
69
+ rubygems_version: 1.8.24
53
70
  signing_key:
54
71
  specification_version: 3
55
72
  summary: Gets images randomly from the web
56
- test_files: []
73
+ test_files:
74
+ - spec/google_suckr_spec.rb