art_typograph 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/Readme.markdown +23 -0
- data/art_typograph.gemspec +23 -0
- data/lib/art_typograph.rb +9 -0
- data/lib/art_typograph/request.rb +90 -0
- data/lib/art_typograph/version.rb +3 -0
- data/spec/art_typograph_spec.rb +17 -0
- data/spec/request_spec.rb +52 -0
- data/spec/spec_helper.rb +2 -0
- metadata +107 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/Readme.markdown
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Клиент для [веб-сервиса «Типографа»](http://www.artlebedev.ru/tools/typograf/webservice/), код частично позаимствован из [als_typograf](https://github.com/alsemyonov/als_typograf).
|
2
|
+
|
3
|
+
```ruby
|
4
|
+
ArtTypograph.process('- Это "Типограф"?')
|
5
|
+
# => "<p>— Это «Типограф»!</p>"
|
6
|
+
|
7
|
+
ArtTypograph.process('- Это "Типограф"?', :use_p => false)
|
8
|
+
# => "— Это «Типограф»!"
|
9
|
+
```
|
10
|
+
|
11
|
+
Возможные и дефолтные опции:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
{
|
15
|
+
:entity_type => :no, # :html, :xml, :no, :mixed
|
16
|
+
:use_br => false,
|
17
|
+
:use_p => true,
|
18
|
+
:max_nobr => 3,
|
19
|
+
:encoding => 'UTF-8'
|
20
|
+
}
|
21
|
+
```
|
22
|
+
|
23
|
+
Можно изменить в `ArtTypograph::Request.default_options`.
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'art_typograph/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "art_typograph"
|
8
|
+
gem.version = ArtTypograph::VERSION
|
9
|
+
gem.authors = ["Max Savchenko"]
|
10
|
+
gem.email = ["robotector@gmail.com"]
|
11
|
+
gem.description = %q{Клиент для веб-сервиса Типографа студии Лебедева}
|
12
|
+
gem.summary = %q{Клиент для веб-сервиса Типографа студии Лебедева}
|
13
|
+
gem.homepage = "http://github.com/macovsky/art_typograph"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency("activesupport", ">= 3.0.0")
|
21
|
+
gem.add_development_dependency 'rspec', '~> 2.12'
|
22
|
+
gem.add_development_dependency 'webmock', '~> 1.9.0'
|
23
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'active_support/core_ext/class/attribute'
|
3
|
+
require 'active_support/core_ext/hash/reverse_merge'
|
4
|
+
|
5
|
+
module ArtTypograph
|
6
|
+
class Request
|
7
|
+
attr_reader :options
|
8
|
+
|
9
|
+
class_attribute :url, :instance_writer => false
|
10
|
+
self.url = URI.parse('http://typograf.artlebedev.ru/webservices/typograf.asmx')
|
11
|
+
|
12
|
+
class_attribute :default_options, :instance_writer => false
|
13
|
+
self.default_options = {
|
14
|
+
:entity_type => :no,
|
15
|
+
:use_br => false,
|
16
|
+
:use_p => true,
|
17
|
+
:max_nobr => 3,
|
18
|
+
:encoding => 'UTF-8'
|
19
|
+
}
|
20
|
+
|
21
|
+
def initialize(options = {})
|
22
|
+
@options = self.class.prepare_options(options)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Process text with remote web-service
|
26
|
+
# @param [String] text text to process
|
27
|
+
def process(text)
|
28
|
+
request = Net::HTTP::Post.new(url.path, {
|
29
|
+
'Content-Type' => 'text/xml',
|
30
|
+
'SOAPAction' => '"http://typograf.artlebedev.ru/webservices/ProcessText"'
|
31
|
+
})
|
32
|
+
request.body = <<-END_SOAP
|
33
|
+
<?xml version="1.0" encoding="#{options[:encoding]}" ?>
|
34
|
+
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
|
35
|
+
<soap:Body>
|
36
|
+
<ProcessText xmlns="http://typograf.artlebedev.ru/webservices/">
|
37
|
+
<text>#{text.gsub(/&/, '&').gsub(/</, '<').gsub(/>/, '>')}</text>
|
38
|
+
<entityType>#{options[:entity_type]}</entityType>
|
39
|
+
<useBr>#{options[:use_br]}</useBr>
|
40
|
+
<useP>#{options[:use_p]}</useP>
|
41
|
+
<maxNobr>#{options[:max_nobr]}</maxNobr>
|
42
|
+
</ProcessText>
|
43
|
+
</soap:Body>
|
44
|
+
</soap:Envelope>
|
45
|
+
END_SOAP
|
46
|
+
|
47
|
+
response = Net::HTTP.new(url.host, url.port).start do |http|
|
48
|
+
http.request(request)
|
49
|
+
end
|
50
|
+
|
51
|
+
case response
|
52
|
+
when Net::HTTPSuccess
|
53
|
+
result = if /<ProcessTextResult>\s*((.|\n)*?)\s*<\/ProcessTextResult>/m =~ response.body
|
54
|
+
$1.gsub(/>/, '>').gsub(/</, '<').gsub(/&/, '&').gsub(/(\t|\n)$/, '')
|
55
|
+
else
|
56
|
+
text
|
57
|
+
end
|
58
|
+
else
|
59
|
+
text
|
60
|
+
end
|
61
|
+
rescue ::Exception => exception
|
62
|
+
text
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.prepare_options(options)
|
66
|
+
o = options.reverse_merge(default_options)
|
67
|
+
|
68
|
+
[:use_br, :use_p].each do |key|
|
69
|
+
val = o[key]
|
70
|
+
o[key] = case val
|
71
|
+
when true then 1
|
72
|
+
when false then 0
|
73
|
+
when 0, 1 then val
|
74
|
+
else raise ArgumentError, "Unknown #{key}: #{val}"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
o[:entity_type] = case o[:entity_type]
|
79
|
+
when :html then 1
|
80
|
+
when :xml then 2
|
81
|
+
when :no then 3
|
82
|
+
when :mixed then 4
|
83
|
+
when 1..4 then o[:entity_type]
|
84
|
+
else raise ArgumentError, "Unknown entity_type: #{o[:entity_type]}"
|
85
|
+
end
|
86
|
+
|
87
|
+
o
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ArtTypograph do
|
4
|
+
context '.process' do
|
5
|
+
let(:options) { {:use_br => true} }
|
6
|
+
let(:request) { double }
|
7
|
+
let(:text) { 'text' }
|
8
|
+
let(:result) { "<p>text</p>" }
|
9
|
+
|
10
|
+
it 'proxies to request' do
|
11
|
+
ArtTypograph::Request.should_receive(:new).with(options).and_return(request)
|
12
|
+
request.should_receive(:process).with(text).and_return(result)
|
13
|
+
|
14
|
+
ArtTypograph.process(text, options).should == result
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ArtTypograph::Request do
|
4
|
+
def req
|
5
|
+
ArtTypograph::Request
|
6
|
+
end
|
7
|
+
|
8
|
+
its(:default_options) { should be_kind_of(Hash) }
|
9
|
+
|
10
|
+
context 'on initialize' do
|
11
|
+
it 'has default options' do
|
12
|
+
req.new.options.should == req.prepare_options(req.default_options)
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'with valid options' do
|
16
|
+
let(:options) { {:use_br => true} }
|
17
|
+
subject { req.new(options) }
|
18
|
+
|
19
|
+
it 'merges default options' do
|
20
|
+
subject.options.should include(:use_br, :use_p, :entity_type, :encoding, :max_nobr)
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'processes options' do
|
24
|
+
subject.options[:use_br].should == 1
|
25
|
+
subject.options[:use_p].should == 1
|
26
|
+
subject.options[:entity_type].should == 3
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'with invalid value' do
|
31
|
+
it 'raises an argument error' do
|
32
|
+
expect { req.new(:use_br => '1') }.to raise_error(ArgumentError)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context '#process' do
|
38
|
+
subject { req.new }
|
39
|
+
let(:processed_text) { "<p>text</p>" }
|
40
|
+
let(:request) { stub_http_request(:post, req.url.to_s) }
|
41
|
+
|
42
|
+
it 'returns processed text when request was successful' do
|
43
|
+
request.to_return(:body => %|<ProcessTextResult>#{processed_text}</ProcessTextResult>|, :status => 200)
|
44
|
+
subject.process("text").should == processed_text
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'returns original text if there was an error' do
|
48
|
+
request.to_return(:status => 500)
|
49
|
+
subject.process("text").should == "text"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: art_typograph
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Max Savchenko
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-31 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.0.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '2.12'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '2.12'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: webmock
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 1.9.0
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.9.0
|
62
|
+
description: Клиент для веб-сервиса Типографа студии Лебедева
|
63
|
+
email:
|
64
|
+
- robotector@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- Gemfile
|
71
|
+
- Rakefile
|
72
|
+
- Readme.markdown
|
73
|
+
- art_typograph.gemspec
|
74
|
+
- lib/art_typograph.rb
|
75
|
+
- lib/art_typograph/request.rb
|
76
|
+
- lib/art_typograph/version.rb
|
77
|
+
- spec/art_typograph_spec.rb
|
78
|
+
- spec/request_spec.rb
|
79
|
+
- spec/spec_helper.rb
|
80
|
+
homepage: http://github.com/macovsky/art_typograph
|
81
|
+
licenses: []
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ! '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ! '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 1.8.23
|
101
|
+
signing_key:
|
102
|
+
specification_version: 3
|
103
|
+
summary: Клиент для веб-сервиса Типографа студии Лебедева
|
104
|
+
test_files:
|
105
|
+
- spec/art_typograph_spec.rb
|
106
|
+
- spec/request_spec.rb
|
107
|
+
- spec/spec_helper.rb
|