Skip to content

Instantly share code, notes, and snippets.

@evmorov
evmorov / xml-test.xml
Last active May 3, 2016 06:23
For the article at medium.com
<root>
<schools>
<school>
<id>1</id>
<name>First school</name>
</school>
<school>
<id>2</id>
<name>Second school</name>
</school>
@evmorov
evmorov / nokogiri1.rb
Last active May 3, 2016 06:45
For the article at medium.com
let(:doc) { Nokogiri::XML(xml) }
it do
id = doc.xpath('//school[name/text()="Second school"]/id').text
expect(id).to eq('2')
end
@evmorov
evmorov / nokogiri2.rb
Last active May 3, 2016 06:44
For the article at medium.com
let(:doc) { Nokogiri::XML(xml) }
it do
school_id = doc.xpath('//school[name/text()="Second school"]/id').text
student_names = doc.xpath("//student[schoolId/text()=#{school_id}]/name").map(&:text)
expect(student_names).to match(%w(Alex Olivia))
end
@evmorov
evmorov / happymapper1.rb
Last active May 3, 2016 06:40
For the article at medium.com
class School
include HappyMapper
tag 'school'
has_one :id, String
has_one :name, String
end
let(:schools) { School.parse(xml) }
it do
@evmorov
evmorov / happymapper2.rb
Created May 3, 2016 06:42
For the article at medium.com
class Student
include HappyMapper
tag 'student'
has_one :name, String
has_one :school_id, String, tag: 'schoolId'
end
class School
include HappyMapper
tag 'school'
require 'spec_helper'
require 'nokogiri'
describe 'Nokogiri' do
let(:xml) do
%(
<root>
<schools>
<school>
<id>1</id>
<?xml version="1.0"?>
<root>
<authorities>
<authority>
<originID>100</originID>
<timestamp>2016-06-08T15:56:32</timestamp>
<authorityName>authorityNameNew</authorityName>
<INN>1234567890</INN>
</authority>
</authorities>
require 'sinatra'
get '/pizza' do
if rand(0..1).zero?
'OK'
else
'Something is wrong'
end
end
require 'net/http'
url = URI.parse('http://localhost:4567/pizza')
req = Net::HTTP::Get.new(url.to_s)
res = Net::HTTP.start(url.host, url.port) { |http| http.request(req) }
body = res.body
abort body if body != 'OK'