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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.json
*.rss
*.atom
*.txt
6 changes: 6 additions & 0 deletions lesson04/homework/Converter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
В данном репозитории находится конвертер из RSS формата в форматы ATOM, JSON и наоборот.

Для использования программы используются следующие обязательные аттрибуты:
-i --input=FILE //Входной файл
-o --output=FORMAT //Выходной формат

22 changes: 22 additions & 0 deletions lesson04/homework/Converter/bin/optionParser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require 'optparse'

class Parser
@options = {}
def self.parse()
OptionParser.new do |parser|
parser.on('-i', '--input=FILE',
'File to convert')

parser.on('-o', '--output=FORMAT',
'Choose one of the formats: json, atom, rss')

parser.on('-h', '--help', 'Prints this help') do
puts parser
exit
end
end.parse!(into: @options)
@options
end
end
13 changes: 13 additions & 0 deletions lesson04/homework/Converter/convert.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'require_all'
require '~/converter/main.rb'
require 'nokogiri'
require 'feedjira'
require 'json'

require_all 'lib'
require_all 'bin'

options = Parser.parse
p options
main = Main.new(options)
main.run
21 changes: 21 additions & 0 deletions lesson04/homework/Converter/lib/check/check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module Check
def self.valid_json?(json)
JSON.parse(json)
true
rescue JSON::ParserError => e
false
end

def self.check(data)
str = data.to_s
if str.include? '<feed'
'atom'
elsif str.include? '<rss'
'rss'
elsif valid_json?(str)
'json'
end
end
end
Empty file.
5 changes: 5 additions & 0 deletions lesson04/homework/Converter/lib/converters/convert_to_json.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module ConvertToJson
def self.convert(data)
File.open('output.json', 'w+') { |f| f.write JSON.pretty_generate(data) }
end
end
Empty file.
Binary file not shown.
46 changes: 46 additions & 0 deletions lesson04/homework/Converter/lib/parsers/atom_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

module ParseAtom
def self.parseFeed(doc)
parsedString = []
feed_title = doc.at_css('feed title').text.strip
feed_updated = doc.at_css('feed updated').text.strip
feed_author = doc.at_css('feed author name').text.strip
feed_id = doc.at_css('feed id').text.strip

parsedString.push(
title: feed_title,
link: feed_link,
updated: feed_updated,
author: feed_author,
id: feed_id
)
end

def self.parseEntry(doc)
items = []

doc.css('entry').each do |item|
entry_title = doc.at_css('entry title').text.strip
entry_id = doc.at_css('entry id') .text.strip
entry_updated = doc.at_css('entry updated').text.strip
entry_summary = doc.at_css('entry summary').text.strip

items.push(
title: entry_title,
id: entry_id,
updated: entry_updated,
summary: entry_summary
)

end

items
end

def self.parse(doc)
doc = Nokogiri::XML(doc)
result = { feed: parseFeed(doc),
entry: parseEntry(doc) }
end
end
9 changes: 9 additions & 0 deletions lesson04/homework/Converter/lib/parsers/json_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require 'json'

module ParseJson
def self.parse(input)
data = JSON.parse(input)
end
end
57 changes: 57 additions & 0 deletions lesson04/homework/Converter/lib/parsers/rss_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

require 'nokogiri'

module ParseRss
def self.parseFeed(doc)
parsedString = []
feed_image_content = {}
feed_language = doc.css('channel language').text.strip
feed_title = doc.at_css('channel title').text
feed_description = doc.at_css('channel description').text.strip
feed_link = doc.at_css('channel link').text.strip
feed_image_content = { 'url' => doc.css('channel image url').text.strip,
'title' => doc.css('channel image title').text.strip,
'link' => doc.css('channel image link').text.strip }

parsedString.push(
language: feed_language,
title: feed_title,
description: feed_description,
link: feed_link,
image: feed_image_content
)
end

def self.parseItems(doc)
items = []

item_tags = doc.xpath('//item/*').map(&:name).each.uniq
p item_tags
doc.css('item').each do |item|
parsed_strings = {}
attr_names = []
item_tags.each do |tag|
attr_hash = {}
if !doc.xpath("//item/#{tag}[@*]").empty? # if tag has attribute parse them
attr_names = doc.xpath("//item/#{tag}/@*").map(&:name).each.uniq
attr_names.each do |att|
attr_hash[att] = item.css(tag)[0][att]
end
parsed_strings[tag] = attr_hash
else
item_result = item.css(tag.to_s).text.strip
parsed_strings[tag] = item_result
end
end
items.push << parsed_strings
end
items
end

def self.parse(doc)
doc = Nokogiri::XML(doc)
result = { feed: parseFeed(doc),
items: parseItems(doc) }
end
end
6 changes: 6 additions & 0 deletions lesson04/homework/Converter/lib/readers/file_reader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module File_reader

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FileReader

def self.read(path)
file = File.open(path)
data = file.read
end
end
7 changes: 7 additions & 0 deletions lesson04/homework/Converter/lib/readers/link_reader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'open-uri'

module Link_reader
def self.read(url)
data = open(url).read
end
end
35 changes: 35 additions & 0 deletions lesson04/homework/Converter/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

class Main
def initialize(options)
@input = options[:input]
@output_format = options[:output]
@sort = options[:sort]
end

def run
data = if @input.match('^https|http')
Link_reader.read(@input)
else
File_reader.read(@input)
end

parsed_data = if Check.check(data) == 'atom'
ParseAtom.parse(data)
elsif Check.check(data) == 'rss'
ParseRss.parse(data)
elsif Check.check(data) == 'json'
ParseJson.parse(data)
else
p 'ERROR'
end

if @output.to_s.downcase == 'atom'
ConvertToAtom.convert(parsed_data)
elsif @output.to_s.downcase == 'rss'
ConvertToRss.convert(parsed_data)
else
ConvertToJson.convert(parsed_data)
end
end
end
6 changes: 6 additions & 0 deletions lesson04/homework/converter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.json
*.txt
*.swp
*.yml
*.atom
*.rss
10 changes: 10 additions & 0 deletions lesson04/homework/converter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
В данном репозитории находится конвертер из RSS формата в форматы ATOM, JSON и наоборот.

Для использования программы используются следующие обязательные аттрибуты:
-i --input=FILE //Входной файл
-o --output=FORMAT //Выходной формат

Необязательный аттрибут для названия выходного файла:
-n --name=FILE //Имя выходного файла

При отсутствии данного аттрибута, данные запишутся в файл output.txt
23 changes: 23 additions & 0 deletions lesson04/homework/converter/bin/option_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require 'optparse'

# parse options
class OptionParser
@options = {}
def self.parse
OptionParser.new do |parser|
parser.on('-i', '--input=FILE',
'File to convert')

parser.on('-o', '--output=FORMAT',
'Choose one of the formats: json, atom, rss')
parser.on('-n', '--name=FILE',
'Type converted file name')
parser.on('-h', '--help', 'Prints this help') do
exit
end
end.parse!(into: @options)
@options
end
end
11 changes: 11 additions & 0 deletions lesson04/homework/converter/convert.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

require 'require_all'
require '~/converter/main.rb'

require_all 'lib'
require_all 'bin'

options = OptionParser.parse
main = Main.new(options)
main.run
23 changes: 23 additions & 0 deletions lesson04/homework/converter/lib/check/check_format.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

# Check if input data matches one of the three formats: Atom, RSS, JSON
module Check
def self.valid_json?(json)
JSON.parse(json)
true
rescue JSON::ParserError => e
false
end

def self.check(data)
str = data.to_s
if str.include? '<feed'
'atom'
elsif str.include? '<rss'
'rss'
elsif valid_json?(str)
'json'
else 'error'
end
end
end
14 changes: 14 additions & 0 deletions lesson04/homework/converter/lib/converters/base_converter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module BaseConverter
def self.convert(data, option)
converters = ClassList.names('lib/converters/modules/*rb')
converters.each do |converter|
class_name = Module.const_get(converter)
# puts class_name.to_s.include?(option.capitalize)
if class_name.to_s.include?(option.capitalize)
return class_name.convert(data)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

require 'nokogiri'

# Class which converts hash data to RSS format
module ConvertToAtom
def self.convert(data)
builder = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
xml.atom('version' => '2.0', 'xmlns:atom' => 'http://www.w3.org/2005/Atom') do
data.values[0].each do |v|
xml.entry do
xml.title v[:title]
xml.summary v[:description]
xml.published v[:pubdate]
end
end
end
end
builder.to_xml
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

require 'json'

# Class which converts hash data to Json format
module ConvertToJson
def self.convert(data)
JSON.pretty_generate(data)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require 'nokogiri'

# Class which converts hash data to RSS format

module ConvertToRss
def self.convert(data)
builder = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
xml.rss('version' => '2.0', 'xmlns:atom' => 'http://www.w3.org/2005/Atom') do
data.values[0].each do |v|
xml.item do
xml.title v[:title]
xml.description v[:description]
xml.pubDate v[:pubdate]
end
end
end
end
builder.to_xml
end
end
Loading