partigirb 0.2.7

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.
@@ -0,0 +1,99 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <feed xmlns="http://www.w3.org/2005/Atom">
3
+ <title>Partigi</title>
4
+ <id>http://localhost:3000/films/pulp-fiction</id>
5
+ <updated>2009-07-17T07:18:27Z</updated>
6
+ <link type="application/atom+xml" href="http://localhost:3000/api/v1/items/show.atom?id=pulp-fiction&amp;type=film" rel="self"/>
7
+ <link type="text/html" href="http://localhost:3000/films/pulp-fiction" rel="alternate"/>
8
+ <author>
9
+ <name>Partigi</name>
10
+ </author>
11
+ <entry xmlns:ptPerson="http://schemas.partigi.com/v1.0/ptPerson" xmlns:ptItem="http://schemas.partigi.com/v1.0/ptItem">
12
+ <category scheme="http://schemas.partigi.com/v1.0#kind" term="http://schemas.partigi.com/v1.0#item"/>
13
+ <id>http://localhost:3000/films/pulp-fiction</id>
14
+ <title>Pulp Fiction</title>
15
+ <updated>2009-07-17T07:18:27Z</updated>
16
+ <published>2009-03-31T18:46:15Z</published>
17
+ <link type="text/html" rel="alternate" href="http://localhost:3000/films/pulp-fiction"/>
18
+ <link type="application/atom+xml" rel="self" href="http://localhost:3000/api/v1/items/show.atom?id=pulp-fiction&amp;type=film"/>
19
+ <ptItem:id>50</ptItem:id>
20
+ <ptItem:type>film</ptItem:type>
21
+ <ptItem:year>1994</ptItem:year>
22
+ <ptItem:title lang="es">Pulp Fiction</ptItem:title>
23
+ <ptItem:country>USA</ptItem:country>
24
+ <ptItem:posters>
25
+ <ptItem:poster type="image/jpeg" lang="" size="thumb" href="http://s3.amazonaws.com/partigidev/films/posters/251/pulp-fiction_thumb.jpg"/>
26
+ </ptItem:posters>
27
+ <ptItem:genre lang="es" name="comedia"/>
28
+ <ptItem:credits>
29
+ <ptPerson:entry rel="director">
30
+ <ptPerson:id>733</ptPerson:id>
31
+ <ptPerson:name>Quentin Tarantino</ptPerson:name>
32
+ </ptPerson:entry>
33
+ <ptPerson:entry rel="writer">
34
+ <ptPerson:id>733</ptPerson:id>
35
+ <ptPerson:name>Quentin Tarantino</ptPerson:name>
36
+ </ptPerson:entry>
37
+ <ptPerson:entry rel="cast_member">
38
+ <ptPerson:id>734</ptPerson:id>
39
+ <ptPerson:name>John Travolta</ptPerson:name>
40
+ </ptPerson:entry>
41
+ <ptPerson:entry rel="cast_member">
42
+ <ptPerson:id>735</ptPerson:id>
43
+ <ptPerson:name>Samuel L. Jackson</ptPerson:name>
44
+ </ptPerson:entry>
45
+ <ptPerson:entry rel="cast_member">
46
+ <ptPerson:id>736</ptPerson:id>
47
+ <ptPerson:name>Tim Roth</ptPerson:name>
48
+ </ptPerson:entry>
49
+ <ptPerson:entry rel="cast_member">
50
+ <ptPerson:id>737</ptPerson:id>
51
+ <ptPerson:name>Amanda Plummer</ptPerson:name>
52
+ </ptPerson:entry>
53
+ <ptPerson:entry rel="cast_member">
54
+ <ptPerson:id>738</ptPerson:id>
55
+ <ptPerson:name>Eric Stoltz</ptPerson:name>
56
+ </ptPerson:entry>
57
+ <ptPerson:entry rel="cast_member">
58
+ <ptPerson:id>739</ptPerson:id>
59
+ <ptPerson:name>Bruce Willis</ptPerson:name>
60
+ </ptPerson:entry>
61
+ <ptPerson:entry rel="cast_member">
62
+ <ptPerson:id>499</ptPerson:id>
63
+ <ptPerson:name>Ving Rhames</ptPerson:name>
64
+ </ptPerson:entry>
65
+ <ptPerson:entry rel="cast_member">
66
+ <ptPerson:id>740</ptPerson:id>
67
+ <ptPerson:name>Phil LaMarr</ptPerson:name>
68
+ </ptPerson:entry>
69
+ <ptPerson:entry rel="cast_member">
70
+ <ptPerson:id>741</ptPerson:id>
71
+ <ptPerson:name>Maria de Medeiros</ptPerson:name>
72
+ </ptPerson:entry>
73
+ <ptPerson:entry rel="cast_member">
74
+ <ptPerson:id>742</ptPerson:id>
75
+ <ptPerson:name>Rosanna Arquette</ptPerson:name>
76
+ </ptPerson:entry>
77
+ <ptPerson:entry rel="cast_member">
78
+ <ptPerson:id>743</ptPerson:id>
79
+ <ptPerson:name>Peter Greene</ptPerson:name>
80
+ </ptPerson:entry>
81
+ <ptPerson:entry rel="cast_member">
82
+ <ptPerson:id>744</ptPerson:id>
83
+ <ptPerson:name>Uma Thurman</ptPerson:name>
84
+ </ptPerson:entry>
85
+ <ptPerson:entry rel="cast_member">
86
+ <ptPerson:id>745</ptPerson:id>
87
+ <ptPerson:name>Duane Whitaker</ptPerson:name>
88
+ </ptPerson:entry>
89
+ <ptPerson:entry rel="cast_member">
90
+ <ptPerson:id>746</ptPerson:id>
91
+ <ptPerson:name>Paul Calderon</ptPerson:name>
92
+ </ptPerson:entry>
93
+ <ptPerson:entry rel="cast_member">
94
+ <ptPerson:id>747</ptPerson:id>
95
+ <ptPerson:name>Frank Whaley</ptPerson:name>
96
+ </ptPerson:entry>
97
+ </ptItem:credits>
98
+ </entry>
99
+ </feed>
@@ -0,0 +1,7 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class JSONHandlerTest < Test::Unit::TestCase
4
+ def test_nothing
5
+ assert true
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ #Used for mocking HTTP requests
2
+ class Net::HTTP
3
+ class << self
4
+ attr_accessor :response, :request, :last_instance
5
+ end
6
+
7
+ def request(req)
8
+ self.class.last_instance = self
9
+ self.class.request = req
10
+ self.class.response
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ #Mock responses that conform mostly to HTTPResponse's interface
2
+ class MockResponse
3
+ include Net::HTTPHeader
4
+ attr_accessor :code, :body
5
+ def initialize(code,body,headers={})
6
+ self.code = code
7
+ self.body = body
8
+ headers.each do |name, value|
9
+ self[name] = value
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,15 @@
1
+ #Transport that collects info on requests and responses for testing purposes
2
+ class MockTransport < Partigirb::Transport
3
+ attr_accessor :status, :body, :method, :url, :options
4
+
5
+ def initialize(status,body,headers={})
6
+ Net::HTTP.response = MockResponse.new(status,body,headers)
7
+ end
8
+
9
+ def request(method, string_url, options)
10
+ self.method = method
11
+ self.url = URI.parse(string_url)
12
+ self.options = options
13
+ super(method,string_url,options)
14
+ end
15
+ end
@@ -0,0 +1,47 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+ require 'builder'
6
+ require 'ruby-debug'
7
+
8
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
10
+ require 'partigirb'
11
+
12
+ Dir.glob('test/mocks/*_mock.rb').each { |e| require e }
13
+
14
+ class Test::Unit::TestCase
15
+ def new_client(status=200, response_body='', client_opts={})
16
+ client = Partigirb::Client.new(client_opts)
17
+ client.transport = MockTransport.new(status,response_body)
18
+ client
19
+ end
20
+
21
+ def request_query
22
+ if Net::HTTP.request && !Net::HTTP.request.path.nil? && !Net::HTTP.request.path.empty?
23
+ Net::HTTP.request.path.split(/\?/)[1].split('&')
24
+ else
25
+ nil
26
+ end
27
+ end
28
+
29
+ def post_data
30
+ Net::HTTP.request.body.split('&')
31
+ end
32
+
33
+ def build_xml_string(&block)
34
+ s = String.new
35
+ xml = Builder::XmlMarkup.new(:target => s)
36
+ yield(xml)
37
+ s
38
+ end
39
+
40
+ def load_fixture(name, format = :xml)
41
+ name += '.xml' if format.to_s == 'xml'
42
+ name += '.atom.xml' if format.to_s == 'atom'
43
+ name += '.json' if format.to_s == 'json'
44
+
45
+ File.read(File.join(File.dirname(__FILE__), 'fixtures', name))
46
+ end
47
+ end
@@ -0,0 +1,8 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+ require 'net/http'
3
+
4
+ class RequestTest < Test::Unit::TestCase
5
+ def test_nothing
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,180 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class XMLHandlerTest < Test::Unit::TestCase
4
+ def setup
5
+ @handler = Partigirb::Handlers::XMLHandler.new
6
+ end
7
+
8
+ should "extract root nodes" do
9
+ xmls = build_xml_string do |xml|
10
+ xml.instruct!
11
+ xml.user do
12
+ xml.id 12
13
+ xml.name 'Wadus'
14
+ end
15
+ end
16
+
17
+ res = @handler.decode_response(xmls)
18
+ assert res.is_a?(Partigirb::PartigiStruct)
19
+ assert_equal 12, res.id
20
+ assert_equal 'Wadus', res.name
21
+ end
22
+
23
+ should "extract nested nodes" do
24
+ xmls = build_xml_string do |xml|
25
+ xml.instruct!
26
+ xml.user do
27
+ xml.id 1
28
+ xml.friends :type => 'collection' do
29
+ xml.friend do
30
+ xml.id 2
31
+ xml.name 'Wadus'
32
+ end
33
+ xml.friend do
34
+ xml.id 3
35
+ xml.name 'Gradus'
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ res = @handler.decode_response(xmls)
42
+ assert res.is_a?(Partigirb::PartigiStruct)
43
+ assert_equal 1, res.id
44
+ assert res.friends.is_a?(Array)
45
+ assert_equal 2, res.friends[0].id
46
+ assert_equal 3, res.friends[1].id
47
+ assert_equal 'Wadus', res.friends[0].name
48
+ assert_equal 'Gradus', res.friends[1].name
49
+ end
50
+
51
+ should "extract nodes using namespaces" do
52
+ xmls = build_xml_string do |xml|
53
+ xml.instruct!
54
+ xml.user({"xmlns:nameSpace" => 'http://schemas.overtheworld.com/v200/nameSpace'}) do
55
+ xml.nameSpace :id, 123
56
+ xml.nameSpace :name, 'Bob'
57
+ end
58
+ end
59
+
60
+ res = @handler.decode_response(xmls)
61
+ assert res.is_a?(Partigirb::PartigiStruct)
62
+ assert_equal 123, res.nameSpace_id
63
+ assert_equal 'Bob', res.nameSpace_name
64
+ end
65
+
66
+ should "extract attributes from nodes" do
67
+ xmls = build_xml_string do |xml|
68
+ xml.instruct!
69
+ xml.user do
70
+ xml.id 123
71
+ xml.name 'Bob'
72
+ xml.field({:attribute1 => 'value1', :attribute2 => 'value2', :attribute3 => 'value3'})
73
+ end
74
+ end
75
+
76
+ res = @handler.decode_response(xmls)
77
+ assert res.is_a?(Partigirb::PartigiStruct)
78
+ assert_equal 123, res.id
79
+ assert_equal 'Bob', res.name
80
+
81
+ assert res.field.is_a?(Partigirb::PartigiStruct)
82
+ assert_equal 'value1', res.field.attribute1
83
+ assert_equal 'value2', res.field.attribute2
84
+ assert_equal 'value3', res.field.attribute3
85
+ end
86
+
87
+ should "extract links to an array" do
88
+ xmls = build_xml_string do |xml|
89
+ xml.instruct!
90
+ xml.user do
91
+ xml.id 123
92
+ xml.link({:rel => 'alternate', :href => 'http://www.pointing-to-something.com', :type => 'text/html'})
93
+ xml.link({:rel => 'self', :href => 'http://www.pointing-to-self-something.com', :type => 'application/xml'})
94
+ end
95
+ end
96
+
97
+ res = @handler.decode_response(xmls)
98
+ assert res.is_a?(Partigirb::PartigiStruct)
99
+ assert_not_nil res.links
100
+ assert res.links.is_a?(Array)
101
+ assert_equal 'alternate', res.links.first.rel
102
+ assert_equal 'self', res.links[1].rel
103
+ end
104
+
105
+ should "extract values and ignore attributes for elements in IGNORE_ATTRIBUTES_FOR" do
106
+ xmls = build_xml_string do |xml|
107
+ xml.instruct!
108
+ xml.entry({"xmlns:ptItem" => 'http://schemas.overtheworld.com/v200/ptItem'}) do
109
+ xml.id 123
110
+ xml.ptItem :synopsis, {:lang => 'en', :type => 'text'}, 'In my opinion...'
111
+ xml.ptItem :title, {:lang => 'en'}, 'Wadus Movie'
112
+ end
113
+ end
114
+
115
+ res = @handler.decode_response(xmls)
116
+ assert res.is_a?(Partigirb::PartigiStruct)
117
+ assert_equal 'In my opinion...', res.ptItem_synopsis
118
+ assert_equal 'Wadus Movie', res.ptItem_title
119
+ end
120
+
121
+ should "create an struct with a method for each type for nodes defined as multiple type nodes" do
122
+ expected_content = <<-CONTENT
123
+ <h1>Testing</h1>
124
+ <p>This is raw html content</p>
125
+ CONTENT
126
+
127
+ xmls = build_xml_string do |xml|
128
+ xml.instruct!
129
+ xml.entry({"xmlns:ptItem" => 'http://schemas.overtheworld.com/v200/ptItem'}) do
130
+ xml.id 123
131
+ xml.content("This is text content", :type => 'text')
132
+ xml.content(:type => 'html') do
133
+ xml << <<-CONTENT
134
+ <![CDATA[
135
+ #{expected_content}
136
+ ]]>
137
+ CONTENT
138
+ end
139
+ end
140
+ end
141
+
142
+ res = @handler.decode_response(xmls)
143
+ assert res.respond_to?('content')
144
+ assert res.content.respond_to?('text')
145
+ assert res.content.respond_to?('html')
146
+
147
+ assert_equal "This is text content", res.content.text
148
+ assert_equal "<h1>Testing</h1><p>This is raw html content</p>", res.content.html.strip
149
+ end
150
+
151
+ should "not parse elements with type property set to xhtml even if they are not in IGNORE_ATTRIBUTES_FOR" do
152
+ expected_content = <<-XHTML
153
+ <div xmlns="http://www.w3.org/1999/xhtml">
154
+ <a href="http://www.partigi.com/films/watchmen">
155
+ <img alt="Watchmen's poster" src="http://s3.amazonaws.com/partigiproduction/films/posters/179/watchmen_thumb.jpg"/>
156
+ </a>
157
+ </div>
158
+ <div xmlns="http://www.w3.org/1999/xhtml">
159
+ <blockquote>Great movie. I recommend to read the comic before watching the movie.</blockquote>
160
+ </div>
161
+ XHTML
162
+
163
+ xmls = build_xml_string do |xml|
164
+ xml.instruct!
165
+ xml.entry({"xmlns:ptItem" => 'http://schemas.overtheworld.com/v200/ptItem'}) do
166
+ xml.id 123
167
+ xml.plaindata(:type => 'xhtml') do
168
+ xml << <<-CONTENT
169
+ <![CDATA[
170
+ #{expected_content}
171
+ ]]>
172
+ CONTENT
173
+ end
174
+ end
175
+ end
176
+
177
+ res = @handler.decode_response(xmls)
178
+ assert_equal expected_content.strip.gsub(/\n\s+/,''), res.plaindata.strip
179
+ end
180
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: partigirb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.7
5
+ platform: ruby
6
+ authors:
7
+ - Alvaro Bautista
8
+ - Fernando Blat
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-10-28 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description:
18
+ email:
19
+ - alvarobp@gmail.com
20
+ - ferblape@gmail.com
21
+ executables: []
22
+
23
+ extensions: []
24
+
25
+ extra_rdoc_files:
26
+ - LICENSE
27
+ - README.markdown
28
+ files:
29
+ - .gitignore
30
+ - LICENSE
31
+ - README.markdown
32
+ - Rakefile
33
+ - VERSION
34
+ - examples/last_reviews_summary.rb
35
+ - examples/who_ignores_me.rb
36
+ - lib/partigirb.rb
37
+ - lib/partigirb/client.rb
38
+ - lib/partigirb/core_ext.rb
39
+ - lib/partigirb/handlers/atom_handler.rb
40
+ - lib/partigirb/handlers/json_handler.rb
41
+ - lib/partigirb/handlers/string_handler.rb
42
+ - lib/partigirb/handlers/xml_handler.rb
43
+ - lib/partigirb/transport.rb
44
+ - partigirb.gemspec
45
+ - test/atom_handler_test.rb
46
+ - test/client_test.rb
47
+ - test/fixtures/alvaro_friends.atom.xml
48
+ - test/fixtures/pulp_fiction.atom.xml
49
+ - test/json_handler_test.rb
50
+ - test/mocks/net_http_mock.rb
51
+ - test/mocks/response_mock.rb
52
+ - test/mocks/transport_mock.rb
53
+ - test/test_helper.rb
54
+ - test/transport_test.rb
55
+ - test/xml_handler_test.rb
56
+ has_rdoc: true
57
+ homepage: http://github.com/partigi/partigirb
58
+ licenses: []
59
+
60
+ post_install_message:
61
+ rdoc_options:
62
+ - --charset=UTF-8
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ version:
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: "0"
76
+ version:
77
+ requirements: []
78
+
79
+ rubyforge_project:
80
+ rubygems_version: 1.3.5
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: A Ruby wrapper for the Partigi API
84
+ test_files:
85
+ - test/atom_handler_test.rb
86
+ - test/client_test.rb
87
+ - test/json_handler_test.rb
88
+ - test/mocks/net_http_mock.rb
89
+ - test/mocks/response_mock.rb
90
+ - test/mocks/transport_mock.rb
91
+ - test/test_helper.rb
92
+ - test/transport_test.rb
93
+ - test/xml_handler_test.rb
94
+ - examples/last_reviews_summary.rb
95
+ - examples/who_ignores_me.rb