art_typograf 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in art_typograf.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Stereobooster
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,50 @@
1
+ # ArtTypograf
2
+
3
+ Universal tool for preparing russian text for web publishing. Ruby wrapper for typograf.artlebedev.ru webservice
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'art_typograf'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install art_typograf
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ require "art_typograf"
23
+
24
+ ArtTypograf.process('text')
25
+ ```
26
+
27
+ You can pass second argument - hash of options.
28
+
29
+ ### Default options
30
+
31
+ ```ruby
32
+ {
33
+ :entity_type => :no, # :html, :xml, :no, :mixed
34
+ :use_br => true,
35
+ :use_p => true,
36
+ :max_nobr => 3
37
+ }
38
+ ```
39
+
40
+ ## TODO
41
+ - support ruby 1.8 (`force_encoding` missing)
42
+ - implement missing specs
43
+
44
+ ## Contributing
45
+
46
+ 1. Fork it
47
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
48
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
49
+ 4. Push to the branch (`git push origin my-new-feature`)
50
+ 5. Create new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ desc "Run all examples"
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -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_typograf/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "art_typograf"
8
+ gem.version = ArtTypograf::VERSION
9
+ gem.authors = ["stereobooster"]
10
+ gem.email = ["stereobooster@gmail.com"]
11
+ gem.description = %q{Universal tool for preparing russian text for web publishing. Ruby wrapper for typograf.artlebedev.ru webservice}
12
+ gem.summary = %q{Universal tool for preparing russian text for web publishing}
13
+ gem.homepage = "https://github.com/stereobooster/art_typograf"
14
+ gem.license = "MIT"
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_development_dependency "rake"
22
+ gem.add_development_dependency "rspec"
23
+ end
@@ -0,0 +1,8 @@
1
+ require "art_typograf/version"
2
+ require "art_typograf/client"
3
+
4
+ module ArtTypograf
5
+ def self.process(text, options = {})
6
+ Client.new(options).send_request(text)
7
+ end
8
+ end
@@ -0,0 +1,101 @@
1
+ # encoding: utf-8
2
+ require 'net/http'
3
+
4
+ module ArtTypograf
5
+
6
+ class NetworkError < StandardError
7
+ def initialize(message="", backtrace = nil)
8
+ super(message)
9
+ self.set_backtrace(backtrace) if backtrace
10
+ end
11
+ end
12
+
13
+ class Client
14
+ URL = 'http://typograf.artlebedev.ru/webservices/typograf.asmx'
15
+ DEFAULT_PREFERENCES = {
16
+ :entity_type => :no,
17
+ :use_br => true,
18
+ :use_p => true,
19
+ :max_nobr => 3
20
+ # :encoding => 'UTF-8'
21
+ }
22
+ RESULT = /<ProcessTextResult>\s*((.|\n)*?)\s*<\/ProcessTextResult>/m
23
+
24
+ def check_options(options)
25
+ o = options.dup
26
+
27
+ [:use_br, :use_p].each do |key|
28
+ val = o[key]
29
+ o[key] = case val
30
+ when true then 1
31
+ when false then 0
32
+ when 0, 1 then val
33
+ else raise ArgumentError, "Unknown #{key}: #{val}"
34
+ end
35
+ end
36
+
37
+ o[:entity_type] = case o[:entity_type]
38
+ when :html then 1
39
+ when :xml then 2
40
+ when :no then 3
41
+ when :mixed then 4
42
+ when 1..4 then o[:entity_type]
43
+ else raise ArgumentError, "Unknown entity_type: #{o[:entity_type]}"
44
+ end
45
+
46
+ o
47
+ end
48
+
49
+ def form_xml(text, options)
50
+ o = options
51
+ xml = <<-SOAP_TEMPLATE
52
+ <?xml version="1.0" encoding="UTF-8" ?>
53
+ <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/">
54
+ <soap:Body>
55
+ <ProcessText xmlns="http://typograf.artlebedev.ru/webservices/">
56
+ <text>#{text.gsub(/&/, '&amp;').gsub(/</, '&lt;').gsub(/>/, '&gt;')}</text>
57
+ <entityType>#{o[:entity_type]}</entityType>
58
+ <useBr>#{o[:use_br]}</useBr>
59
+ <useP>#{o[:use_p]}</useP>
60
+ <maxNobr>#{o[:max_nobr]}</maxNobr>
61
+ </ProcessText>
62
+ </soap:Body>
63
+ </soap:Envelope>
64
+ SOAP_TEMPLATE
65
+ xml.gsub(/^\s|\s$/, '')
66
+ end
67
+
68
+ def initialize(options = {})
69
+ @url = URI.parse(options.delete(:url) || URL)
70
+ @options = check_options( DEFAULT_PREFERENCES.dup.merge(options) )
71
+ end
72
+
73
+ # Process text with remote web-service
74
+ def send_request(text)
75
+ begin
76
+ request = Net::HTTP::Post.new(@url.path, {
77
+ 'Content-Type' => 'text/xml',
78
+ 'SOAPAction' => '"http://typograf.artlebedev.ru/webservices/ProcessText"'
79
+ })
80
+ request.body = form_xml(text, @options)
81
+
82
+ response = Net::HTTP.new(@url.host, @url.port).start do |http|
83
+ http.request(request)
84
+ end
85
+ rescue StandardError => exception
86
+ raise NetworkError.new(exception.message, exception.backtrace)
87
+ end
88
+
89
+ if !response.is_a?(Net::HTTPOK)
90
+ raise NetworkError, "#{response.code}: #{response.message}"
91
+ end
92
+
93
+ if RESULT =~ response.body
94
+ body = $1.gsub(/&gt;/, '>').gsub(/&lt;/, '<').gsub(/&amp;/, '&')
95
+ body.force_encoding("UTF-8").chomp
96
+ else
97
+ raise NetworkError, "Can't match result #{response.body}"
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,3 @@
1
+ module ArtTypograf
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+ require 'art_typograf'
3
+ require 'rspec'
4
+
5
+ describe ArtTypograf do
6
+ it ".process" do
7
+ ArtTypograf.process("- Это \"Типограф\"?\n— Нет, это «Типограф»!").should eq "<p>— Это «Типограф»?<br />\n— Нет, это «Типограф»!<br />\n</p>"
8
+ end
9
+
10
+ it "should raise 404 error" do
11
+ lambda {ArtTypograf.process("Тест", :url => 'http://typograf.artlebedev.ru/404')}.should raise_error ArtTypograf::NetworkError
12
+ end
13
+
14
+ it "should raise host not found error" do
15
+ lambda {ArtTypograf.process("Тест", :url => 'http://www')}.should raise_error ArtTypograf::NetworkError
16
+ end
17
+
18
+ # TODO: mock server for offline testing, for detecting service change
19
+ # TODO: test every option
20
+ end
21
+
22
+ describe ArtTypograf::Client do
23
+ it ".form_xml" do
24
+ pending "TODO"
25
+ end
26
+
27
+ it ".check_options" do
28
+ pending "TODO"
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: art_typograf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - stereobooster
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '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: '0'
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: '0'
46
+ description: Universal tool for preparing russian text for web publishing. Ruby wrapper
47
+ for typograf.artlebedev.ru webservice
48
+ email:
49
+ - stereobooster@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - art_typograf.gemspec
60
+ - lib/art_typograf.rb
61
+ - lib/art_typograf/client.rb
62
+ - lib/art_typograf/version.rb
63
+ - spec/art_typograf_spec.rb
64
+ homepage: https://github.com/stereobooster/art_typograf
65
+ licenses:
66
+ - MIT
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ segments:
78
+ - 0
79
+ hash: -618060813
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ segments:
87
+ - 0
88
+ hash: -618060813
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 1.8.24
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: Universal tool for preparing russian text for web publishing
95
+ test_files:
96
+ - spec/art_typograf_spec.rb