contact_congress_parser 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/contact_congress_parser.gemspec +23 -0
- data/lib/contact_congress_parser.rb +10 -0
- data/lib/contact_congress_parser/action.rb +72 -0
- data/lib/contact_congress_parser/field.rb +32 -0
- data/lib/contact_congress_parser/parser.rb +43 -0
- data/lib/contact_congress_parser/version.rb +3 -0
- data/spec/action_spec.rb +28 -0
- data/spec/field_spec.rb +26 -0
- data/spec/parser_spec.rb +29 -0
- data/spec/spec_helper.rb +31 -0
- metadata +91 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0851b68700ea62d6a553088b4592eb526b97f54d
|
4
|
+
data.tar.gz: 0b123632d0196d1526b0e383bf92ba443d3bb472
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0ce2e91cb49d293edec0b0d97b090351b0781c803dc688302c1ebdfb5c0915c5c5a563e29b7c77092ffc95f8ffa419a2c8f8608e546cf0ec6dbd5f059c7406d7
|
7
|
+
data.tar.gz: d25330d9fa004fbfca17533e816e897bd898094faeb479eaed9e48196c231f7d43402811c0d6c7a1a55ecae285859ae818668916843e6e3deb5d5644f9c08f6b
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Nikias Kalpaxis
|
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,29 @@
|
|
1
|
+
# ContactCongressParser
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'contact_congress_parser'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install contact_congress_parser
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( http://github.com/<my-github-username>/contact_congress_parser/fork )
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'contact_congress_parser/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "contact_congress_parser"
|
8
|
+
spec.version = ContactCongressParser::VERSION
|
9
|
+
spec.authors = ["Nikias Kalpaxis"]
|
10
|
+
spec.email = ["kalpaxisn@gmail.com"]
|
11
|
+
spec.summary = %q{Parser for https://github.com/unitedstates/contact-congress member files.}
|
12
|
+
spec.description = %q{}
|
13
|
+
spec.homepage = "https://github.com/nhjk/contact_congress_parser"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'debugger'
|
2
|
+
class Action
|
3
|
+
attr_reader :name, :data
|
4
|
+
|
5
|
+
def self.create_from_step(step)
|
6
|
+
name = step.keys.first
|
7
|
+
values = step.values.first
|
8
|
+
|
9
|
+
if values.class == Array
|
10
|
+
values.map { |value| self.new(name, value) }
|
11
|
+
else
|
12
|
+
self.new(name, values)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize(name, data)
|
17
|
+
@name = name
|
18
|
+
@data = data
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_s
|
22
|
+
name == 'wait' ? '' : name + ' ' + send("#{name}_args")
|
23
|
+
end
|
24
|
+
|
25
|
+
def visit_args
|
26
|
+
'"' + path(data) + '"'
|
27
|
+
end
|
28
|
+
|
29
|
+
def find_args
|
30
|
+
to_find = data['value'] || data['selector']
|
31
|
+
'"' + to_find + '"'
|
32
|
+
end
|
33
|
+
|
34
|
+
def fill_in_args
|
35
|
+
'"' + escape_quotes(@data['selector']) + '"' +
|
36
|
+
', with: ' +
|
37
|
+
field(@data['value'])
|
38
|
+
end
|
39
|
+
|
40
|
+
def select_args
|
41
|
+
field(@data['value'], options: @data['options'])
|
42
|
+
end
|
43
|
+
|
44
|
+
def choose_args
|
45
|
+
select_args
|
46
|
+
end
|
47
|
+
|
48
|
+
def check_args
|
49
|
+
'"' + @data['selector'] + '"'
|
50
|
+
end
|
51
|
+
|
52
|
+
def uncheck_args
|
53
|
+
check_args
|
54
|
+
end
|
55
|
+
|
56
|
+
def click_on_args
|
57
|
+
find_args
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
def path(uri)
|
62
|
+
URI.parse(uri).path
|
63
|
+
end
|
64
|
+
|
65
|
+
def field(name, options=nil)
|
66
|
+
Field.new(name, options).to_s
|
67
|
+
end
|
68
|
+
|
69
|
+
def escape_quotes(str)
|
70
|
+
str.gsub('"', '\"')
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
class ContactCongressParser::Field
|
2
|
+
attr_reader :name, :field_options
|
3
|
+
|
4
|
+
def initialize(name, options=nil)
|
5
|
+
@name = name
|
6
|
+
@options = options
|
7
|
+
@field_options = opts_to_array(options[:options]) if options
|
8
|
+
@field_options = escape_opts_quotes(@field_options) if field_options
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_s
|
12
|
+
"field(\"#{name}\"#{options_str})"
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
private
|
17
|
+
def opts_to_array(opts)
|
18
|
+
case opts
|
19
|
+
when Array then opts
|
20
|
+
when Hash then opts.keys
|
21
|
+
when String then [opts]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def escape_opts_quotes(opts)
|
26
|
+
opts.map { |str| str.gsub('"', '\"') }
|
27
|
+
end
|
28
|
+
|
29
|
+
def options_str
|
30
|
+
field_options.nil? ? "" : ", options: [\"" + field_options.join("\", \"") + "\"]"
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'uri'
|
3
|
+
|
4
|
+
class ContactCongressParser::Parser
|
5
|
+
attr_reader :write_io
|
6
|
+
|
7
|
+
def self.parse(read_io, write_io)
|
8
|
+
Parser.new(read_io, write_io).parse
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(read_io, write_io)
|
12
|
+
@yaml = YAML.load(read_io.read)
|
13
|
+
@write_io = write_io
|
14
|
+
end
|
15
|
+
|
16
|
+
def actions
|
17
|
+
@actions ||= steps.map { |step| Action.create_from_step(step) }.flatten
|
18
|
+
end
|
19
|
+
|
20
|
+
def parse
|
21
|
+
create_form do
|
22
|
+
actions.each { |action| write_io.puts tab + action.to_s }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def create_form(&block)
|
27
|
+
write_io.puts("Conformity::Form.new('#{domain}') do")
|
28
|
+
yield
|
29
|
+
write_io.puts("end")
|
30
|
+
end
|
31
|
+
|
32
|
+
def domain
|
33
|
+
@domain ||= URI.parse(steps.first['visit']).host
|
34
|
+
end
|
35
|
+
|
36
|
+
def steps
|
37
|
+
@steps ||= @yaml['contact_form']['steps']
|
38
|
+
end
|
39
|
+
|
40
|
+
def tab
|
41
|
+
' '
|
42
|
+
end
|
43
|
+
end
|
data/spec/action_spec.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
require 'contact_congress_parser/action'
|
3
|
+
|
4
|
+
|
5
|
+
include ContactCongressParser
|
6
|
+
describe Action do
|
7
|
+
describe 'create_from_step' do
|
8
|
+
it 'creates a single action for single val steps' do
|
9
|
+
action = Action.create_from_step(SINGLE_ACT_STEP)
|
10
|
+
expect(action.class).to be(Action)
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'creates multiple actions for multi val steps' do
|
14
|
+
actions = Action.create_from_step(MULTI_ACT_STEP)
|
15
|
+
expect(actions.all? { |action| action.class == Action }).to be_truthy
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe '#to_s' do
|
20
|
+
it 'is eval-able by conformity' do
|
21
|
+
form = Conformity::Form.new
|
22
|
+
|
23
|
+
actions = Action.create_from_step(MULTI_ACT_STEP)
|
24
|
+
expect { actions.each { |action| form.send(:eval, action.to_s) } }.
|
25
|
+
to_not raise_error
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/spec/field_spec.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
require 'contact_congress_parser/field'
|
3
|
+
|
4
|
+
|
5
|
+
include ContactCongressParser
|
6
|
+
describe Field do
|
7
|
+
describe '#to_s' do
|
8
|
+
it 'outputs a valid field' do
|
9
|
+
field = Field.new('name')
|
10
|
+
expect(field.to_s).to match("field(\"name\")")
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'handles options properly' do
|
14
|
+
field = Field.new('state', options: ['NY', 'CA'])
|
15
|
+
expect(field.to_s).to match("field(\"state\", options: [\"NY\", \"CA\"])")
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'handles options with non-standard classes properly' do
|
19
|
+
hash = Field.new('f', options: {'Opt' => 'Opt'})
|
20
|
+
string = Field.new('f', options: 'Opt')
|
21
|
+
|
22
|
+
expect { hash.to_s }.to_not raise_error
|
23
|
+
expect { string.to_s }.to_not raise_error
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/spec/parser_spec.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
require 'git'
|
3
|
+
require 'debugger'
|
4
|
+
|
5
|
+
|
6
|
+
clone_from = 'https://github.com/nhjk/contact-congress.git'
|
7
|
+
clone_to = 'contact-congress'
|
8
|
+
|
9
|
+
describe ContactCongressParser do
|
10
|
+
before :context do
|
11
|
+
Git.clone(clone_from, clone_to)
|
12
|
+
@member_files = Dir.glob("#{clone_to}/members/*.yaml")
|
13
|
+
raise "Error retrieving member_files" if @member_files.empty?
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'parses member yaml files into forms runnable by conformity' do
|
17
|
+
@member_files.each do |member_file|
|
18
|
+
member_io = File.open(member_file)
|
19
|
+
s_io = StringIO.new
|
20
|
+
ContactCongressParser.parse(member_io, s_io)
|
21
|
+
expect { Conformity.send(:eval, s_io.string) }.to_not raise_error
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
after :context do
|
26
|
+
`rm -rf #{clone_to}`
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
|
4
|
+
|
5
|
+
require 'yaml'
|
6
|
+
require 'conformity'
|
7
|
+
require 'contact_congress_parser'
|
8
|
+
|
9
|
+
|
10
|
+
REP = File.expand_path('../member_files/representative.yaml', __FILE__)
|
11
|
+
SEN = File.expand_path('../member_files/senator.yaml', __FILE__)
|
12
|
+
|
13
|
+
SINGLE_ACT_STEP = YAML.load(
|
14
|
+
<<-STEP
|
15
|
+
visit: "https://meng.house.gov/contact/email-me"
|
16
|
+
STEP
|
17
|
+
)
|
18
|
+
|
19
|
+
MULTI_ACT_STEP = YAML.load(
|
20
|
+
<<-STEP
|
21
|
+
fill_in:
|
22
|
+
- name: required-zip5
|
23
|
+
selector: "#required-zip5"
|
24
|
+
value: $ADDRESS_ZIP5
|
25
|
+
required: true
|
26
|
+
- name: zip4
|
27
|
+
selector: "#zip4"
|
28
|
+
value: $ADDRESS_ZIP4
|
29
|
+
required: true
|
30
|
+
STEP
|
31
|
+
)
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: contact_congress_parser
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nikias Kalpaxis
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: ''
|
42
|
+
email:
|
43
|
+
- kalpaxisn@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- contact_congress_parser.gemspec
|
54
|
+
- lib/contact_congress_parser.rb
|
55
|
+
- lib/contact_congress_parser/action.rb
|
56
|
+
- lib/contact_congress_parser/field.rb
|
57
|
+
- lib/contact_congress_parser/parser.rb
|
58
|
+
- lib/contact_congress_parser/version.rb
|
59
|
+
- spec/action_spec.rb
|
60
|
+
- spec/field_spec.rb
|
61
|
+
- spec/parser_spec.rb
|
62
|
+
- spec/spec_helper.rb
|
63
|
+
homepage: https://github.com/nhjk/contact_congress_parser
|
64
|
+
licenses:
|
65
|
+
- MIT
|
66
|
+
metadata: {}
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 2.2.2
|
84
|
+
signing_key:
|
85
|
+
specification_version: 4
|
86
|
+
summary: Parser for https://github.com/unitedstates/contact-congress member files.
|
87
|
+
test_files:
|
88
|
+
- spec/action_spec.rb
|
89
|
+
- spec/field_spec.rb
|
90
|
+
- spec/parser_spec.rb
|
91
|
+
- spec/spec_helper.rb
|