Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/newsletter/adapters/json.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Newsletter
module Adapters
class Json
attr_reader :source

def initialize(source)
@source = source
end

def parse
JSON.parse(source)
end
end
end
end
19 changes: 19 additions & 0 deletions lib/newsletter/adapters/xml.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Newsletter
module Adapters
class Xml
attr_reader :source

def initialize(source)
@source = source
end

def parse
xml = Nokogiri::XML source
title = xml.xpath('//item/title')[0].children[0].text
body = xml.xpath('//item/body')[0].children[0].text

{ 'title' => title, 'body' => body }
end
end
end
end
17 changes: 2 additions & 15 deletions lib/newsletter/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,14 @@ class Content
attr_reader :title, :body

def self.parse(source, format)
content = if format == :json then parse_json(source)
elsif format == :xml then parse_xml(source)
end
adapter = Newsletter::Adapters.const_get(format).new(source)
content = adapter.parse
new(content['title'], content['body'])
end

def initialize(title, body)
@title = title
@body = body
end

def self.parse_json(source)
JSON.parse(source)
end

def self.parse_xml(sourse)
xml = Nokogiri::XML sourse
title = xml.xpath('//item/title')[0].children[0].text
body = xml.xpath('//item/body')[0].children[0].text

{ "title" => title, "body" => body }
end
end
end
6 changes: 4 additions & 2 deletions spec/anything_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require 'pry'

require 'newsletter/content'
require 'newsletter/adapters/json'
require 'newsletter/adapters/xml'

module Newsletter
describe Content do
Expand All @@ -14,7 +16,7 @@ module Newsletter
)
end

subject { Content.parse(json, :json) }
subject { Content.parse(json, :Json) }

it 'parses the title' do
expect(subject.title).to eq('Hello world!')
Expand All @@ -32,7 +34,7 @@ module Newsletter
)
end

subject { Content.parse(json, :xml) }
subject { Content.parse(json, :Xml) }

it 'parses the title' do
expect(subject.title).to eq('Hello world!')
Expand Down