als_typograf 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -21,3 +21,5 @@ doc
21
21
  pkg
22
22
 
23
23
  ## PROJECT::SPECIFIC
24
+ test/*.log
25
+ test/*.db
data/README.rdoc CHANGED
@@ -12,8 +12,18 @@ Example:
12
12
  require 'als_typograf'
13
13
  puts AlsTypograf.process('"Вы все еще кое-как верстаете в "Ворде"? - Тогда мы идем к вам!"')
14
14
 
15
+ class Article < ActiveRecord::Base
16
+ typograf :title, :use_br => false, :use_p => false
17
+ typograf :content
18
+ typograf :skills, :achievements, :description, :encoding => 'UTF-16'
19
+ typograf :foo => {:use_br => false},
20
+ :bar => {:use_p => false},
21
+ :baz => {:entity_type => AlsTypograf::NO_ENTITIES}
22
+ end
23
+
24
+
15
25
  == Note on Patches/Pull Requests
16
-
26
+
17
27
  * Fork the project.
18
28
  * Make your feature addition or bug fix.
19
29
  * Add tests for it. This is important so I don't break it in a
data/Rakefile CHANGED
@@ -14,7 +14,6 @@ begin
14
14
  gem.add_dependency 'httparty', '>= 0.4.5'
15
15
  gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
16
16
  gem.add_development_dependency "yard", ">= 0"
17
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
17
  end
19
18
  Jeweler::GemcutterTasks.new
20
19
  rescue LoadError
@@ -41,9 +40,30 @@ rescue LoadError
41
40
  end
42
41
  end
43
42
 
44
- task :test => :check_dependencies
43
+ begin
44
+ require 'reek/adapters/rake_task'
45
+ Reek::RakeTask.new do |t|
46
+ t.fail_on_error = true
47
+ t.verbose = false
48
+ t.source_files = 'lib/**/*.rb'
49
+ end
50
+ rescue LoadError
51
+ task :reek do
52
+ abort "Reek is not available. In order to run reek, you must: sudo gem install reek"
53
+ end
54
+ end
45
55
 
46
- task :default => :test
56
+ begin
57
+ require 'roodi'
58
+ require 'roodi_task'
59
+ RoodiTask.new do |t|
60
+ t.verbose = false
61
+ end
62
+ rescue LoadError
63
+ task :roodi do
64
+ abort "Roodi is not available. In order to run roodi, you must: sudo gem install roodi"
65
+ end
66
+ end
47
67
 
48
68
  begin
49
69
  require 'yard'
@@ -53,3 +73,8 @@ rescue LoadError
53
73
  abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
54
74
  end
55
75
  end
76
+
77
+ task :test => :check_dependencies
78
+ task :default => :test
79
+ desc 'Push release to github and gemcutter and install gem in the system'
80
+ task :push => %w(release gemcutter:release install)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
data/als_typograf.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{als_typograf}
8
- s.version = "0.0.3"
8
+ s.version = "0.0.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Alexander Semyonov"]
12
- s.date = %q{2009-11-24}
12
+ s.date = %q{2009-11-26}
13
13
  s.description = %q{ruby-implementation of ArtLebedevStudio.RemoteTypograf class (web-service client)}
14
14
  s.email = %q{rotuka@rotuka.com}
15
15
  s.extra_rdoc_files = [
@@ -25,8 +25,14 @@ Gem::Specification.new do |s|
25
25
  "VERSION",
26
26
  "als_typograf.gemspec",
27
27
  "lib/als_typograf.rb",
28
+ "lib/als_typograf/active_record.rb",
29
+ "lib/als_typograf/request.rb",
30
+ "rails/init.rb",
31
+ "test/database.yml",
28
32
  "test/helper.rb",
29
- "test/test_als_typograf.rb"
33
+ "test/schema.rb",
34
+ "test/test_als_typograf.rb",
35
+ "test/test_article.rb"
30
36
  ]
31
37
  s.homepage = %q{http://github.com/rotuka/als_typograf}
32
38
  s.rdoc_options = ["--charset=UTF-8"]
@@ -35,7 +41,9 @@ Gem::Specification.new do |s|
35
41
  s.summary = %q{ArtLebedevStudio.RemoteTypograf}
36
42
  s.test_files = [
37
43
  "test/helper.rb",
38
- "test/test_als_typograf.rb"
44
+ "test/test_als_typograf.rb",
45
+ "test/schema.rb",
46
+ "test/test_article.rb"
39
47
  ]
40
48
 
41
49
  if s.respond_to? :specification_version then
data/lib/als_typograf.rb CHANGED
@@ -2,54 +2,18 @@ $KCODE = 'u'
2
2
 
3
3
  require 'activesupport'
4
4
  require 'httparty'
5
+ require 'als_typograf/request'
5
6
 
6
7
  # ruby-implementation of ArtLebedevStudio.RemoteTypograf class (web-service client)
7
8
  # @author Alexander Semyonov
8
9
  module AlsTypograf
9
- # The request class
10
- class Request
11
- include HTTParty
12
- base_uri 'typograf.artlebedev.ru'
13
- format :xml
14
- headers('Content-Type' => 'text/xml',
15
- 'Host' => 'typograf.artlebedev.ru',
16
- 'SOAPAction' => '"http://typograf.artlebedev.ru/webservices/ProcessText"')
17
-
18
- # Process text with remote web-service
19
- # @param [String] text to process
20
- # @param [Hash] options for web-service
21
- def process_text(text, options = {})
22
- prepared_text = text.gsub(/&/, '&amp;').
23
- gsub(/</, '&lt;').
24
- gsub(/>/, '&gt;')
25
- soap_request = <<-END_SOAP
26
- <?xml version="1.0" encoding="#{options[:encoding]}" ?>
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/">
28
- <soap:Body>
29
- <ProcessText xmlns="http://typograf.artlebedev.ru/webservices/">
30
- <text>#{prepared_text}</text>
31
- <entityType>#{options[:entity_type]}</entityType>
32
- <useBr>#{options[:use_br]}</useBr>
33
- <useP>#{options[:use_p]}</useP>
34
- <maxNobr>#{options[:max_nobr]}</maxNobr>
35
- </ProcessText>
36
- </soap:Body>
37
- </soap:Envelope>
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
48
- end
49
- end
10
+ HTML_ENTITIES = 1
11
+ XML_ENTITIES = 2
12
+ NO_ENTITIES = 3
13
+ MIXED_ENTITIES = 4
50
14
 
51
15
  DEFAULT_OPTIONS = {
52
- :entity_type => 4,
16
+ :entity_type => NO_ENTITIES,
53
17
  :use_br => 1,
54
18
  :use_p => 1,
55
19
  :max_nobr => 3,
@@ -61,14 +25,14 @@ module AlsTypograf
61
25
  @@options = DEFAULT_OPTIONS.dup
62
26
 
63
27
  # Get a global AlsTypograf option
64
- # @param [String, Symbol] option name
28
+ # @param [String, Symbol] param option name
65
29
  def self.[](param)
66
30
  self.options[param.to_sym]
67
31
  end
68
32
 
69
33
  # Set a global AlsTypograf option
70
- # @param [String, Symbol] option name
71
- # @param [Numeric, String] value for the option
34
+ # @param [String, Symbol] param option name
35
+ # @param [Numeric, String] value value for the option
72
36
  def self.[]=(param, value)
73
37
  self.options[param.to_sym] = value
74
38
  end
@@ -80,38 +44,38 @@ module AlsTypograf
80
44
 
81
45
  # Set option #entity_type to HTML (e. g. &nbsp;, &mdash;)
82
46
  def self.html_entities!
83
- self[:entity_type] = 1
47
+ self[:entity_type] = HTML_ENTITIES
84
48
  end
85
49
 
86
50
  # Set option #entity_type to XML (e. g. &#169;, &#155;)
87
51
  def self.xml_entities!
88
- self[:entity_type] = 2
52
+ self[:entity_type] = XML_ENTITIES
89
53
  end
90
54
 
91
55
  # Set option #entity_type to nothing (e. g. —, ©, ×)
92
56
  def self.no_entities!
93
- self[:entity_type] = 3
57
+ self[:entity_type] = NO_ENTITIES
94
58
  end
95
59
 
96
60
  # Set option #entity_type to mixed (e. g. &#169;, &nbsp;)
97
61
  def self.mixed_entities!
98
- self[:entity_type] = 4
62
+ self[:entity_type] = MIXED_ENTITIES
99
63
  end
100
64
 
101
65
  # Option to replace \n with +<br />+
102
- # @param [Boolean] to use +<br />+ or not
66
+ # @param [Boolean] value to use +<br />+ or not
103
67
  def self.use_br=(value)
104
68
  self[:use_br] = value ? 1 : 0
105
69
  end
106
70
 
107
71
  # Option to wrap paragraphs with +<p></p>+
108
- # @param [Boolean] to wrap para or not
72
+ # @param [Boolean] value to wrap para or not
109
73
  def self.use_p=(value)
110
74
  self[:use_p] = value ? 1 : 0
111
75
  end
112
76
 
113
77
  # How many symbols around dash to surround with +<nobr>+ tag
114
- # @param [Numeric, Boolean] symbols count
78
+ # @param [Numeric, Boolean] value symbols count
115
79
  def self.max_nobr=(value)
116
80
  self[:max_nobr] = value ? value : 0
117
81
  end
@@ -122,17 +86,24 @@ module AlsTypograf
122
86
  self[$1.to_sym] = args.first
123
87
  when /^(#{VALID_OPTIONS})$/
124
88
  self[method_name.to_sym]
89
+ else
90
+ super(method_name, *args)
125
91
  end
126
92
  end
127
93
 
128
94
  # Process text with Typograf web-service
129
- # @param [String] text to process
95
+ # @param [String] text text to process
130
96
  # @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
97
+ # @option custom_options [String] :entity_type (NO_ENTITIES) Type of entities to use in processed text
98
+ # @option custom_options [String] :use_br (1) Whether or not to use +<br />+
99
+ # @option custom_options [String] :use_p (1) Whether or not to surround paras with +<p></p>+
100
+ # @option custom_options [String] :max_nobr (3) How many symbols around dash to surround with +<nobr>+ tag
101
+ # @option custom_options [String] :encoding ('UTF-8') Encoding of text
135
102
  def self.process(text, custom_options = {})
136
103
  Request.new.process_text(text, custom_options.reverse_merge(options))
137
104
  end
138
105
  end
106
+
107
+ if defined? ActiveRecord
108
+ require 'als_typograf/active_record'
109
+ end
@@ -0,0 +1,57 @@
1
+ module AlsTypograf
2
+ module ActiveRecord
3
+ # Run AlsTypograf#process on configured columns.
4
+ # Used to run as before_validation filter (automaticaly added on ActiveRecord::Base.typograf)
5
+ def typograf_current_fields
6
+ self.class.typograf_fields.each do |column, options|
7
+ send(:"#{column}=",
8
+ AlsTypograf.process(send(column).to_s,
9
+ options))
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ class ActiveRecord::Base
16
+ # Specify columns to typograf
17
+ # @overload typograf(column_name, options)
18
+ # Typograf only specified column with specified or default options
19
+ # @example typograf column with default options
20
+ # typograf :content # will typograf +content+ with default AlsTypograf options
21
+ # @example typograf column with custom options
22
+ # typograf :title, :use_br => false # will typograf +title+ without replacing +\n+ with <br />
23
+ # @param [String, Symbol] column_name name of the column to typograf
24
+ # @param [Hash] options custom options to AlsTypograf#process method
25
+ # @overload typograf(column1, column2, ..., options)
26
+ # Typograf every specified columns with default options
27
+ # @example typograf columns with common custom options
28
+ # typograf :skills, :achievements, :description, :encoding => 'CP1251'
29
+ # @param [String, Symbol] column1 first column name
30
+ # @param [String, Symbol] column2 next column name
31
+ # @param [String, Symbol] ... specify all columns, you need
32
+ # @param [Hash] options options to AlsTypograf#process method for all specified fields
33
+ # @overload typograf(columns_with_options)
34
+ # Typograf specified columns with specified options
35
+ # @example typograf specified columns with specified options
36
+ # typograf :foo => {:use_br => false}, :bar => {:use_p => false}
37
+ # @param [Hash] columns_with_options column names with correspond options to AlsTypograf#process method
38
+ def self.typograf(*columns)
39
+ unless respond_to?(:typograf_fields)
40
+ cattr_accessor :typograf_fields
41
+ self.typograf_fields = {}
42
+ before_validation :typograf_current_fields
43
+ include AlsTypograf::ActiveRecord
44
+ end
45
+
46
+ common_options = columns.extract_options!
47
+ if columns == [] # There was an columns_with_options variant
48
+ common_options.each do |column, custom_options|
49
+ typograf_fields[column.to_sym] = custom_options
50
+ end
51
+ else
52
+ columns.each do |column|
53
+ typograf_fields[column.to_sym] = common_options
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,35 @@
1
+ module AlsTypograf
2
+ # The request class
3
+ class Request
4
+ include HTTParty
5
+ base_uri 'typograf.artlebedev.ru'
6
+ format :xml
7
+ headers('Content-Type' => 'text/xml',
8
+ 'Host' => 'typograf.artlebedev.ru',
9
+ 'SOAPAction' => '"http://typograf.artlebedev.ru/webservices/ProcessText"')
10
+
11
+ # Process text with remote web-service
12
+ # @param [String] text text to process
13
+ # @param [Hash] options options for web-service
14
+ def process_text(text, options = {})
15
+ soap_request = <<-END_SOAP
16
+ <?xml version="1.0" encoding="#{options[:encoding]}" ?>
17
+ <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/">
18
+ <soap:Body>
19
+ <ProcessText xmlns="http://typograf.artlebedev.ru/webservices/">
20
+ <text>#{text.gsub(/&/, '&amp;').gsub(/</, '&lt;').gsub(/>/, '&gt;')}</text>
21
+ <entityType>#{options[:entity_type]}</entityType>
22
+ <useBr>#{options[:use_br]}</useBr>
23
+ <useP>#{options[:use_p]}</useP>
24
+ <maxNobr>#{options[:max_nobr]}</maxNobr>
25
+ </ProcessText>
26
+ </soap:Body>
27
+ </soap:Envelope>
28
+ END_SOAP
29
+ response = self.class.post('/webservices/typograf.asmx', :body => soap_request)
30
+ response['soap:Envelope']['soap:Body']['ProcessTextResponse']['ProcessTextResult'].gsub(/&amp;/, '&').gsub(/&lt;/, '<').gsub(/&gt;/, '>').gsub(/\t$/, '')
31
+ rescue ::Exception => exception
32
+ return text
33
+ end
34
+ end
35
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'als_typograf'
data/test/database.yml ADDED
@@ -0,0 +1,21 @@
1
+ sqlite:
2
+ :adapter: sqlite
3
+ :database: vendor/plugins/als_typograf/test/als_typograf_plugin.sqlite.db
4
+
5
+ sqlite3:
6
+ :adapter: sqlite3
7
+ :database: vendor/plugins/als_typograf/test/als_typograf_plugin.sqlite3.db
8
+
9
+ postgresql:
10
+ :adapter: postgresql
11
+ :username: postgres
12
+ :password: postgres
13
+ :database: als_typograf_plugin_test
14
+ :min_messages: ERROR
15
+
16
+ mysql:
17
+ :adapter: mysql
18
+ :host: localhost
19
+ :username: root
20
+ :password: password
21
+ :database: als_typograf_plugin_test
data/test/helper.rb CHANGED
@@ -1,7 +1,11 @@
1
+ ENV['RAILS_ENV'] = 'test'
2
+ ENV['RAILS_ROOT'] ||= File.join(File.dirname(__FILE__), '../../../..')
3
+
1
4
  require 'rubygems'
2
5
  require 'test/unit'
3
6
  require 'shoulda'
4
7
  require 'redgreen' rescue nil
8
+ require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
5
9
 
6
10
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
11
  $LOAD_PATH.unshift(File.dirname(__FILE__))
@@ -20,3 +24,33 @@ class Test::Unit::TestCase
20
24
  AlsTypograf.process(text)
21
25
  end
22
26
  end
27
+
28
+ def load_schema
29
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
30
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
31
+
32
+ db_adapter = ENV['DB']
33
+
34
+ # no db passed, try one of these fine config-free DBs before bombing.
35
+ db_adapter ||=
36
+ begin
37
+ require 'rubygems'
38
+ require 'sqlite'
39
+ 'sqlite'
40
+ rescue MissingSourceFile
41
+ begin
42
+ require 'sqlite3'
43
+ 'sqlite3'
44
+ rescue MissingSourceFile
45
+ end
46
+ end
47
+
48
+ if db_adapter.nil?
49
+ raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
50
+ end
51
+
52
+ ActiveRecord::Base.establish_connection(config[db_adapter])
53
+ load(File.dirname(__FILE__) + "/schema.rb")
54
+ require File.dirname(__FILE__) + '/../rails/init.rb'
55
+ end
56
+
data/test/schema.rb ADDED
@@ -0,0 +1,7 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :articles, :force => true do |t|
3
+ t.string :title
4
+ t.text :content, :skills, :achievements, :description,
5
+ :foo, :bar, :baz
6
+ end
7
+ end
@@ -1,18 +1,23 @@
1
1
  require 'helper'
2
2
 
3
3
  class TestAlsTypograf < Test::Unit::TestCase
4
+
5
+ class Article < ActiveRecord::Base
6
+ end
7
+
4
8
  # TODO: need more tests or not?
5
9
  context 'with default configuration' do
6
10
  setup { AlsTypograf.default_options! }
7
11
  process_assertions({
8
- '- Это "Типограф"?' => "<p>&#151; Это &laquo;Типограф&raquo;?</p>",
12
+ '- Это "Типограф"?' => "<p>— Это «Типограф»?</p>",
9
13
  })
10
14
  end
11
15
 
12
16
  context 'no p' do
13
17
  setup { AlsTypograf.use_p = false }
14
18
  process_assertions({
15
- '- Это "Типограф"?' => "&#151; Это &laquo;Типограф&raquo;?",
19
+ '- Это "Типограф"?' => " Это «Типограф»?",
16
20
  })
17
21
  end
22
+
18
23
  end
@@ -0,0 +1,38 @@
1
+ require 'helper'
2
+ require 'als_typograf/active_record'
3
+ require 'pp'
4
+
5
+ load_schema
6
+
7
+ class Article < ActiveRecord::Base
8
+ typograf(:content)
9
+ typograf(:title => {:use_p => false, :use_br => false})
10
+ typograf(:skills, :achievements, :description, :use_br => false)
11
+ end
12
+
13
+ class TestArticle < Test::Unit::TestCase
14
+ def self.described_type; Article end
15
+
16
+ should_have_class_methods :typograf
17
+ should_have_instance_methods :typograf_fields
18
+
19
+ should 'load schema correctly' do
20
+ assert_equal [], Article.all
21
+ end
22
+
23
+ context 'with an Article' do
24
+ setup do
25
+ @article = Article.create(:title => '- Does it "Article"?',
26
+ :content => 'Yes, this is an "Article"...')
27
+ AlsTypograf.default_options!
28
+ end
29
+
30
+ should 'typograf article’s content with default options' do
31
+ assert_equal '<p>Yes, this is an «Article»…</p>', @article.content
32
+ end
33
+
34
+ should 'typograf article’s title with custom options' do
35
+ assert_equal '— Does it «Article»?', @article.title
36
+ end
37
+ end
38
+ end
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.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Semyonov
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-24 00:00:00 +03:00
12
+ date: 2009-11-26 00:00:00 +03:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -70,8 +70,14 @@ files:
70
70
  - VERSION
71
71
  - als_typograf.gemspec
72
72
  - lib/als_typograf.rb
73
+ - lib/als_typograf/active_record.rb
74
+ - lib/als_typograf/request.rb
75
+ - rails/init.rb
76
+ - test/database.yml
73
77
  - test/helper.rb
78
+ - test/schema.rb
74
79
  - test/test_als_typograf.rb
80
+ - test/test_article.rb
75
81
  has_rdoc: true
76
82
  homepage: http://github.com/rotuka/als_typograf
77
83
  licenses: []
@@ -103,3 +109,5 @@ summary: ArtLebedevStudio.RemoteTypograf
103
109
  test_files:
104
110
  - test/helper.rb
105
111
  - test/test_als_typograf.rb
112
+ - test/schema.rb
113
+ - test/test_article.rb