kdonovan-happymapper 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- data/History +77 -0
- data/License +20 -0
- data/Manifest +40 -0
- data/README +10 -0
- data/Rakefile +43 -0
- data/TODO +0 -0
- data/examples/amazon.rb +34 -0
- data/examples/current_weather.rb +21 -0
- data/examples/dashed_elements.rb +20 -0
- data/examples/family_tree.rb +55 -0
- data/examples/post.rb +19 -0
- data/examples/twitter.rb +37 -0
- data/happymapper.gemspec +35 -0
- data/lib/happymapper/attribute.rb +3 -0
- data/lib/happymapper/element.rb +3 -0
- data/lib/happymapper/item.rb +175 -0
- data/lib/happymapper/version.rb +3 -0
- data/lib/happymapper.rb +220 -0
- data/spec/fixtures/address.xml +8 -0
- data/spec/fixtures/commit.xml +52 -0
- data/spec/fixtures/current_weather.xml +89 -0
- data/spec/fixtures/family_tree.xml +23 -0
- data/spec/fixtures/multiple_namespaces.xml +170 -0
- data/spec/fixtures/partial_posts.xml +5 -0
- data/spec/fixtures/pita.xml +133 -0
- data/spec/fixtures/posts.xml +23 -0
- data/spec/fixtures/product_default_namespace.xml +17 -0
- data/spec/fixtures/product_no_namespace.xml +10 -0
- data/spec/fixtures/product_single_namespace.xml +10 -0
- data/spec/fixtures/radar.xml +21 -0
- data/spec/fixtures/statuses.xml +422 -0
- data/spec/happymapper_attribute_spec.rb +17 -0
- data/spec/happymapper_element_spec.rb +17 -0
- data/spec/happymapper_item_spec.rb +94 -0
- data/spec/happymapper_spec.rb +694 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +13 -0
- data/spec/xml_helper.rb +115 -0
- data/website/css/common.css +47 -0
- data/website/index.html +98 -0
- metadata +113 -0
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
begin
|
2
|
+
require 'spec'
|
3
|
+
rescue LoadError
|
4
|
+
require 'rubygems'
|
5
|
+
gem 'rspec'
|
6
|
+
require 'spec'
|
7
|
+
end
|
8
|
+
|
9
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'happymapper')
|
10
|
+
|
11
|
+
def fixture_file(filename)
|
12
|
+
File.read(File.dirname(__FILE__) + "/fixtures/#{filename}")
|
13
|
+
end
|
data/spec/xml_helper.rb
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
# This code has been modified from code shared on:
|
2
|
+
# http://blog.wolfman.com/articles/2008/01/02/xpath-matchers-for-rspec
|
3
|
+
|
4
|
+
# would love to move away from rexml to libxml-ruby
|
5
|
+
require 'rexml/document'
|
6
|
+
require 'rexml/element'
|
7
|
+
|
8
|
+
module Spec
|
9
|
+
module Matchers
|
10
|
+
|
11
|
+
# check if the xpath exists one or more times
|
12
|
+
class HaveXpath
|
13
|
+
def initialize(xpath, namespaces={})
|
14
|
+
@xpath = xpath
|
15
|
+
@namespaces = namespaces
|
16
|
+
end
|
17
|
+
|
18
|
+
def matches?(response)
|
19
|
+
@response = response
|
20
|
+
doc = response.is_a?(REXML::Document) ? response : REXML::Document.new(@response)
|
21
|
+
match = REXML::XPath.match(doc, @xpath, @namespaces)
|
22
|
+
not match.empty?
|
23
|
+
end
|
24
|
+
|
25
|
+
def failure_message
|
26
|
+
"Did not find expected xpath #{@xpath}"
|
27
|
+
end
|
28
|
+
|
29
|
+
def negative_failure_message
|
30
|
+
"Did find unexpected xpath #{@xpath}"
|
31
|
+
end
|
32
|
+
|
33
|
+
def description
|
34
|
+
"match the xpath expression #{@xpath}"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def have_xpath(xpath, namespaces={})
|
39
|
+
HaveXpath.new(xpath, namespaces)
|
40
|
+
end
|
41
|
+
|
42
|
+
# check if the xpath has the specified value
|
43
|
+
# value is a string and there must be a single result to match its
|
44
|
+
# equality against
|
45
|
+
class MatchXpath
|
46
|
+
def initialize(xpath, val, namespaces={})
|
47
|
+
@xpath = xpath
|
48
|
+
@val= val
|
49
|
+
@namespaces = namespaces
|
50
|
+
end
|
51
|
+
|
52
|
+
def matches?(response)
|
53
|
+
@response = response
|
54
|
+
doc = response.is_a?(REXML::Document) ? response : REXML::Document.new(@response)
|
55
|
+
ok= true
|
56
|
+
match = REXML::XPath.match(doc, @xpath, @namespaces)
|
57
|
+
return false if match.empty?
|
58
|
+
REXML::XPath.each(doc, @xpath, @namespaces) do |e|
|
59
|
+
@actual_val= case e
|
60
|
+
when REXML::Attribute
|
61
|
+
e.to_s
|
62
|
+
when REXML::Element
|
63
|
+
e.text
|
64
|
+
else
|
65
|
+
e.to_s
|
66
|
+
end
|
67
|
+
return false unless @val == @actual_val
|
68
|
+
end
|
69
|
+
return ok
|
70
|
+
end
|
71
|
+
|
72
|
+
def failure_message
|
73
|
+
"The xpath #{@xpath} did not have the value '#{@val}'\nIt was '#{@actual_val}'"
|
74
|
+
end
|
75
|
+
|
76
|
+
def description
|
77
|
+
"match the xpath expression #{@xpath} with #{@val}"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def match_xpath(xpath, val, namespaces={})
|
82
|
+
MatchXpath.new(xpath, val, namespaces)
|
83
|
+
end
|
84
|
+
|
85
|
+
# checks if the given xpath occurs num times
|
86
|
+
class HaveNodes #:nodoc:
|
87
|
+
def initialize(xpath, num, namespaces = nil)
|
88
|
+
@xpath= xpath
|
89
|
+
@num = num
|
90
|
+
@namespaces = namespaces
|
91
|
+
end
|
92
|
+
|
93
|
+
def matches?(response)
|
94
|
+
@response = response
|
95
|
+
doc = response.is_a?(REXML::Document) ? response : REXML::Document.new(@response)
|
96
|
+
match = REXML::XPath.match(doc, @xpath, @namespaces)
|
97
|
+
@num_found= match.size
|
98
|
+
@num_found == @num
|
99
|
+
end
|
100
|
+
|
101
|
+
def failure_message
|
102
|
+
"Did not find expected number of nodes #{@num} in xpath #{@xpath}\nFound #{@num_found}"
|
103
|
+
end
|
104
|
+
|
105
|
+
def description
|
106
|
+
"match the number of nodes #{@num}"
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def have_nodes(xpath, num, namespaces = nil)
|
111
|
+
HaveNodes.new(xpath, num, namespaces)
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
@media screen, projection {
|
2
|
+
/*
|
3
|
+
Copyright (c) 2007, Yahoo! Inc. All rights reserved.
|
4
|
+
Code licensed under the BSD License:
|
5
|
+
http://developer.yahoo.net/yui/license.txt
|
6
|
+
version: 2.2.0
|
7
|
+
*/
|
8
|
+
body {font:13px arial,helvetica,clean,sans-serif;*font-size:small;*font:x-small;}table {font-size:inherit;font:100%;}select, input, textarea {font:99% arial,helvetica,clean,sans-serif;}pre, code {font:115% monospace;*font-size:100%;}body * {line-height:1.22em;}
|
9
|
+
body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,textarea,p,blockquote,th,td{margin:0;padding:0;}table{border-collapse:collapse;border-spacing:0;}fieldset,img{border:0;}address,caption,cite,code,dfn,em,strong,th,var{font-style:normal;font-weight:normal;}/*ol,ul {list-style:none;}*/caption,th {text-align:left;}h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:normal;}q:before,q:after{content:'';}abbr,acronym {border:0;}
|
10
|
+
/* end of yahoo reset and fonts */
|
11
|
+
|
12
|
+
body {color:#333; background:#4b1a1a; line-height:1.3;}
|
13
|
+
p {margin:0 0 20px;}
|
14
|
+
a {color:#4b1a1a;}
|
15
|
+
a:hover {text-decoration:none;}
|
16
|
+
strong {font-weight:bold;}
|
17
|
+
em {font-style:italics;}
|
18
|
+
h1,h2,h3,h4,h5,h6 {font-weight:bold;}
|
19
|
+
h1 {font-size:197%; margin:30px 0; color:#4b1a1a;}
|
20
|
+
h2 {font-size:174%; margin:20px 0; color:#b8111a;}
|
21
|
+
h3 {font-size:152%; margin:10px 0;}
|
22
|
+
h4 {font-size:129%; margin:10px 0;}
|
23
|
+
pre {background:#eee; margin:0 0 20px; padding:20px; border:1px solid #ccc; font-size:100%; overflow:auto;}
|
24
|
+
code {font-size:100%; margin:0; padding:0;}
|
25
|
+
ul, ol {margin:10px 0 10px 25px;}
|
26
|
+
ol li {margin:0 0 10px;}
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
div#wrapper {background:#fff; width:560px; margin:0 auto; padding:20px; border:10px solid #bc8c46; border-width:0 10px;}
|
33
|
+
div#header {position:relative; border-bottom:1px dotted; margin:0 0 10px; padding:0 0 10px;}
|
34
|
+
div#header p {margin:0; padding:0;}
|
35
|
+
div#header h1 {margin:0; padding:0;}
|
36
|
+
ul#nav {position:absolute; top:0; right:0; list-style:none; margin:0; padding:0;}
|
37
|
+
ul#nav li {display:inline; padding:0 0 0 5px;}
|
38
|
+
ul#nav li a {}
|
39
|
+
div#content {}
|
40
|
+
div#footer {margin:40px 0 0; border-top:1px dotted; padding:10px 0 0;}
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
}
|
data/website/index.html
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
2
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
3
|
+
<head>
|
4
|
+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
5
|
+
<title>HappyMapper by John Nunemaker</title>
|
6
|
+
<link rel="stylesheet" href="css/common.css" type="text/css" />
|
7
|
+
</head>
|
8
|
+
<body>
|
9
|
+
|
10
|
+
<div id="wrapper">
|
11
|
+
<div id="header">
|
12
|
+
<h1>HappyMapper</h1>
|
13
|
+
<p>Object to xml mapping library.</p>
|
14
|
+
|
15
|
+
<ul id="nav">
|
16
|
+
<li><a href="rdoc/">Docs</a></li>
|
17
|
+
<li><a href="http://github.com/jnunemaker/happymapper">Github</a></li>
|
18
|
+
<li><a href="http://jnunemaker.lighthouseapp.com/projects/20014-happy-mapper/overview">Lighthouse</a></li>
|
19
|
+
<li><a href="http://rubyforge.org/projects/happymapper/">Rubyforge</a></li>
|
20
|
+
</ul>
|
21
|
+
</div>
|
22
|
+
|
23
|
+
<div id="content">
|
24
|
+
<h2>Install</h2>
|
25
|
+
<pre><code>$ sudo gem install happymapper</code></pre>
|
26
|
+
|
27
|
+
<h2>Examples</h2>
|
28
|
+
|
29
|
+
<h3>Given the following xml:</h3>
|
30
|
+
<pre><code><statuses type="array">
|
31
|
+
<status>
|
32
|
+
<created_at>Sat Aug 09 05:38:12 +0000 2008</created_at>
|
33
|
+
<id>882281424</id>
|
34
|
+
<text>I so just thought the guy lighting the Olympic torch was falling when he began to run on the wall. Wow that would have been catastrophic.</text>
|
35
|
+
<source>web</source>
|
36
|
+
<truncated>false</truncated>
|
37
|
+
<in_reply_to_status_id>1234</in_reply_to_status_id>
|
38
|
+
<in_reply_to_user_id>12345</in_reply_to_user_id>
|
39
|
+
<favorited></favorited>
|
40
|
+
<user>
|
41
|
+
<id>4243</id>
|
42
|
+
<name>John Nunemaker</name>
|
43
|
+
<screen_name>jnunemaker</screen_name>
|
44
|
+
<location>Mishawaka, IN, US</location>
|
45
|
+
<description>Loves his wife, ruby, notre dame football and iu basketball</description>
|
46
|
+
<profile_image_url>http://s3.amazonaws.com/twitter_production/profile_images/53781608/Photo_75_normal.jpg</profile_image_url>
|
47
|
+
<url>http://addictedtonew.com</url>
|
48
|
+
<protected>false</protected>
|
49
|
+
<followers_count>486</followers_count>
|
50
|
+
</user>
|
51
|
+
</status>
|
52
|
+
</statuses></code></pre>
|
53
|
+
|
54
|
+
<h3>You could have the following objects:</h3>
|
55
|
+
|
56
|
+
<pre><code>class User
|
57
|
+
include HappyMapper
|
58
|
+
|
59
|
+
element :id, Integer
|
60
|
+
element :name, String
|
61
|
+
element :screen_name, String
|
62
|
+
element :location, String
|
63
|
+
element :description, String
|
64
|
+
element :profile_image_url, String
|
65
|
+
element :url, String
|
66
|
+
element :protected, Boolean
|
67
|
+
element :followers_count, Integer
|
68
|
+
end
|
69
|
+
|
70
|
+
class Status
|
71
|
+
include HappyMapper
|
72
|
+
|
73
|
+
element :id, Integer
|
74
|
+
element :text, String
|
75
|
+
element :created_at, Time
|
76
|
+
element :source, String
|
77
|
+
element :truncated, Boolean
|
78
|
+
element :in_reply_to_status_id, Integer
|
79
|
+
element :in_reply_to_user_id, Integer
|
80
|
+
element :favorited, Boolean
|
81
|
+
has_one :user, User
|
82
|
+
end
|
83
|
+
|
84
|
+
statuses = Status.parse(file_contents)
|
85
|
+
statuses.each do |status|
|
86
|
+
puts status.user.name, status.user.screen_name, status.text, status.source, ''
|
87
|
+
end</code></pre>
|
88
|
+
|
89
|
+
<h2>Support</h2>
|
90
|
+
<p>Conversations welcome in the <a href="http://groups.google.com/group/happymapper">google group</a> and bugs/features over at <a href="http://jnunemaker.lighthouseapp.com/projects/20014-happy-mapper/overview">Lightouse</a>.</p>
|
91
|
+
</div>
|
92
|
+
|
93
|
+
<div id="footer">
|
94
|
+
<p>Created by <a href="http://addictedtonew.com/about/">John Nunemaker</a></p>
|
95
|
+
</div>
|
96
|
+
</div>
|
97
|
+
</body>
|
98
|
+
</html>
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kdonovan-happymapper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- John Nunemaker
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-02-21 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: libxml-ruby
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - "="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.9.8
|
24
|
+
version:
|
25
|
+
description: object to xml mapping library
|
26
|
+
email: nunemaker@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- lib/happymapper/attribute.rb
|
33
|
+
- lib/happymapper/element.rb
|
34
|
+
- lib/happymapper/item.rb
|
35
|
+
- lib/happymapper/version.rb
|
36
|
+
- lib/happymapper.rb
|
37
|
+
- README
|
38
|
+
- TODO
|
39
|
+
files:
|
40
|
+
- examples/amazon.rb
|
41
|
+
- examples/current_weather.rb
|
42
|
+
- examples/dashed_elements.rb
|
43
|
+
- examples/family_tree.rb
|
44
|
+
- examples/post.rb
|
45
|
+
- examples/twitter.rb
|
46
|
+
- happymapper.gemspec
|
47
|
+
- History
|
48
|
+
- lib/happymapper/attribute.rb
|
49
|
+
- lib/happymapper/element.rb
|
50
|
+
- lib/happymapper/item.rb
|
51
|
+
- lib/happymapper/version.rb
|
52
|
+
- lib/happymapper.rb
|
53
|
+
- License
|
54
|
+
- Manifest
|
55
|
+
- Rakefile
|
56
|
+
- README
|
57
|
+
- spec/fixtures/address.xml
|
58
|
+
- spec/fixtures/commit.xml
|
59
|
+
- spec/fixtures/current_weather.xml
|
60
|
+
- spec/fixtures/family_tree.xml
|
61
|
+
- spec/fixtures/multiple_namespaces.xml
|
62
|
+
- spec/fixtures/partial_posts.xml
|
63
|
+
- spec/fixtures/pita.xml
|
64
|
+
- spec/fixtures/posts.xml
|
65
|
+
- spec/fixtures/product_default_namespace.xml
|
66
|
+
- spec/fixtures/product_no_namespace.xml
|
67
|
+
- spec/fixtures/product_single_namespace.xml
|
68
|
+
- spec/fixtures/radar.xml
|
69
|
+
- spec/fixtures/statuses.xml
|
70
|
+
- spec/happymapper_attribute_spec.rb
|
71
|
+
- spec/happymapper_element_spec.rb
|
72
|
+
- spec/happymapper_item_spec.rb
|
73
|
+
- spec/happymapper_spec.rb
|
74
|
+
- spec/spec.opts
|
75
|
+
- spec/spec_helper.rb
|
76
|
+
- spec/xml_helper.rb
|
77
|
+
- TODO
|
78
|
+
- website/css/common.css
|
79
|
+
- website/index.html
|
80
|
+
has_rdoc: true
|
81
|
+
homepage: http://happymapper.rubyforge.org
|
82
|
+
licenses:
|
83
|
+
post_install_message: May you have many happy mappings!
|
84
|
+
rdoc_options:
|
85
|
+
- --line-numbers
|
86
|
+
- --inline-source
|
87
|
+
- --title
|
88
|
+
- Happymapper
|
89
|
+
- --main
|
90
|
+
- README
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: "0"
|
98
|
+
version:
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: "1.2"
|
104
|
+
version:
|
105
|
+
requirements: []
|
106
|
+
|
107
|
+
rubyforge_project: happymapper
|
108
|
+
rubygems_version: 1.3.5
|
109
|
+
signing_key:
|
110
|
+
specification_version: 2
|
111
|
+
summary: object to xml mapping library
|
112
|
+
test_files: []
|
113
|
+
|