marauder 0.1.1.2 → 0.1.1.3

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.
Files changed (5) hide show
  1. data/README.rdoc +10 -2
  2. data/Rakefile +1 -1
  3. data/lib/marauder.rb +72 -47
  4. data/marauder.gemspec +2 -2
  5. metadata +4 -4
@@ -16,10 +16,18 @@ Check out the Google Base API Documentation for more info: http://code.google.co
16
16
 
17
17
  Marauder.search(query)
18
18
 
19
- Returns an array of OpenStruct objects with getters for all data provided by the Google Base.
19
+ Or:
20
+
21
+ Marauder::SearchRequest.new(query).process
22
+
23
+ Returns an array of Results containing all data provided by Google Base.
20
24
 
21
25
  Marauder.find_by_google_id(google_id)
26
+
27
+ Or:
28
+
29
+ Marauder::ItemRequest.new(google_id).process
22
30
 
23
- Returns OpenStruct object of object.
31
+ Returns Result.
24
32
 
25
33
 
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('marauder', '0.1.1.2') do |p|
5
+ Echoe.new('marauder', '0.1.1.3') do |p|
6
6
  p.description = "Wrapper for the Google Base product search API."
7
7
  p.url = "http://github.com/samueljamesbell/marauder"
8
8
  p.author = "Sam Bell"
@@ -1,66 +1,91 @@
1
+ require 'net/http'
2
+ require 'rexml/document'
3
+
1
4
  module Marauder
2
- require 'net/http'
3
- require 'rexml/document'
4
- require 'ostruct'
5
-
6
5
 
7
- def self.search(query)
8
- formatted_query = query.gsub(/[ ]/, '+')
9
- url = "http://www.google.com/base/feeds/snippets?bq=#{formatted_query}"
10
- xml_data = Net::HTTP.get_response(URI.parse(url)).body
11
-
12
- self.process_xml(xml_data)
6
+ def self.search(query, params = {})
7
+ extra_params = []
8
+ unless params.nil?
9
+ params.each {|attribute, value| extra_params << "[#{attribute}:#{value}]"}
10
+ end
11
+ SearchRequest.new(query, extra_params).process
13
12
  end
14
-
15
- def self.search_by_attribute(query, attribute, value)
16
- formatted_query = query.gsub(/[ ]/, '+')
17
- url = "http://www.google.com/base/feeds/snippets?q=#{formatted_query}&bq=[#{attribute}:#{value}]"
18
- xml_data = Net::HTTP.get_response(URI.parse(url)).body
19
-
20
- self.process_xml(xml_data)
13
+
14
+ def self.find_by_google_id(google_id)
15
+ ItemRequest.new(google_id).process
21
16
  end
22
17
 
23
- def self.find_by_google_id(google_id)
24
- xml_data = Net::HTTP.get_response(URI.parse(google_id)).body
25
- doc = REXML::Document.new(xml_data)
18
+ class ItemRequest
19
+ attr_accessor :url
26
20
 
27
- entry = doc.elements['entry']
21
+ def initialize(google_id)
22
+ self.url = google_id
23
+ end
28
24
 
29
- self.create_ostruct(entry)
25
+ def process
26
+ xml_data = Net::HTTP.get_response(URI.parse(url)).body
27
+ doc = REXML::Document.new(xml_data)
28
+ entry = doc.elements['entry']
29
+
30
+ Result.new(entry)
31
+ end
30
32
  end
31
-
32
- private
33
-
34
- def self.process_xml(xml_data)
35
- doc = REXML::Document.new(xml_data)
36
33
 
37
- entries = []
38
- doc.elements.each('feed/entry') {|element| entries << element}
34
+ class SearchRequest
35
+ attr_accessor :url
39
36
 
40
- results = []
41
- entries.each do |entry|
42
- r = self.create_ostruct(entry)
43
- results << r
37
+ def initialize(query, extra_params = [])
38
+ formatted_query = format_query(query)
39
+ if extra_params.empty?
40
+ self.url = "http://www.google.com/base/feeds/snippets?q=#{formatted_query}"
41
+ else
42
+ formatted_params = extra_params.join(",")
43
+ self.url = "http://www.google.com/base/feeds/snippets?q=#{formatted_query}&bq=#{formatted_params}"
44
+ end
44
45
  end
45
46
 
46
- return results
47
+ def process
48
+ xml_data = Net::HTTP.get_response(URI.parse(url)).body
49
+ doc = REXML::Document.new(xml_data)
50
+
51
+ results = []
52
+ doc.elements.each("feed/entry") do |entry|
53
+ results << Result.new(entry)
54
+ end
55
+
56
+ results
57
+ end
58
+
59
+ private
60
+
61
+ def format_query(query)
62
+ return query.gsub(/[ ]/, '+')
63
+ end
64
+
47
65
  end
48
66
 
49
- def self.create_ostruct(entry)
50
- r = OpenStruct.new
51
- entry.elements.each do |element|
52
- if element.name == 'id'
53
- r.google_id = entry.elements['id'].text
54
- elsif element.name.gsub(/g:/,'') == 'author'
55
- r.author_name = entry.elements['author'].elements['name'].text
56
- else
57
- m = element.name.gsub(/g:/,'').to_s
58
- method = m + "="
59
- r.send method.to_sym, element.text
67
+ class Result
68
+ attr_accessor :hash
69
+
70
+ def initialize(entry)
71
+ self.hash = {}
72
+
73
+ entry.elements.each do |element|
74
+ case element.name
75
+ when /id/
76
+ hash['google_id'] = entry.elements['id'].text
77
+ when 'g:author'
78
+ hash['author_name'] = entry.elements['author'].elements['name'].text
79
+ else
80
+ hash[element.name.gsub(/g:/, '')] = element.text
81
+ end
60
82
  end
61
83
  end
62
-
63
- return r
84
+
85
+ def method_missing(method, *args)
86
+ puts hash.inspect
87
+ hash.has_key?(method.to_s) ? hash[method.to_s] : super
88
+ end
64
89
  end
65
90
 
66
91
  end
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{marauder}
5
- s.version = "0.1.1.2"
5
+ s.version = "0.1.1.3"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Sam Bell"]
9
- s.date = %q{2010-07-22}
9
+ s.date = %q{2010-07-26}
10
10
  s.description = %q{Wrapper for the Google Base product search API.}
11
11
  s.email = %q{samueljamesbell@gmail.com}
12
12
  s.extra_rdoc_files = ["CHANGELOG", "README.rdoc", "lib/marauder.rb"]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: marauder
3
3
  version: !ruby/object:Gem::Version
4
- hash: 71
4
+ hash: 69
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
9
  - 1
10
- - 2
11
- version: 0.1.1.2
10
+ - 3
11
+ version: 0.1.1.3
12
12
  platform: ruby
13
13
  authors:
14
14
  - Sam Bell
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-07-22 00:00:00 +01:00
19
+ date: 2010-07-26 00:00:00 +01:00
20
20
  default_executable:
21
21
  dependencies: []
22
22