als_typograf 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -14,8 +14,10 @@ tmtags
14
14
  *.swp
15
15
 
16
16
  ## PROJECT::GENERAL
17
+ .yardoc
17
18
  coverage
18
19
  rdoc
20
+ doc
19
21
  pkg
20
22
 
21
23
  ## PROJECT::SPECIFIC
data/README.rdoc CHANGED
@@ -1,6 +1,16 @@
1
- = als_typograf
1
+ = AlsTypograf
2
2
 
3
- Description goes here.
3
+ ruby-implementation of ArtLebedevStudio.RemoteTypograf class (web-service client)
4
+
5
+ * Typograf homepage: http://typograf.artlebedev.ru/
6
+ * Web-service address: http://typograf.artlebedev.ru/webservices/typograf.asmx
7
+ * WSDL-description: http://typograf.artlebedev.ru/webservices/typograf.asmx?WSDL
8
+
9
+ Default charset: UTF-8
10
+
11
+ Example:
12
+ require 'als_typograf'
13
+ puts AlsTypograf.process('"Вы все еще кое-как верстаете в "Ворде"? - Тогда мы идем к вам!"')
4
14
 
5
15
  == Note on Patches/Pull Requests
6
16
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -0,0 +1,57 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{als_typograf}
8
+ s.version = "0.0.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Alexander Semyonov"]
12
+ s.date = %q{2009-11-24}
13
+ s.description = %q{ruby-implementation of ArtLebedevStudio.RemoteTypograf class (web-service client)}
14
+ s.email = %q{rotuka@rotuka.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "als_typograf.gemspec",
27
+ "lib/als_typograf.rb",
28
+ "test/helper.rb",
29
+ "test/test_als_typograf.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/rotuka/als_typograf}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.5}
35
+ s.summary = %q{ArtLebedevStudio.RemoteTypograf}
36
+ s.test_files = [
37
+ "test/helper.rb",
38
+ "test/test_als_typograf.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
47
+ s.add_development_dependency(%q<yard>, [">= 0"])
48
+ else
49
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
50
+ s.add_dependency(%q<yard>, [">= 0"])
51
+ end
52
+ else
53
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
54
+ s.add_dependency(%q<yard>, [">= 0"])
55
+ end
56
+ end
57
+
data/lib/als_typograf.rb CHANGED
@@ -3,7 +3,10 @@ $KCODE = 'u'
3
3
  require 'activesupport'
4
4
  require 'httparty'
5
5
 
6
+ # ruby-implementation of ArtLebedevStudio.RemoteTypograf class (web-service client)
7
+ # @author Alexander Semyonov
6
8
  module AlsTypograf
9
+ # The request class
7
10
  class Request
8
11
  include HTTParty
9
12
  base_uri 'typograf.artlebedev.ru'
@@ -12,30 +15,19 @@ module AlsTypograf
12
15
  'Host' => 'typograf.artlebedev.ru',
13
16
  'SOAPAction' => '"http://typograf.artlebedev.ru/webservices/ProcessText"')
14
17
 
18
+ # Process text with remote web-service
19
+ # @param [String] text to process
20
+ # @param [Hash] options for web-service
15
21
  def process_text(text, options = {})
16
22
  prepared_text = text.gsub(/&/, '&amp;').
17
23
  gsub(/</, '&lt;').
18
24
  gsub(/>/, '&gt;')
19
- response = self.class.post('/webservices/typograf.asmx',
20
- :body => soap_request(prepared_text, options))
21
- result = response['soap:Envelope']['soap:Body']['ProcessTextResponse']['ProcessTextResult'].
22
- gsub(/&amp;/, '&').
23
- gsub(/&lt;/, '<').
24
- gsub(/&gt;/, '>').
25
- gsub(/\t$/, '')
26
- rescue
27
- text
28
- end
29
-
30
- protected
31
-
32
- def soap_request(text, options = {})
33
- soap_body = <<-END_SOAP
25
+ soap_request = <<-END_SOAP
34
26
  <?xml version="1.0" encoding="#{options[:encoding]}" ?>
35
27
  <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/">
36
28
  <soap:Body>
37
29
  <ProcessText xmlns="http://typograf.artlebedev.ru/webservices/">
38
- <text>#{text}</text>
30
+ <text>#{prepared_text}</text>
39
31
  <entityType>#{options[:entity_type]}</entityType>
40
32
  <useBr>#{options[:use_br]}</useBr>
41
33
  <useP>#{options[:use_p]}</useP>
@@ -44,11 +36,18 @@ module AlsTypograf
44
36
  </soap:Body>
45
37
  </soap:Envelope>
46
38
  END_SOAP
39
+ response = self.class.post('/webservices/typograf.asmx',
40
+ :body => soap_request)
41
+ response['soap:Envelope']['soap:Body']['ProcessTextResponse']['ProcessTextResult'].
42
+ gsub(/&amp;/, '&').
43
+ gsub(/&lt;/, '<').
44
+ gsub(/&gt;/, '>').
45
+ gsub(/\t$/, '')
46
+ rescue
47
+ text
47
48
  end
48
49
  end
49
50
 
50
- mattr_accessor :options
51
-
52
51
  DEFAULT_OPTIONS = {
53
52
  :entity_type => 4,
54
53
  :use_br => 1,
@@ -56,42 +55,84 @@ module AlsTypograf
56
55
  :max_nobr => 3,
57
56
  :encoding => 'UTF-8'
58
57
  }
58
+ VALID_OPTIONS = DEFAULT_OPTIONS.keys.join('|')
59
+
60
+ mattr_accessor :options
59
61
  @@options = DEFAULT_OPTIONS.dup
60
62
 
63
+ # Get a global AlsTypograf option
64
+ # @param [String, Symbol] option name
65
+ def self.[](param)
66
+ self.options[param.to_sym]
67
+ end
61
68
 
69
+ # Set a global AlsTypograf option
70
+ # @param [String, Symbol] option name
71
+ # @param [Numeric, String] value for the option
62
72
  def self.[]=(param, value)
63
- self.options[param] = value
73
+ self.options[param.to_sym] = value
64
74
  end
65
75
 
76
+ # Reset default options
66
77
  def self.default_options!
67
78
  @@options = DEFAULT_OPTIONS.dup
68
79
  end
69
80
 
81
+ # Set option #entity_type to HTML (e. g. &nbsp;, &mdash;)
70
82
  def self.html_entities!
71
83
  self[:entity_type] = 1
72
84
  end
73
85
 
86
+ # Set option #entity_type to XML (e. g. &#169;, &#155;)
74
87
  def self.xml_entities!
75
88
  self[:entity_type] = 2
76
89
  end
77
90
 
91
+ # Set option #entity_type to nothing (e. g. —, ©, ×)
78
92
  def self.no_entities!
79
93
  self[:entity_type] = 3
80
94
  end
81
95
 
96
+ # Set option #entity_type to mixed (e. g. &#169;, &nbsp;)
82
97
  def self.mixed_entities!
83
98
  self[:entity_type] = 4
84
99
  end
85
100
 
86
- def self.p!(value)
101
+ # Option to replace \n with +<br />+
102
+ # @param [Boolean] to use +<br />+ or not
103
+ def self.use_br=(value)
104
+ self[:use_br] = value ? 1 : 0
105
+ end
106
+
107
+ # Option to wrap paragraphs with +<p></p>+
108
+ # @param [Boolean] to wrap para or not
109
+ def self.use_p=(value)
87
110
  self[:use_p] = value ? 1 : 0
88
111
  end
89
112
 
90
- def self.nobr!(value)
113
+ # How many symbols around dash to surround with +<nobr>+ tag
114
+ # @param [Numeric, Boolean] symbols count
115
+ def self.max_nobr=(value)
91
116
  self[:max_nobr] = value ? value : 0
92
117
  end
93
118
 
94
- def self.process(text)
95
- Request.new.process_text(text, options)
119
+ def self.method_missing(method_name, *args)
120
+ case method_name.to_s
121
+ when /^(#{VALID_OPTIONS})=$/
122
+ self[$1.to_sym] = args.first
123
+ when /^(#{VALID_OPTIONS})$/
124
+ self[method_name.to_sym]
125
+ end
126
+ end
127
+
128
+ # Process text with Typograf web-service
129
+ # @param [String] text to process
130
+ # @param [Hash] custom_options the options to process current text with
131
+ # @option custom_options [String] :entity_type Type of entities to use in processed text
132
+ # @option custom_options [String] :use_br Whether or not to use +<br />+
133
+ # @option custom_options [String] :use_p Whether or not to surround paras with +<p></p>+
134
+ # @option custom_options [String] :encoding Encoding of text
135
+ def self.process(text, custom_options = {})
136
+ Request.new.process_text(text, custom_options.reverse_merge(options))
96
137
  end
97
138
  end
@@ -10,7 +10,7 @@ class TestAlsTypograf < Test::Unit::TestCase
10
10
  end
11
11
 
12
12
  context 'no p' do
13
- setup { AlsTypograf.p!(false) }
13
+ setup { AlsTypograf.use_p = false }
14
14
  process_assertions({
15
15
  '- Это "Типограф"?' => "&#151; Это &laquo;Типограф&raquo;?",
16
16
  })
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: als_typograf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Semyonov
@@ -48,6 +48,7 @@ files:
48
48
  - README.rdoc
49
49
  - Rakefile
50
50
  - VERSION
51
+ - als_typograf.gemspec
51
52
  - lib/als_typograf.rb
52
53
  - test/helper.rb
53
54
  - test/test_als_typograf.rb