Skip to content

Commit

Permalink
Move examples to proper place
Browse files Browse the repository at this point in the history
  • Loading branch information
alsemyonov committed Apr 6, 2017
1 parent 9c2ba1c commit 6aab837
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 176 deletions.
6 changes: 3 additions & 3 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Bundler/OrderedGems:
# Offense count: 1
Lint/ShadowingOuterLocalVariable:
Exclude:
- 'examples/atom.rb'
- 'example/atom.rb'

# Offense count: 9
Metrics/AbcSize:
Expand Down Expand Up @@ -177,8 +177,8 @@ Style/EmptyLineAfterMagicComment:
- 'bin/console'
- 'bin/rake'
- 'bin/rubocop'
- 'examples/atom.rb'
- 'examples/xml.rb'
- 'example/atom.rb'
- 'example/xml.rb'

# Offense count: 2
# Configuration parameters: MinBodyLength.
Expand Down
4 changes: 4 additions & 0 deletions .yardopts
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
--title=Xommelier
--markup=markdown
--files=example/building_an_atom_feed.rb
--files=example/building_an_atom_feed_using_hash.rb
--files=example/reading_an_atom_feed.rb
--readme=README.md
172 changes: 8 additions & 164 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,170 +17,13 @@ Look into {Xommelier::Atom}, {Xommelier::Atom::Threading}, and {Xommelier::Atom:
* [![Build Status](https://travis-ci.org/alsemyonov/xommelier.png?branch=master)](http://travis-ci.org/alsemyonov/xommelier)
* [![Dependency Status](https://gemnasium.com/alsemyonov/xommelier.png)](https://gemnasium.com/alsemyonov/xommelier)

## Examples with Atom

```ruby
require 'xommelier/atom/full'
```

### Reading a feed

```ruby
feed = Xommelier::Atom::Feed.parse(open('spec/fixtures/feed.atom.xml'))
puts feed.id, feed.title, feed.updated

feed.entries do |entry|
puts entry.id, entry.title, entry.published, entry.updated
puts entry.content || entry.summary
end
```

### Building a feed

```ruby
feed = Xommelier::Atom::Feed.new
feed.id = 'http://example.com/blog'
feed.title = 'Example.com blog'
feed.complete = Xommelier::Atom::History::Complete.new

entry = feed.entry = Xommelier::Atom::Entry.new(
id: 'http://example.com/blog/2012/03/05',
title: "Happy Xommelier's day!",
updated: 5.days.ago
).tap do |entry|
entry.link = Xommelier::Atom::Link.new(
href: entry.id,
rel: 'alternate',
type: 'text/html'
)
entry.links << Xommelier::Atom::Link.new(
href: "#{entry.id}/comments.atom",
rel: 'replies',
type: 'application/atom+xml',
count: 5
)
end

# Add Comments
3.times do |i|
feed.entries << Xommelier::Atom::Entry.new(
id: "http://example.com/blog/2012/03/05#comment_#{i}",
title: ('Hooray! ' * (i + 1)).strip,
updated: (5 - i).days.ago
).tap do |comment|
comment.in_reply_to = Xommelier::Atom::Threading::InReplyTo.new(
ref: entry.id,
href: entry.link.href
)
end
end

puts feed.to_xml
```

will output:

```xml
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:fh="http://purl.org/syndication/history/1.0">
<id>http://example.com/blog</id>
<title>Example.com blog</title>
<fh:complete/>
<entry>
<id>http://example.com/blog/2012/03/05</id>
<title>Happy Xommelier's day!</title>
<updated>2012-02-29T07:52:51+04:00</updated>
<link href="http://example.com/blog/2012/03/05" rel="alternate" type="text/html"/>
<link href="http://example.com/blog/2012/03/05/comments.atom" rel="replies" type="application/atom+xml" thr:count="5"/>
</entry>
<entry>
<id>http://example.com/blog/2012/03/05#comment_0</id>
<title>Hooray!</title>
<updated>2012-02-29T07:52:51+04:00</updated>
<thr:in-reply-to ref="http://example.com/blog/2012/03/05" href="http://example.com/blog/2012/03/05"/>
</entry>
<entry>
<id>http://example.com/blog/2012/03/05#comment_1</id>
<title>Hooray! Hooray!</title>
<updated>2012-03-01T07:52:51+04:00</updated>
<thr:in-reply-to ref="http://example.com/blog/2012/03/05" href="http://example.com/blog/2012/03/05"/>
</entry>
<entry>
<id>http://example.com/blog/2012/03/05#comment_2</id>
<title>Hooray! Hooray! Hooray!</title>
<updated>2012-03-02T07:52:51+04:00</updated>
<thr:in-reply-to ref="http://example.com/blog/2012/03/05" href="http://example.com/blog/2012/03/05"/>
</entry>
</feed>
```

### Building from hash

```ruby
feed = Xommelier::Atom::Feed.new(
{
title: 'Xommelier nest elements',
subtitle: 'Xommelier is able to build complex objects from very nested hash',
author: { name: 'Alex', email: '[email protected]' },
updated: Time.utc(2012, 04, 04, 04, 04),
contributors: [
{ name: 'Ivan', email: '[email protected]' },
{ name: 'Pyotr', email: '[email protected]' },
{ name: 'Sidor', email: '[email protected]' },
],
entries: [
{ title: 'First article', updated: Time.utc(2012, 01, 01, 01, 01) },
{ title: 'Second article', updated: Time.utc(2012, 02, 02, 02, 02) },
{ title: 'Third article', updated: Time.utc(2012, 03, 03, 03, 03) },
]
}
)

Xommelier::Atom::Person === feed.author #=> true
Xommelier::Atom::Person === feed.contributors[1] #=> true
Xommelier::Atom::Entry === feed.entries[2] #=> true

puts feed.to_xml
```

will output

```xml
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Xommelier nest elements</title>
<subtitle>Xommelier is able to build complex objects from very nested hash</subtitle>
<author>
<name>Alex</name>
<email>[email protected]</email>
</author>
<updated>2012-04-04T04:04:00Z</updated>
<contributor>
<name>Ivan</name>
<email>[email protected]</email>
</contributor>
<contributor>
<name>Pyotr</name>
<email>[email protected]</email>
</contributor>
<contributor>
<name>Sidor</name>
<email>[email protected]</email>
</contributor>
<entry>
<title>First article</title>
<updated>2012-01-01T01:01:00Z</updated>
</entry>
<entry>
<title>Second article</title>
<updated>2012-02-02T02:02:00Z</updated>
</entry>
<entry>
<title>Third article</title>
<updated>2012-03-03T03:03:00Z</updated>
</entry>
</feed>
```
## Examples

See `examples` dir for examples of:

* reading an Atom feed;
* building an Atom feed;
* building an Atom feed from hash;

## Built in XML namespaces:

Expand All @@ -191,6 +34,7 @@ will output

## TODO

* Rebuild on top of ROM
* Validating built XML against RelaxNG
* Converting XML Schema, RelaxNG, RelaxNG Compact and DTD into Xommelier Ruby DSL
* ActiveRecord-like automatic loading of XML Schema, RelaxNG, RelaxNG Compact and DTD without needing to write it down into ruby code
Expand Down
18 changes: 10 additions & 8 deletions examples/atom.rb → example/atom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
require 'xommelier/atom/threading'
require 'active_support/core_ext'

include Xommelier::Atom

# Reading a feed
feed = Xommelier::Atom::Feed.parse(open('spec/fixtures/feed.atom.xml'))
feed = Feed.parse(open('spec/fixtures/feed.atom.xml'))
puts feed.id, feed.title, feed.updated

feed.entries do |entry|
Expand All @@ -13,28 +15,28 @@
end

# Building a feed
feed = Xommelier::Atom::Feed.new
feed = Feed.new
feed.id = 'http://example.com/blog'
feed.title = 'Example.com blog'
feed.complete = Xommelier::Atom::History::Complete.new
feed.complete = History::Complete.new

entry = feed.entry = Xommelier::Atom::Entry.new(
entry = feed.entry = Entry.new(
id: 'http://example.com/blog/2012/03/05',
title: 'Happy Xommelier\'s day!',
updated: 5.days.ago
).tap do |entry|
entry.link = Xommelier::Atom::Link.new(href: entry.id, rel: 'alternate', type: 'text/html')
entry.links << Xommelier::Atom::Link.new(href: "#{entry.id}/comments.atom", rel: 'replies', type: 'application/atom+xml', count: 5)
entry.link = Link.new(href: entry.id, rel: 'alternate', type: 'text/html')
entry.links << Link.new(href: "#{entry.id}/comments.atom", rel: 'replies', type: 'application/atom+xml', count: 5)
end

# Add Comments
5.times do |i|
feed.entries << Xommelier::Atom::Entry.new(
feed.entries << Entry.new(
id: "http://example.com/blog/2012/03/05#comment_#{i}",
title: ('Hooray! ' * (i + 1)).strip,
updated: (5 - i).days.ago
).tap do |comment|
comment.in_reply_to = Xommelier::Atom::Threading::InReplyTo.new(ref: entry.id, href: entry.link.href)
comment.in_reply_to = Threading::InReplyTo.new(ref: entry.id, href: entry.link.href)
end
end

Expand Down
31 changes: 31 additions & 0 deletions example/building_an_atom_feed.output.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:fh="http://purl.org/syndication/history/1.0" xmlns:thr="http://purl.org/syndication/thread/1.0">
<id>http://example.com/blog</id>
<title>Example.com blog</title>
<entry>
<id>http://example.com/blog/2012/03/05</id>
<title>Happy Xommelier's day!</title>
<updated>2017-04-02T00:34:38+03:00</updated>
<link href="http://example.com/blog/2012/03/05" rel="alternate" type="text/html"/>
<link href="http://example.com/blog/2012/03/05/comments.atom" rel="replies" type="application/atom+xml" thr:count="5"/>
</entry>
<entry>
<id>http://example.com/blog/2012/03/05#comment_0</id>
<title>Hooray!</title>
<updated>2017-04-02T00:34:38+03:00</updated>
<thr:in-reply-to ref="http://example.com/blog/2012/03/05" href="http://example.com/blog/2012/03/05"/>
</entry>
<entry>
<id>http://example.com/blog/2012/03/05#comment_1</id>
<title>Hooray! Hooray!</title>
<updated>2017-04-03T00:34:38+03:00</updated>
<thr:in-reply-to ref="http://example.com/blog/2012/03/05" href="http://example.com/blog/2012/03/05"/>
</entry>
<entry>
<id>http://example.com/blog/2012/03/05#comment_2</id>
<title>Hooray! Hooray! Hooray!</title>
<updated>2017-04-04T00:34:38+03:00</updated>
<thr:in-reply-to ref="http://example.com/blog/2012/03/05" href="http://example.com/blog/2012/03/05"/>
</entry>
<fh:complete/>
</feed>
49 changes: 49 additions & 0 deletions example/building_an_atom_feed.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

# @title Building an Atom feed

require 'xommelier/atom/full'
require 'active_support/core_ext/numeric/time'

feed = Xommelier::Atom::Feed.new
feed.id = 'http://example.com/blog'
feed.title = 'Example.com blog'
feed.complete = Xommelier::Atom::History::Complete.new

feed.entry =
entry = Xommelier::Atom::Entry.new(
id: 'http://example.com/blog/2012/03/05',
title: "Happy Xommelier's day!",
updated: 5.days.ago
)

# Adding the only one link
entry.link = Xommelier::Atom::Link.new(
href: entry.id,
rel: 'alternate',
type: 'text/html'
)

# Add more links later
entry.links << Xommelier::Atom::Link.new(
href: "#{entry.id}/comments.atom",
rel: 'replies',
type: 'application/atom+xml',
count: 5
)

# Add Comments
3.times do |i|
feed.entries << Xommelier::Atom::Entry.new(
id: "http://example.com/blog/2012/03/05#comment_#{i}",
title: ('Hooray! ' * (i + 1)).strip,
updated: (5 - i).days.ago
).tap do |comment|
comment.in_reply_to = Xommelier::Atom::Threading::InReplyTo.new(
ref: entry.id,
href: entry.link.href
)
end
end

puts feed.to_xml
34 changes: 34 additions & 0 deletions example/building_an_atom_feed_using_hash.output.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Xommelier nest elements</title>
<updated>2012-04-04T04:04:00Z</updated>
<subtitle>Xommelier is able to build complex objects from very nested hash</subtitle>
<author>
<name>Alex</name>
<email>[email protected]</email>
</author>
<contributor>
<name>Ivan</name>
<email>[email protected]</email>
</contributor>
<contributor>
<name>Pyotr</name>
<email>[email protected]</email>
</contributor>
<contributor>
<name>Sidor</name>
<email>[email protected]</email>
</contributor>
<entry>
<title>First article</title>
<updated>2012-01-01T01:01:00Z</updated>
</entry>
<entry>
<title>Second article</title>
<updated>2012-02-02T02:02:00Z</updated>
</entry>
<entry>
<title>Third article</title>
<updated>2012-03-03T03:03:00Z</updated>
</entry>
</feed>
Loading

0 comments on commit 6aab837

Please sign in to comment.