excise 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +9 -0
- data/Guardfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +37 -0
- data/Rakefile +6 -0
- data/excise.gemspec +20 -0
- data/lib/excise/base.rb +35 -0
- data/lib/excise/version.rb +3 -0
- data/lib/excise.rb +9 -0
- data/spec/excise/base_spec.rb +16 -0
- data/spec/excise_spec.rb +12 -0
- data/spec/spec_helper.rb +9 -0
- metadata +78 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Ezekiel Templin
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# Excise [![Build Status](https://secure.travis-ci.org/ezkl/excise.png)](http://travis-ci.org/ezkl/excise)
|
2
|
+
> This is a simple helper to extract values from a string based on a pattern.
|
3
|
+
|
4
|
+
A Ruby port of [laktek's](https://github.com/laktek/) [extract-values](https://github.com/laktek/extract-values).
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'excise'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install excise
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
require 'excise'
|
24
|
+
|
25
|
+
Excise.parse('About 49,000,000 results (0.15 seconds)',
|
26
|
+
'About {result_count} results ({load_time} seconds)')
|
27
|
+
|
28
|
+
#=> {:result_count=>"49,000,000", :load_time=>"0.15"}
|
29
|
+
```
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
1. Fork it
|
34
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
35
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
36
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
37
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/excise.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'excise/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "excise"
|
8
|
+
gem.version = Excise::VERSION
|
9
|
+
gem.authors = ["Ezekiel Templin"]
|
10
|
+
gem.email = ["zeke@templ.in"]
|
11
|
+
gem.description = %q{Excise is a Ruby port of extract-values}
|
12
|
+
gem.summary = %q{From extract-values description: "... a simple helper to extract values from a string based on a pattern."}
|
13
|
+
gem.homepage = "http://zeke.templ.in/excise"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.test_files = gem.files.grep('spec')
|
17
|
+
gem.require_paths = ["lib"]
|
18
|
+
|
19
|
+
gem.add_development_dependency 'rspec', '~> 2.11.0'
|
20
|
+
end
|
data/lib/excise/base.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Excise
|
4
|
+
class Base
|
5
|
+
def initialize(string, pattern)
|
6
|
+
@string = string
|
7
|
+
@pattern = pattern
|
8
|
+
@matches = pattern_expression.match(@string)
|
9
|
+
@output = {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def parse
|
13
|
+
tokens.each_with_index do |key, index|
|
14
|
+
@output[key.to_sym] = @matches[index+1]
|
15
|
+
end
|
16
|
+
@output
|
17
|
+
end
|
18
|
+
|
19
|
+
def tokens
|
20
|
+
@pattern.scan(token_expression).flatten
|
21
|
+
end
|
22
|
+
|
23
|
+
def pattern_expression
|
24
|
+
Regexp.new(@pattern.gsub(special_characters_expression, '\\\\\0').gsub(token_expression, "(\.+)"))
|
25
|
+
end
|
26
|
+
|
27
|
+
def token_expression
|
28
|
+
/{([^{}\t\r\n]+)}/
|
29
|
+
end
|
30
|
+
|
31
|
+
def special_characters_expression
|
32
|
+
/[\\\^\$\*\+\.\?\(\)\[\]]/
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/excise.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
module Excise
|
4
|
+
describe "Base" do
|
5
|
+
context 'patterned string' do
|
6
|
+
subject { Base.new(string, pattern).parse }
|
7
|
+
|
8
|
+
let(:string) { '[this] (patterned) <STRING>' }
|
9
|
+
let(:pattern) { '[{first}] ({second}) <{third}>' }
|
10
|
+
|
11
|
+
it { should be_a Hash }
|
12
|
+
its(:keys) { should eq [:first, :second, :third] }
|
13
|
+
its(:values) { should eq ['this', 'patterned', 'STRING'] }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/spec/excise_spec.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Excise do
|
4
|
+
describe ".parse" do
|
5
|
+
subject { Excise.parse(string, pattern) }
|
6
|
+
|
7
|
+
let(:string) { '[this] (patterned) <STRING>' }
|
8
|
+
let(:pattern) { '[{first}] ({second}) <{third}>' }
|
9
|
+
|
10
|
+
it { should be_a Hash }
|
11
|
+
end
|
12
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: excise
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ezekiel Templin
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-29 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.11.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: 2.11.0
|
30
|
+
description: Excise is a Ruby port of extract-values
|
31
|
+
email:
|
32
|
+
- zeke@templ.in
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- .rspec
|
39
|
+
- .travis.yml
|
40
|
+
- Gemfile
|
41
|
+
- Guardfile
|
42
|
+
- LICENSE.txt
|
43
|
+
- README.md
|
44
|
+
- Rakefile
|
45
|
+
- excise.gemspec
|
46
|
+
- lib/excise.rb
|
47
|
+
- lib/excise/base.rb
|
48
|
+
- lib/excise/version.rb
|
49
|
+
- spec/excise/base_spec.rb
|
50
|
+
- spec/excise_spec.rb
|
51
|
+
- spec/spec_helper.rb
|
52
|
+
homepage: http://zeke.templ.in/excise
|
53
|
+
licenses: []
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.8.23
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: ! 'From extract-values description: "... a simple helper to extract values
|
76
|
+
from a string based on a pattern."'
|
77
|
+
test_files: []
|
78
|
+
has_rdoc:
|