eshopworks-rboss 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,6 @@
1
+ === Version 0.1.2
2
+ * Maintain search result data in ResultCollection
3
+
1
4
  === Version 0.1.1
2
5
  * Fixed exact search error
3
6
  * search method takes the same arguments as other search methods
data/Manifest.txt CHANGED
@@ -22,11 +22,13 @@ lib/boss/result/image.rb
22
22
  lib/boss/result/news.rb
23
23
  lib/boss/result/spell.rb
24
24
  lib/boss/result/web.rb
25
+ lib/boss/result_collection.rb
25
26
  lib/boss/result_factory.rb
26
27
  lib/boss/version.rb
27
28
  rboss.gemspec
28
29
  spec/boss/api_spec.rb
29
30
  spec/boss/config_spec.rb
31
+ spec/boss/result_collection_spec.rb
30
32
  spec/boss/result_factory_spec.rb
31
33
  spec/spec.opts
32
34
  spec/spec_helper.rb
data/TODO.txt CHANGED
@@ -1,4 +1,4 @@
1
1
  == FEATURES/PROBLEMS:
2
2
 
3
3
  * Search all
4
- * TODO: cleanup encode_for_url
4
+ * Clean up ResultCollection, we store hash result in ResultCollection aswell as objects.
data/lib/boss/api.rb CHANGED
@@ -2,7 +2,6 @@ require 'net/http'
2
2
  require 'rexml/document'
3
3
  require 'uri'
4
4
 
5
- require 'ostruct'
6
5
  require 'cgi'
7
6
 
8
7
 
@@ -29,23 +28,23 @@ module Boss
29
28
  end
30
29
 
31
30
  def search(term, *conditions, &block)
32
- search_boss term, SearchType::WEB, *conditions, &block
31
+ search_boss(term, SearchType::WEB, *conditions, &block)
33
32
  end
34
33
 
35
34
  def search_images(term, *conditions, &block)
36
- search_boss term, SearchType::IMAGES, *conditions, &block
35
+ search_boss(term, SearchType::IMAGES, *conditions, &block)
37
36
  end
38
37
 
39
38
  def search_news(term, *conditions, &block)
40
- search_boss term, SearchType::NEWS, *conditions, &block
39
+ search_boss(term, SearchType::NEWS, *conditions, &block)
41
40
  end
42
41
 
43
42
  def search_web(term, *conditions, &block)
44
- search_boss term, SearchType::WEB, *conditions, &block
43
+ search_boss(term, SearchType::WEB, *conditions, &block)
45
44
  end
46
45
 
47
46
  def search_spelling(term, *conditions, &block)
48
- search_boss term, SearchService::SPELL, *conditions, &block
47
+ search_boss(term, SearchService::SPELL, *conditions, &block)
49
48
  end
50
49
 
51
50
  private
@@ -65,15 +64,15 @@ module Boss
65
64
  raise InvalidFormat unless (FORMATS.include? config.format)
66
65
  raise InvalidConfig unless (config.count>0)
67
66
 
68
- request = query_url terms, search_type, config
69
- response = Net::HTTP.get_response request
67
+ request = URI.parse(build_request_url(terms, search_type, config))
68
+ response = Net::HTTP.get_response(request)
70
69
 
71
70
  case response.code
72
71
  when "200"
73
72
  data = response.body
74
73
 
75
74
  if format_as_objects
76
- search_results = ResultFactory.build search_type, data
75
+ search_results = ResultFactory.build(search_type, data)
77
76
  else
78
77
  search_results = data
79
78
  end
@@ -90,9 +89,9 @@ module Boss
90
89
  end
91
90
 
92
91
  protected
93
- def query_url(terms, search_type, config)
92
+ def build_request_url(terms, search_type, config)
94
93
  #We could use URI.encode but it leaves things like ? unencoded which fails search.
95
- encoded_terms = CGI.escape terms
94
+ encoded_terms = CGI.escape(terms)
96
95
  # puts "#{@endpoint}#{search_type}/#{boss_version}/#{encoded_terms}?appid=#{@app_id}#{config.to_url}"
97
96
  "#{@endpoint}#{search_type}/#{boss_version}/#{encoded_terms}?appid=#{@app_id}#{config.to_url}"
98
97
  end
data/lib/boss/config.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'ostruct'
2
+
1
3
  module Boss
2
4
 
3
5
  class Config < OpenStruct
@@ -1,3 +1,5 @@
1
+ require 'ostruct'
2
+
1
3
  module Boss
2
4
  module Result
3
5
  class Base < OpenStruct
@@ -0,0 +1,26 @@
1
+ module Boss
2
+ class ResultCollection
3
+ include Enumerable
4
+
5
+ def initialize(search_data)
6
+ search_data.each do |name, value|
7
+ instance_variable_set("@#{name}",value)
8
+ instance_eval("def #{name}\n @#{name}\n end")
9
+ end
10
+ @results=[]
11
+ end
12
+
13
+ def each
14
+ @results.each { |result| yield result }
15
+ end
16
+
17
+ def <<(element)
18
+ @results << element
19
+ end
20
+
21
+ def [](key)
22
+ @results[key]
23
+ end
24
+
25
+ end
26
+ end
@@ -18,31 +18,31 @@ module Boss
18
18
  if json_hash.has_key? 'Error'
19
19
  raise BossError, "Web service error"
20
20
  end
21
-
22
- search_results=[]
23
21
 
24
- if has_results json_hash, :for => search_type
22
+ result_collection = ResultCollection.new(json_hash[SEARCH_RESPONSE])
23
+
24
+ if has_results?(json_hash, :for => search_type)
25
25
 
26
26
  json_hash[SEARCH_RESPONSE]["#{RESULT_SET}_#{search_type}"].each do |result|
27
27
 
28
28
  case search_type
29
29
  when SearchType::WEB
30
- search_results << Result::Web.new(result)
30
+ result_collection << Result::Web.new(result)
31
31
  when SearchType::IMAGES
32
- search_results << Result::Image.new(result)
32
+ result_collection << Result::Image.new(result)
33
33
  when SearchType::NEWS
34
- search_results << Result::News.new(result)
34
+ result_collection << Result::News.new(result)
35
35
  when SearchType::SPELL
36
- search_results << Result::Spell.new(result)
36
+ result_collection << Result::Spell.new(result)
37
37
  end
38
38
 
39
39
  end
40
40
  end
41
41
 
42
- search_results
42
+ result_collection
43
43
  end
44
44
 
45
- def has_results(json_hash, type={})
45
+ def has_results?(json_hash, type={})
46
46
  !(json_hash[SEARCH_RESPONSE]["#{RESULT_SET}_#{type[:for]}"]).nil?
47
47
  end
48
48
 
data/lib/boss/version.rb CHANGED
@@ -2,7 +2,7 @@ module Boss #:nodoc:
2
2
  class VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- TINY = 1
5
+ TINY = 2
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
data/lib/boss.rb CHANGED
@@ -4,6 +4,7 @@ $:.unshift(File.dirname(__FILE__)) unless
4
4
  require 'boss/api'
5
5
  require 'boss/config'
6
6
  require 'boss/result'
7
+ require 'boss/result_collection'
7
8
  require 'boss/result_factory'
8
9
  require 'boss/version'
9
10
 
data/rboss.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = %q{rboss}
3
- s.version = "0.1.1"
3
+ s.version = "0.1.2"
4
4
 
5
5
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
6
6
  s.authors = ["Joseph Wilk"]
@@ -8,7 +8,7 @@ Gem::Specification.new do |s|
8
8
  s.description = %q{Api wrapping Yahoo Boss search}
9
9
  s.email = ["joe@eshopworks.co.uk"]
10
10
  s.extra_rdoc_files = ["History.txt", "License.txt", "Manifest.txt", "TODO.txt"]
11
- s.files = ["History.txt", "License.txt", "Manifest.txt", "README.textile", "Rakefile", "TODO.txt", "config/hoe.rb", "config/requirements.rb", "gem_tasks/deployment.rake", "gem_tasks/environment.rake", "gem_tasks/fix_cr_lf.rake", "gem_tasks/gemspec.rake", "gem_tasks/rspec.rake", "gem_tasks/verify_rcov.rake", "gem_tasks/website.rake", "lib/boss.rb", "lib/boss/api.rb", "lib/boss/config.rb", "lib/boss/result.rb", "lib/boss/result/base.rb", "lib/boss/result/image.rb", "lib/boss/result/news.rb", "lib/boss/result/spell.rb", "lib/boss/result/web.rb", "lib/boss/result_factory.rb", "lib/boss/version.rb", "rboss.gemspec", "spec/boss/api_spec.rb", "spec/boss/config_spec.rb", "spec/boss/result_factory_spec.rb", "spec/spec.opts", "spec/spec_helper.rb"]
11
+ s.files = ["History.txt", "License.txt", "Manifest.txt", "README.textile", "Rakefile", "TODO.txt", "config/hoe.rb", "config/requirements.rb", "gem_tasks/deployment.rake", "gem_tasks/environment.rake", "gem_tasks/fix_cr_lf.rake", "gem_tasks/gemspec.rake", "gem_tasks/rspec.rake", "gem_tasks/verify_rcov.rake", "gem_tasks/website.rake", "lib/boss.rb", "lib/boss/api.rb", "lib/boss/config.rb", "lib/boss/result.rb", "lib/boss/result/base.rb", "lib/boss/result/image.rb", "lib/boss/result/news.rb", "lib/boss/result/spell.rb", "lib/boss/result/web.rb", "lib/boss/result_collection.rb", "lib/boss/result_factory.rb", "lib/boss/version.rb", "rboss.gemspec", "spec/boss/api_spec.rb", "spec/boss/config_spec.rb", "spec/boss/result_collection_spec.rb", "spec/boss/result_factory_spec.rb", "spec/spec.opts", "spec/spec_helper.rb"]
12
12
  s.has_rdoc = true
13
13
  s.homepage = %q{http://github.com/eshopworks/rboss-gem}
14
14
  s.rdoc_options = ["--main", "README.txt"]
@@ -17,14 +17,14 @@ describe Boss::Api do
17
17
  it "should make a spelling request to yahoo service" do
18
18
  Net::HTTP.should_receive(:get_response).and_return{ mock_http_response }
19
19
 
20
- @api.search_spelling "girafes"
20
+ @api.search_spelling("girafes")
21
21
  end
22
22
 
23
23
  it "should build the spelling objects" do
24
24
  Net::HTTP.stub!(:get_response).and_return{ mock_http_response }
25
25
  Boss::ResultFactory.should_receive(:build).with(Boss::SearchService::SPELL, '{"ysearchresponse":{}}')
26
26
 
27
- @api.search_spelling "girafes"
27
+ @api.search_spelling("girafes")
28
28
  end
29
29
 
30
30
  end
@@ -33,14 +33,14 @@ describe Boss::Api do
33
33
  it "should make a news request to yahoo service" do
34
34
  Net::HTTP.should_receive(:get_response).and_return{ mock_http_response }
35
35
 
36
- @api.search_news "monkey"
36
+ @api.search_news("monkey")
37
37
  end
38
38
 
39
39
  it "should build the news objects" do
40
40
  Net::HTTP.stub!(:get_response).and_return{ mock_http_response }
41
41
  Boss::ResultFactory.should_receive(:build).with(Boss::SearchType::NEWS, '{"ysearchresponse":{}}')
42
42
 
43
- @api.search_news "monkey"
43
+ @api.search_news("monkey")
44
44
  end
45
45
  end
46
46
 
@@ -48,14 +48,14 @@ describe Boss::Api do
48
48
  it "should make a image request to yahoo service" do
49
49
  Net::HTTP.should_receive(:get_response).and_return{ mock_http_response }
50
50
 
51
- @api.search_images "hippo"
51
+ @api.search_images("hippo")
52
52
  end
53
53
 
54
54
  it "should build the image objects" do
55
55
  Net::HTTP.stub!(:get_response).and_return{ mock_http_response }
56
56
  Boss::ResultFactory.should_receive(:build).with(Boss::SearchType::IMAGES, '{"ysearchresponse":{}}')
57
57
 
58
- @api.search_images "hippo"
58
+ @api.search_images("hippo")
59
59
  end
60
60
  end
61
61
 
@@ -64,14 +64,14 @@ describe Boss::Api do
64
64
  it "should make a web request to yahoo service" do
65
65
  Net::HTTP.should_receive(:get_response).and_return{ mock_http_response }
66
66
 
67
- @api.search_web "monkey"
67
+ @api.search_web("monkey")
68
68
  end
69
69
 
70
70
  it "should build the web objects" do
71
71
  Net::HTTP.stub!(:get_response).and_return{ mock_http_response }
72
72
  Boss::ResultFactory.should_receive(:build).with(Boss::SearchType::WEB, '{"ysearchresponse":{}}')
73
73
 
74
- @api.search_web "monkey"
74
+ @api.search_web("monkey")
75
75
  end
76
76
 
77
77
  end
@@ -81,7 +81,7 @@ describe Boss::Api do
81
81
  it "should raise error on failed search" do
82
82
  Net::HTTP.stub!(:get_response).and_return{ mock_http_response :code => "404" }
83
83
 
84
- lambda { @api.search_web "monkey" }.should raise_error(Boss::BossError)
84
+ lambda { @api.search_web("monkey") }.should raise_error(Boss::BossError)
85
85
  end
86
86
 
87
87
  end
@@ -98,7 +98,7 @@ describe Boss::Api do
98
98
  @config.should_receive(:count=).with(1)
99
99
  Boss::Config.should_receive(:new).and_return(@config)
100
100
 
101
- result = @api.search_web "monkeys" do |setup|
101
+ result = @api.search_web("monkeys") do |setup|
102
102
  setup.count = 1
103
103
  end
104
104
  end
@@ -106,7 +106,7 @@ describe Boss::Api do
106
106
  it "should allow configuring through hash" do
107
107
  Boss::Config.should_receive(:new).with({:count => 1}).and_return(@config)
108
108
 
109
- @api.search_web "monkeys", :count => 1
109
+ @api.search_web("monkeys", :count => 1)
110
110
  end
111
111
 
112
112
  end
@@ -119,20 +119,20 @@ describe Boss::Api do
119
119
 
120
120
  it "should not return any objects when format is 'xml'" do
121
121
  Boss::ResultFactory.should_receive(:build).never
122
- @api.search_web "monkeys", :format => 'xml', :count => 1
122
+ @api.search_web("monkeys", :format => 'xml', :count => 1)
123
123
  end
124
124
 
125
125
  it "should not return any objects when format is 'json'" do
126
126
  Boss::ResultFactory.should_receive(:build).never
127
- @api.search_web "monkeys", :format => 'json', :count => 1
127
+ @api.search_web("monkeys", :format => 'json', :count => 1)
128
128
  end
129
129
 
130
130
  it "should raise an error invalid format" do
131
- lambda { @api.search_web "monkeys", :format => 'grilled_cheese', :count => 1 }.should raise_error(Boss::InvalidFormat)
131
+ lambda { @api.search_web("monkeys", :format => 'grilled_cheese', :count => 1) }.should raise_error(Boss::InvalidFormat)
132
132
  end
133
133
 
134
134
  it "should raise an error on invalid count" do
135
- lambda { @api.search_web "monkeys", :count => 0 }.should raise_error(Boss::InvalidConfig)
135
+ lambda { @api.search_web("monkeys", :count => 0) }.should raise_error(Boss::InvalidConfig)
136
136
  end
137
137
 
138
138
  end
@@ -142,7 +142,7 @@ describe Boss::Api do
142
142
  pending("fix for http://eshopworks.lighthouseapp.com/projects/15732/tickets/1")
143
143
  Net::HTTP.stub!(:get_response).and_return{ mock_http_response :code => "206" }
144
144
 
145
- lambda { @api.search_web "monkey" }.should_not raise_error(Boss::BossError)
145
+ lambda { @api.search_web("monkey") }.should_not raise_error(Boss::BossError)
146
146
  end
147
147
  end
148
148
 
@@ -152,7 +152,7 @@ describe Boss::Api do
152
152
  CGI.stub!(:escape)
153
153
  CGI.should_receive(:escape).with('monkey?magic').and_return('monkey%3Fmagic')
154
154
 
155
- @api.search_web "monkey?magic"
155
+ @api.search_web("monkey?magic")
156
156
  end
157
157
 
158
158
  end
@@ -19,13 +19,13 @@ describe Boss::Config do
19
19
  end
20
20
 
21
21
  it "should add custom values to url" do
22
- config = Boss::Config.new :mizaru => 'cannot_see'
22
+ config = Boss::Config.new(:mizaru => 'cannot_see')
23
23
 
24
24
  config.to_url.should include("&mizaru=cannot_see")
25
25
  end
26
26
 
27
27
  it "should encode invalid url characters" do
28
- config = Boss::Config.new :mizaru => 'dancing monkeys?'
28
+ config = Boss::Config.new(:mizaru => 'dancing monkeys?')
29
29
 
30
30
  config.to_url.should include("dancing+monkeys%3F")
31
31
  end
@@ -0,0 +1,23 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Boss::ResultCollection do
4
+
5
+ it "should dynamically set instance values given at creation" do
6
+ collection = Boss::ResultCollection.new({:totalhits => "344"})
7
+
8
+ collection.totalhits.should eql("344")
9
+ end
10
+
11
+ it "should allow iterating over result collection" do
12
+ collection = Boss::ResultCollection.new({})
13
+
14
+ collection << 1
15
+ collection << 2
16
+
17
+ collection.each do |value|
18
+ [1,2].member?(value).should be_true
19
+ end
20
+
21
+ end
22
+
23
+ end
@@ -2,16 +2,24 @@ require File.dirname(__FILE__) + '/../spec_helper'
2
2
 
3
3
  describe Boss::ResultFactory do
4
4
 
5
- news_json_result = '{"ysearchresponse":{"responsecode":"200","nextpage":"nextpage","totalhits":"344","count":"1","start":"0","resultset_news":[{"abstract":"abstract","clickurl":"clickurl","date":"2008\/08\/18","language":"en english","source":"source","sourceurl":"sourceurl","time":"09:17:29","title":"monkey_title","url":"url"}]}}'
5
+ news_json_result = '{"ysearchresponse":{"responsecode":"200","nextpage":"nextpage","totalhits":"344","count":"1","start":"0","resultset_news":[{"abstract":"abstract","clickurl":"clickurl","date":"2008\/08\/18","language":"en english","source":"source","sourceurl":"sourceurl","time":"09:17:29","title":"monkey_title","url":"url"}]}}'
6
6
 
7
- image_json_result = '{"ysearchresponse":{"responsecode":"200","nextpage":"http:\/\/www.example.com","totalhits":"80","count":"1","start":"0","resultset_images":[{"abstract":"more monkeys","clickurl":"http:\/\/www.example.con\/click","date":"2001\/12\/19","filename":"monkeys.jpg","format":"jpeg","height":"600","mimetype":"image\/jpeg","refererclickurl":"http:\/\/www.example.com\/ref","refererurl":"http:\/\/www.monkeys.com","size":"60900","thumbnail_height":"116","thumbnail_url":"http:\/\/monkey.com\/image\/25\/m7\/3918546506","thumbnail_width":"155","title":"monkeys.jpg","url":"http:\/\/monkey\/monkeys.jpg","width":"800"}]}}'
7
+ image_json_result = '{"ysearchresponse":{"responsecode":"200","nextpage":"http:\/\/www.example.com","totalhits":"80","count":"1","start":"0","resultset_images":[{"abstract":"more monkeys","clickurl":"http:\/\/www.example.con\/click","date":"2001\/12\/19","filename":"monkeys.jpg","format":"jpeg","height":"600","mimetype":"image\/jpeg","refererclickurl":"http:\/\/www.example.com\/ref","refererurl":"http:\/\/www.monkeys.com","size":"60900","thumbnail_height":"116","thumbnail_url":"http:\/\/monkey.com\/image\/25\/m7\/3918546506","thumbnail_width":"155","title":"monkeys.jpg","url":"http:\/\/monkey\/monkeys.jpg","width":"800"}]}}'
8
8
 
9
- web_json_result = '{"ysearchresponse":{"responsecode":"200","nextpage":"nextpage","totalhits":"344","count":"1","start":"0","resultset_web":[{"abstract":"abstract","clickurl":"clickurl","date":"2008\/08\/18","language":"en english","source":"source","sourceurl":"sourceurl","time":"09:17:29","title":"monkey_title","url":"url"}]}}'
10
-
11
- spelling_json_result = '{"ysearchresponse":{"responsecode":"200","totalhits":"1","count":"1","start":"0","resultset_spell":[{"suggestion":"giraffes"}]}}'
9
+ web_json_result = '{"ysearchresponse":{"responsecode":"200","nextpage":"nextpage","totalhits":"344","count":"1","start":"0","resultset_web":[{"abstract":"abstract","clickurl":"clickurl","date":"2008\/08\/18","language":"en english","source":"source","sourceurl":"sourceurl","time":"09:17:29","title":"monkey_title","url":"url"}]}}'
12
10
 
13
- error_json_result = '{"Error":"true"}'
11
+ spelling_json_result = '{"ysearchresponse":{"responsecode":"200","totalhits":"1","count":"1","start":"0","resultset_spell":[{"suggestion":"giraffes"}]}}'
14
12
 
13
+ error_json_result = '{"Error":"true"}'
14
+
15
+ it "should create a result collection" do
16
+ news_hash = JSON.parse(news_json_result)
17
+ result_collection = Boss::ResultCollection.new(news_hash)
18
+ Boss::ResultCollection.should_receive(:new).once.with(news_hash['ysearchresponse']).and_return(result_collection)
19
+
20
+ Boss::ResultFactory.build(Boss::SearchType::NEWS, news_json_result)
21
+ end
22
+
15
23
  it "should create a new news object from json" do
16
24
  Boss::Result::News.should_receive(:new).once
17
25
 
@@ -23,27 +31,27 @@ describe Boss::ResultFactory do
23
31
 
24
32
  news_results[0].title.should == "monkey_title"
25
33
  end
26
-
34
+
27
35
  it "should build image objects from json" do
28
36
  Boss::Result::Image.should_receive(:new).once
29
37
 
30
38
  Boss::ResultFactory.build(Boss::SearchType::IMAGES, image_json_result)
31
39
  end
32
-
40
+
33
41
  it "should build web object from json" do
34
42
  Boss::Result::Web.should_receive(:new).once
35
43
 
36
44
  Boss::ResultFactory.build(Boss::SearchType::WEB, web_json_result)
37
45
  end
38
-
46
+
39
47
  it "should build spelling object from json" do
40
48
  Boss::Result::Spell.should_receive(:new).once
41
-
49
+
42
50
  Boss::ResultFactory.build(Boss::SearchType::SPELL, spelling_json_result)
43
51
  end
44
-
52
+
45
53
  it "should raise an error if json result carries an error" do
46
54
  lambda { Boss::ResultFactory.build(Boss::SearchType::WEB, error_json_result) }.should raise_error(Boss::BossError)
47
55
  end
48
-
49
- end
56
+
57
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eshopworks-rboss
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Wilk
@@ -85,11 +85,13 @@ files:
85
85
  - lib/boss/result/news.rb
86
86
  - lib/boss/result/spell.rb
87
87
  - lib/boss/result/web.rb
88
+ - lib/boss/result_collection.rb
88
89
  - lib/boss/result_factory.rb
89
90
  - lib/boss/version.rb
90
91
  - rboss.gemspec
91
92
  - spec/boss/api_spec.rb
92
93
  - spec/boss/config_spec.rb
94
+ - spec/boss/result_collection_spec.rb
93
95
  - spec/boss/result_factory_spec.rb
94
96
  - spec/spec.opts
95
97
  - spec/spec_helper.rb