oos4ruby 0.1.7 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/oos4ruby.rb CHANGED
@@ -1,3 +1,6 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
1
4
  module Oos4ruby
2
5
  class UnknownUser < ArgumentError; end
3
6
 
@@ -32,7 +35,6 @@ module Oos4ruby
32
35
  TRUSTED_URL = API_URL + '/trusted'
33
36
  SEARCH_URL = API_URL + '/search'
34
37
  end
35
- %w(rubygems).each { |f| require f }
36
38
 
37
39
  require 'oos4ruby/http_invoker.rb'
38
40
  require 'oos4ruby/auth.rb'
data/lib/oos4ruby/bean.rb CHANGED
@@ -26,7 +26,7 @@ module Bean #:nodoc:
26
26
  end
27
27
 
28
28
  def set_variable(options = {})
29
- if options.empty?
29
+ unless options.empty?
30
30
  options.each_pair { |key, value|
31
31
  class_variable_set("@@#{key.to_s}".to_sym, value)
32
32
  }
@@ -197,4 +197,3 @@ module Bean #:nodoc:
197
197
  end
198
198
  end
199
199
  end
200
-
@@ -11,7 +11,7 @@ class Collection < Array
11
11
  @feed = feed
12
12
  @auth = auth
13
13
  @slug = slug
14
- evalued_class = eval(self.class.name.gsub(/s$/, ''))
14
+ evalued_class = Oos4ruby.const_get(self.class.name.gsub(/s$/, ''))
15
15
  @feed.entries.each {|entry| self << evalued_class.new(entry, @auth, @slug) }
16
16
  end
17
17
 
@@ -111,7 +111,7 @@ class Collection < Array
111
111
  :content_type => file content type
112
112
  =end
113
113
  def create!(opts = {})
114
- evalued_class = eval(self.class.name.gsub(/s$/, ''))
114
+ evalued_class = Oos4ruby.const_get(self.class.name.gsub(/s$/, ''))
115
115
  body = evalued_class.dump! opts, @slug
116
116
  unless self.instance_of?Medias
117
117
  body = "<?xml version='1.0' ?>\n" + body.to_s
@@ -119,14 +119,13 @@ class Collection < Array
119
119
 
120
120
  poster = HTTPInvoker.new( @feed.self_link, @auth)
121
121
 
122
- content_type = opts[:content_type] || AtomEntryContentType
123
- opts.reject {|k, v| k == :content_type}
122
+ content_type = opts.delete(:content_type) || AtomEntryContentType
124
123
  opts.each {|k, v| poster.set_header(k, v)}
125
124
 
126
125
  worked = poster.post content_type, body
127
126
 
128
- raise RuntimeError.new unless worked
129
- return evalued_class.new(poster.entry, @auth, @slug)
127
+ raise poster.response.message unless worked
128
+ return get_page(@feed.self_link, @auth).first
130
129
  end
131
130
  =begin rdoc
132
131
  return true if the text of the element is equals to the given value
@@ -163,8 +162,7 @@ class Collection < Array
163
162
  end
164
163
 
165
164
  def convert( body )
166
- evalued_class = eval(self.class.name)
167
- return evalued_class.new( Feed.read( body ), @auth, @slug )
165
+ return self.class.new( Feed.read( body ), @auth, @slug )
168
166
  end
169
167
 
170
168
  end
@@ -118,7 +118,7 @@ class HTTPInvoker
118
118
  unless req
119
119
  url = @uri.path
120
120
  url += "?#{@uri.query}" if @uri.query
121
- req = eval("Net::HTTP::#{option.to_s.capitalize}").new( url )
121
+ req = Net::HTTP.const_get(option.to_s.capitalize).new( url )
122
122
  end
123
123
 
124
124
  @authent.add_to req
@@ -141,4 +141,4 @@ class HTTPInvoker
141
141
  end
142
142
 
143
143
  end
144
- end
144
+ end
@@ -14,12 +14,8 @@ module Oos4ruby
14
14
  end
15
15
 
16
16
  def Media.dump!(opts, slug)
17
- if (opts[:file].instance_of?File)
18
- file = opts[:file]
19
- elsif (opts[:file].instance_of?String)
20
- file = File.new( opts[:file ])
21
- end
22
- return file
17
+ file = opts.delete(:file)
18
+ file.respond_to?(:path) ? file : File.new(file)
23
19
  end
24
20
 
25
21
  private
@@ -1,4 +1,3 @@
1
-
2
1
  require 'cgi'
3
2
 
4
3
  module Oos4ruby
@@ -20,55 +19,29 @@ class Search
20
19
  def Search.find(auth, opts)
21
20
  query = SEARCH_URL
22
21
 
23
- more_params = false
24
- unless opts.empty?
25
- query += "?"
26
- if opts[:q]
27
- query += "q=#{CGI.escape(opts[:q])}"
28
- more_params = true
29
- end
30
- if opts[:bbox]
31
- query += "&" if more_params
32
- query += "bbox=#{opts[:bbox].join(",")}"
33
- more_params = true
34
- end
35
- if opts[:as]
36
- query += "&" if more_params
37
- query += "as=#{opts[:as]}"
38
- more_params = true
39
- end
40
- if opts[:tags]
41
- query += "&" if more_params
42
- opts[:tags].each_with_index do |tag, index|
43
- query += "tag=#{CGI.escape(tag)}"
44
- query += "&" if index < opts[:tags].length - 1
45
- end
46
- more_params = true
47
- end
48
- if opts[:tag_op]
49
- query += "&" if more_params
50
- query += "tagOp=#{opts[:tag_op]}"
51
- more_params = true
52
- end
53
- if opts[:lat] and opts[:lon] and opts[:radius]
54
- query += "&" if more_params
55
- query += "lat=#{opts[:lat]}&lon=#{opts[:lon]}&radius=#{opts[:radius]}"
56
- more_params = true
57
- end
58
- if opts[:page]
59
- query += "&" if more_params
60
- query += "page=#{opts[:page]}"
61
- end
62
- else
63
- raise RuntimeError('a parameter is obligatory at least')
64
- end
65
-
66
- getter = HTTPInvoker.new query, auth
22
+ getter = HTTPInvoker.new parse_params(opts), auth
67
23
 
68
24
  worked = getter.get
69
25
  raise RuntimeError.new(getter.response.message) unless worked
70
26
  Sites.new(Feed.read(getter.body), auth, nil)
71
27
  end
28
+
29
+ def Search.parse_params(args)
30
+ raise "at least a parameter is mandatory" if args.nil? || args.empty?
31
+
32
+ args[:q] = CGI.escape(args[:q]) if args.include?(:q)
33
+ args[:bbox] = args[:bbox].join(",") if args.include?(:bbox)
34
+ args[:tag] = args.delete(:tags).map {|tag| CGI.escape(tag)} if args.include?(:tags)
35
+ args[:tagOp] = args.delete(:tag_op) if args.include?(:tag_op)
36
+
37
+ args.map do |k, v|
38
+ if v.respond_to?(:each_with_index)
39
+ v.map {|m| "#{k}=#{m}"}
40
+ else
41
+ "#{k}=#{v}"
42
+ end
43
+ end.flatten.join("&")
44
+ end
72
45
  end
73
46
  end
74
47
 
data/lib/oos4ruby/site.rb CHANGED
@@ -61,60 +61,57 @@ class Site < Bean::BeanClass
61
61
 
62
62
  add_element entry, 'updated', updated
63
63
  add_element entry, 'id', make_id
64
- add_element entry, 'oos:id', opts[:id] if opts[:id]
65
- add_element entry, 'title', opts[:title] if opts[:title]
66
- add_element entry, 'oos:useraddress', opts[:user_address] if opts[:user_address]
67
- add_element entry, 'oos:url', opts[:url] if opts[:url]
68
- add_element entry, 'oos:telephone', opts[:telephone] if opts[:telephone]
69
- add_element entry, 'summary', opts[:review_title] if opts[:review_title]
70
- add_element entry, 'summary', opts[:summary] if opts[:summary] and REXML::XPath.first(entry, 'summary', XmlNamespaces).nil?
71
-
72
- if opts[:privacy]
73
- if opts[:privacy].is_a?REXML::Element
74
- entry.add_element opts[:privacy]
75
- elsif opts[:privacy].is_a?String
76
- add_category entry, opts[:privacy], PRIVACY_URL
64
+
65
+ {:id => 'oos:id', :title => 'title', :user_address => 'oos:useraddress',
66
+ :url => 'oos:url', :telephone => 'oos:telephone', :review_title => 'summary'}.each do |k, v|
67
+ add_element entry, v, opts.delete(k) if opts.include?(k)
68
+ end
69
+
70
+ add_element entry, 'summary', opts.delete(:summary) if opts.include?(:summary) and REXML::XPath.first(entry, 'summary', XmlNamespaces).nil?
71
+
72
+ if opts.include?(:privacy)
73
+ privacy = opts.delete(:privacy)
74
+ if privacy.is_a?REXML::Element
75
+ entry.add_element privacy
76
+ elsif privacy.is_a?String
77
+ add_category entry, privacy, PRIVACY_URL
77
78
  end
78
79
  end
79
80
 
80
- if opts[:review_content] || opts[:content]
81
- content = opts[:review_content] || opts[:content]
81
+ if opts.include?(:review_content) || opts.include?(:content)
82
+ content = opts.delete(:review_content) || opts.delete(:content)
82
83
  attrs = {}
83
- text = nil
84
- if content.is_a?String
85
- text = content
86
- elsif content.is_a?Hash
87
- text = content[:text]
88
- content.delete :text
84
+ text = content
85
+ if content.respond_to?(:each_pair)
86
+ text = content.delete(:text)
89
87
  content.each_pair {|key, value| attrs[key.to_s] = value}
90
88
  end
91
89
  add_element entry, 'content', text, attrs
92
90
  end
93
91
 
94
- if opts[:locality]
95
- name = nil
92
+ if opts.include?(:locality)
93
+ locality = opts.delete(:locality)
94
+ name = locality
96
95
  local_slug = nil
97
96
  attrs = {}
98
- if opts[:locality].is_a?Hash
99
- name = opts[:locality][:name]
100
- local_slug = opts[:locality][:slug]
101
- elsif opts[:locality].is_a?String
102
- name = opts[:locality]
97
+ if locality.respond_to?(:each_pair)
98
+ name = locality[:name]
99
+ local_slug = locality[:slug]
103
100
  end
104
101
  attrs['slug'] = local_slug if local_slug
105
102
  add_element entry, 'oos:locality', name, attrs
106
103
  end
107
104
 
108
- if opts[:country]
109
- name = opts[:country][:name]
110
- opts[:country].delete :name
105
+ if opts.include?(:country)
106
+ country = opts.delete(:country)
107
+ name = country.delete(:name)
111
108
  attrs = {}
112
- opts[:country].each_pair {|key, value| attrs[key.to_s] = value}
109
+ country.each_pair {|key, value| attrs[key.to_s] = value}
113
110
  add_element entry, 'oos:country', name, attrs
114
111
  end
115
112
 
116
- opts[:tags].each { |tag| add_category entry, tag, "#{TAGS_URL}/#{slug}"} if opts[:tags]
117
- opts[:lists].each { |list| add_category entry, list, "#{LISTS_URL}/#{slug}"} if opts[:lists]
113
+ opts.delete(:tags).each { |tag| add_category entry, tag, "#{TAGS_URL}/#{slug}"} if opts.include(:tags)
114
+ opts.delete(:lists).each { |list| add_category entry, list, "#{LISTS_URL}/#{slug}"} if opts.include?(:lists)
118
115
 
119
116
  entry
120
117
  end
@@ -172,9 +169,7 @@ class Site < Bean::BeanClass
172
169
  end
173
170
 
174
171
  def privacy
175
- return @privacy if @privacy
176
- @privacy = @xml.category({:scheme => PRIVACY_URL}).attributes['term']
177
- @privacy
172
+ @privacy ||= @xml.category({:scheme => PRIVACY_URL}).attributes['term']
178
173
  end
179
174
 
180
175
  def privacy=(privacy)
@@ -187,7 +182,7 @@ class Site < Bean::BeanClass
187
182
  getter = HTTPInvoker.new href, @auth
188
183
  worked = getter.get
189
184
 
190
- raise RuntimeError unless worked
185
+ raise getter.response.message unless worked
191
186
  Medias.new(Feed.read(getter.body), @auth, @slug)
192
187
  end
193
188
 
@@ -1,8 +1,8 @@
1
1
  module Oos4ruby #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
- MINOR = 1
5
- TINY = 7
4
+ MINOR = 2
5
+ TINY = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -0,0 +1,34 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class TestSearch < Test::Unit::TestCase
4
+
5
+ VALID_PARAMS = {
6
+ :q => 'gloria',
7
+ :tags => ['madrid', 'barcelona'],
8
+ :as => '/es',
9
+ :bbox => ['23452345', '345325', '23434', '32453245'],
10
+ :tag_op => 'and',
11
+ :lon => '2345',
12
+ :lat => '234234',
13
+ :radius => '3'
14
+ }
15
+
16
+ def test_parse_params_without_arguments
17
+ assert_raise(RuntimeError) { Oos4ruby::Search.parse_params(nil) }
18
+ assert_raise(RuntimeError) { Oos4ruby::Search.parse_params({}) }
19
+ end
20
+
21
+ def test_parse_params_with_arguments
22
+ query = Oos4ruby::Search.parse_params(VALID_PARAMS)
23
+
24
+ assert query.include?("q=gloria")
25
+ assert query.include?("tag=madrid")
26
+ assert query.include?("tag=barcelona")
27
+ assert query.include?("as=/es")
28
+ assert query.include?("bbox=23452345,345325,23434,32453245")
29
+ assert query.include?("tagOp=and")
30
+ assert query.include?("lon=2345")
31
+ assert query.include?("lat=234234")
32
+ assert query.include?("radius=3")
33
+ end
34
+ end
data/website/index.html CHANGED
@@ -60,13 +60,13 @@
60
60
  </pre></p>
61
61
  <p>How to add a review from a site that doesn&#8217;t exist in 11870:</p>
62
62
  <p><pre class='syntax'>
63
- <span class="ident">oos</span><span class="punct">.</span><span class="ident">user</span><span class="punct">.</span><span class="ident">sites</span><span class="punct">.</span><span class="ident">create!</span> <span class="punct">{</span><span class="symbol">:title</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">this site doesn</span><span class="punct">'</span><span class="ident">t</span> <span class="ident">exist</span><span class="punct">'</span><span class="string">,
64
- :user_address =&gt; </span><span class="punct">'</span><span class="ident">the</span> <span class="ident">address</span><span class="punct">'</span><span class="string">,
65
- :locality =&gt; {:name =&gt; </span><span class="punct">'</span><span class="constant">Madrid</span><span class="punct">'</span><span class="string">, :slug =&gt; </span><span class="punct">'/</span><span class="ident">es</span><span class="punct">/</span><span class="ident">madrid</span><span class="punct">'</span><span class="string">},
66
- :country =&gt; {:name =&gt; </span><span class="punct">'</span><span class="constant">Españ</span><span class="ident">a</span><span class="punct">'</span><span class="string">, :slug =&gt; </span><span class="punct">'/</span><span class="ident">es</span><span class="punct">'</span><span class="string">}
67
- :summary =&gt; </span><span class="punct">'</span><span class="ident">review</span> <span class="ident">title</span><span class="punct">'</span><span class="string">, :content =&gt; </span><span class="punct">'</span><span class="ident">review</span> <span class="ident">content</span><span class="punct">'</span><span class="string">
68
- :tags =&gt; [</span><span class="punct">'</span><span class="ident">tapas</span><span class="punct">'</span><span class="string">, </span><span class="punct">'</span><span class="ident">tipical</span> <span class="ident">spanish</span><span class="punct">'</span><span class="string">], :lists =&gt; [</span><span class="punct">'</span><span class="ident">food</span><span class="punct">'</span><span class="string">]}<span class="normal">
69
- </span></span></pre></p>
63
+ <span class="ident">oos</span><span class="punct">.</span><span class="ident">user</span><span class="punct">.</span><span class="ident">sites</span><span class="punct">.</span><span class="ident">create!</span> <span class="punct">{</span><span class="symbol">:title</span> <span class="punct">=&gt;</span> <span class="punct">&quot;</span><span class="string">this site doesn't exist</span><span class="punct">&quot;,</span>
64
+ <span class="symbol">:user_address</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">the address</span><span class="punct">',</span>
65
+ <span class="symbol">:locality</span> <span class="punct">=&gt;</span> <span class="punct">{</span><span class="symbol">:name</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">Madrid</span><span class="punct">',</span> <span class="symbol">:slug</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">/es/madrid</span><span class="punct">'},</span>
66
+ <span class="symbol">:country</span> <span class="punct">=&gt;</span> <span class="punct">{</span><span class="symbol">:name</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">España</span><span class="punct">',</span> <span class="symbol">:slug</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">/es</span><span class="punct">'}</span>
67
+ <span class="symbol">:summary</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">review title</span><span class="punct">',</span> <span class="symbol">:content</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">review content</span><span class="punct">'</span>
68
+ <span class="symbol">:tags</span> <span class="punct">=&gt;</span> <span class="punct">['</span><span class="string">tapas</span><span class="punct">',</span> <span class="punct">'</span><span class="string">tipical spanish</span><span class="punct">'],</span> <span class="symbol">:lists</span> <span class="punct">=&gt;</span> <span class="punct">['</span><span class="string">food</span><span class="punct">']}</span>
69
+ </pre></p>
70
70
  <p>How to add a review from a site that exists in 11870:</p>
71
71
  <p><pre class='syntax'>
72
72
  <span class="ident">oos</span><span class="punct">.</span><span class="ident">user</span><span class="punct">.</span><span class="ident">sites</span><span class="punct">.</span><span class="ident">create!</span> <span class="punct">{</span><span class="symbol">:id</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">11870 site id</span><span class="punct">',</span>
@@ -76,6 +76,13 @@
76
76
  <p><pre class='syntax'><span class="ident">contacts</span> <span class="punct">=</span> <span class="ident">user</span><span class="punct">.</span><span class="ident">contacts</span></pre></p>
77
77
  <p>How to obtain the multimedia feed associated with a review:</p>
78
78
  <p><pre class='syntax'><span class="ident">media_collection</span> <span class="punct">=</span> <span class="ident">user</span><span class="punct">.</span><span class="ident">sites</span><span class="punct">[</span><span class="number">0</span><span class="punct">].</span><span class="ident">multimedia</span></pre></p>
79
+ <p>How to add a new image or video to a review:</p>
80
+ <p><pre class='syntax'><span class="ident">user</span><span class="punct">.</span><span class="ident">sites</span><span class="punct">[</span><span class="number">0</span><span class="punct">].</span><span class="ident">multimedia</span><span class="punct">.</span><span class="ident">create!</span> <span class="punct">{</span>
81
+ <span class="symbol">:file</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">image.png</span><span class="punct">',</span>
82
+ <span class="symbol">:content_length</span> <span class="punct">=&gt;</span> <span class="constant">File</span><span class="punct">.</span><span class="ident">size</span><span class="punct">('</span><span class="string">image.png</span><span class="punct">'),</span>
83
+ <span class="symbol">:content_type</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">image/png</span><span class="punct">'</span>
84
+ <span class="punct">}</span>
85
+ </pre></p>
79
86
  <h2>Forum</h2>
80
87
  <p><a href="http://groups.google.com/group/api-11870">http://groups.google.com/group/api-11870</a></p>
81
88
  <h2>How to submit patches</h2>
@@ -86,7 +93,7 @@
86
93
  <h2>Contact</h2>
87
94
  <p>Comments are welcome. Send an email to <a href="mailto:david.calavera@gmail.com">David Calavera</a> email via the <a href="http://groups.google.com/group/api-11870">forum</a></p>
88
95
  <p class="coda">
89
- <a href="FIXME email">David Calavera</a>, 16th March 2009<br>
96
+ <a href="FIXME email">David Calavera</a>, 19th November 2009<br>
90
97
  Theme extended from <a href="http://rb2js.rubyforge.org/">Paul Battley</a>
91
98
  </p>
92
99
  </div>
data/website/index.txt CHANGED
@@ -43,7 +43,7 @@ sites = user.sites
43
43
  How to add a review from a site that doesn't exist in 11870:
44
44
 
45
45
  <pre syntax="ruby">
46
- oos.user.sites.create! {:title => 'this site doesn't exist',
46
+ oos.user.sites.create! {:title => "this site doesn't exist",
47
47
  :user_address => 'the address',
48
48
  :locality => {:name => 'Madrid', :slug => '/es/madrid'},
49
49
  :country => {:name => 'España', :slug => '/es'}
@@ -66,6 +66,15 @@ How to obtain the multimedia feed associated with a review:
66
66
 
67
67
  <pre syntax="ruby">media_collection = user.sites[0].multimedia</pre>
68
68
 
69
+ How to add a new image or video to a review:
70
+
71
+ <pre syntax="ruby">user.sites[0].multimedia.create! {
72
+ :file => 'image.png',
73
+ :content_length => File.size('image.png'),
74
+ :content_type => 'image/png'
75
+ }
76
+ </pre>
77
+
69
78
  h2. Forum
70
79
 
71
80
  "http://groups.google.com/group/api-11870":http://groups.google.com/group/api-11870
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oos4ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Calavera
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-16 00:00:00 +01:00
12
+ date: 2009-11-19 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -30,7 +30,7 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 1.8.0
33
+ version: 2.3.3
34
34
  version:
35
35
  description: oos4ruby is a ruby binding for the 11870 API
36
36
  email: david.calavera@gmail.com
@@ -88,6 +88,8 @@ files:
88
88
  - website/template.rhtml
89
89
  has_rdoc: true
90
90
  homepage: http://oos4r.rubyforge.org
91
+ licenses: []
92
+
91
93
  post_install_message:
92
94
  rdoc_options:
93
95
  - --main
@@ -109,10 +111,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
111
  requirements: []
110
112
 
111
113
  rubyforge_project: oos4r
112
- rubygems_version: 1.3.1
114
+ rubygems_version: 1.3.5
113
115
  signing_key:
114
- specification_version: 2
116
+ specification_version: 3
115
117
  summary: oos4ruby is a ruby binding for the 11870 API
116
118
  test_files:
117
119
  - test/test_helper.rb
120
+ - test/test_search.rb
118
121
  - test/test_oos4ruby.rb