contact_congress_parser 0.0.7 → 0.0.8
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 +4 -4
- data/lib/contact_congress_parser.rb +8 -2
- data/lib/contact_congress_parser/form.rb +50 -0
- data/lib/contact_congress_parser/parser.rb +7 -32
- data/lib/contact_congress_parser/success_conditions.rb +31 -0
- data/lib/contact_congress_parser/version.rb +1 -1
- data/spec/form_spec.rb +28 -0
- data/spec/parser_spec.rb +12 -22
- data/spec/spec_helper.rb +1 -0
- data/spec/success_conditions_spec.rb +18 -0
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b0f8d3f863eccc838fda533ac01475652568e7eb
|
4
|
+
data.tar.gz: a69134a888c381218e7edad074ee4e5a2cb428a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c857664dcbe3b7c3f6cc0988812d48b02c7a21288fb431448762985cc606cb28520a2d657994443a48b560b2182d65c65543be702a5c02cabda3e8b015cf7c68
|
7
|
+
data.tar.gz: 7547db2184ee54fbd4025a9d11203e9341e1e62835f5366eee412a962bd8ff1e627983f177a8c9d18979544c3d4ad44705edc8aa9af942ec179f9901f1f8f051
|
@@ -4,12 +4,18 @@ require 'yaml'
|
|
4
4
|
require 'uri'
|
5
5
|
|
6
6
|
require 'contact_congress_parser/parser'
|
7
|
+
require 'contact_congress_parser/form'
|
7
8
|
require 'contact_congress_parser/action'
|
8
9
|
require 'contact_congress_parser/field'
|
10
|
+
require 'contact_congress_parser/success_conditions'
|
9
11
|
|
10
12
|
|
11
13
|
module ContactCongressParser
|
12
|
-
def self.
|
13
|
-
Parser.
|
14
|
+
def self.parse_form(yaml_str)
|
15
|
+
Parser.new(yaml_str).form
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.parse_bioguide(yaml_str)
|
19
|
+
Parser.new(yaml_str).bioguide
|
14
20
|
end
|
15
21
|
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module ContactCongressParser
|
2
|
+
class Form
|
3
|
+
def initialize(data)
|
4
|
+
@data = data
|
5
|
+
end
|
6
|
+
|
7
|
+
def to_s
|
8
|
+
create_form do
|
9
|
+
actions.map { |action| tab + action.to_s }.join(newline) +
|
10
|
+
newline +
|
11
|
+
success_conditions.to_s
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
def create_form(&block)
|
17
|
+
"Conformity::Form.new('#{domain}') do" +
|
18
|
+
yield +
|
19
|
+
'end'
|
20
|
+
end
|
21
|
+
|
22
|
+
def domain
|
23
|
+
@domain ||= URI.parse(steps.first['visit']).host
|
24
|
+
end
|
25
|
+
|
26
|
+
def actions
|
27
|
+
@actions ||= steps.map { |step| Action.create_from_step(step) }.flatten
|
28
|
+
end
|
29
|
+
|
30
|
+
def success_conditions
|
31
|
+
@success_conditions ||= SuccessConditions.new(success_data)
|
32
|
+
end
|
33
|
+
|
34
|
+
def steps
|
35
|
+
@steps ||= @data['contact_form']['steps']
|
36
|
+
end
|
37
|
+
|
38
|
+
def success_data
|
39
|
+
@success ||= @data['contact_form']['success']
|
40
|
+
end
|
41
|
+
|
42
|
+
def newline
|
43
|
+
"\n"
|
44
|
+
end
|
45
|
+
|
46
|
+
def tab
|
47
|
+
' '
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -1,42 +1,17 @@
|
|
1
1
|
module ContactCongressParser
|
2
2
|
class Parser
|
3
|
-
attr_reader :
|
3
|
+
attr_reader :data
|
4
4
|
|
5
|
-
def
|
6
|
-
|
5
|
+
def initialize(yaml_str)
|
6
|
+
@data = YAML.load(yaml_str)
|
7
7
|
end
|
8
8
|
|
9
|
-
def
|
10
|
-
@
|
11
|
-
@write_io = write_io
|
9
|
+
def bioguide
|
10
|
+
@data['bioguide']
|
12
11
|
end
|
13
12
|
|
14
|
-
def
|
15
|
-
|
16
|
-
end
|
17
|
-
|
18
|
-
def parse
|
19
|
-
create_form do
|
20
|
-
actions.each { |action| write_io.puts tab + action.to_s }
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def create_form(&block)
|
25
|
-
write_io.puts("Conformity::Form.new('#{domain}') do")
|
26
|
-
yield
|
27
|
-
write_io.puts("end")
|
28
|
-
end
|
29
|
-
|
30
|
-
def domain
|
31
|
-
@domain ||= URI.parse(steps.first['visit']).host
|
32
|
-
end
|
33
|
-
|
34
|
-
def steps
|
35
|
-
@steps ||= @yaml['contact_form']['steps']
|
36
|
-
end
|
37
|
-
|
38
|
-
def tab
|
39
|
-
' '
|
13
|
+
def form
|
14
|
+
Form.new(data).to_s
|
40
15
|
end
|
41
16
|
end
|
42
17
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module ContactCongressParser
|
2
|
+
class SuccessConditions
|
3
|
+
def initialize(data)
|
4
|
+
@data = data
|
5
|
+
end
|
6
|
+
|
7
|
+
def to_s
|
8
|
+
(has_content_str ? ' ' + has_content_str + "\n" : '') +
|
9
|
+
(has_status_code_str ? ' ' + has_status_code_str + "\n" : '')
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
attr_reader :data
|
14
|
+
|
15
|
+
def has_content_str
|
16
|
+
"has_content? \"#{content}\"" if content
|
17
|
+
end
|
18
|
+
|
19
|
+
def has_status_code_str
|
20
|
+
"has_status_code? #{status_code}" if status_code
|
21
|
+
end
|
22
|
+
|
23
|
+
def content
|
24
|
+
data.fetch('body', {}).fetch('contains', nil)
|
25
|
+
end
|
26
|
+
|
27
|
+
def status_code
|
28
|
+
data.fetch('headers', {}).fetch('status', nil)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/spec/form_spec.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
clone_from = 'https://github.com/nhjk/contact-congress.git'
|
5
|
+
clone_to = 'contact-congress'
|
6
|
+
|
7
|
+
|
8
|
+
include ContactCongressParser
|
9
|
+
describe Form 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 'outputs forms parsable by conformity' do
|
17
|
+
@member_files.each do |member_file|
|
18
|
+
data = YAML.load(File.read(member_file))
|
19
|
+
form = Form.new(data)
|
20
|
+
|
21
|
+
expect { Conformity.send(:eval, form.to_s) }.to_not raise_error
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
after :context do
|
26
|
+
`rm -rf #{clone_to}`
|
27
|
+
end
|
28
|
+
end
|
data/spec/parser_spec.rb
CHANGED
@@ -1,28 +1,18 @@
|
|
1
1
|
require_relative 'spec_helper'
|
2
|
-
require 'git'
|
3
2
|
|
4
3
|
|
5
|
-
|
6
|
-
|
4
|
+
describe Parser do
|
5
|
+
it 'parses the bioguide properly' do
|
6
|
+
member_yaml = <<-YAML
|
7
|
+
bioguide: A999999
|
8
|
+
contact_form:
|
9
|
+
method: POST
|
10
|
+
action: ""
|
11
|
+
steps: ""
|
12
|
+
success: ""
|
13
|
+
YAML
|
7
14
|
|
8
|
-
|
9
|
-
|
10
|
-
Git.clone(clone_from, clone_to)
|
11
|
-
@member_files = Dir.glob("#{clone_to}/members/*.yaml")
|
12
|
-
raise "Error retrieving member_files" if @member_files.empty?
|
13
|
-
end
|
14
|
-
|
15
|
-
it 'parses member yaml files into forms runnable by conformity' do
|
16
|
-
@member_files.each do |member_file|
|
17
|
-
member_io = File.open(member_file)
|
18
|
-
s_io = StringIO.new
|
19
|
-
ContactCongressParser.parse(member_io, s_io)
|
20
|
-
expect { Conformity.send(:eval, s_io.string) }.to_not raise_error
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
after :context do
|
25
|
-
`rm -rf #{clone_to}`
|
15
|
+
parser = Parser.new(member_yaml)
|
16
|
+
expect(parser.bioguide).to match("A999999")
|
26
17
|
end
|
27
18
|
end
|
28
|
-
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
include ContactCongressParser
|
5
|
+
describe SuccessConditions do
|
6
|
+
it 'parses success conditions correctly' do
|
7
|
+
data = {"headers"=>{"status"=>200}, "body"=>{"contains"=>"Thank You"}}
|
8
|
+
sc = SuccessConditions.new(data)
|
9
|
+
expect(sc.to_s).to match(" has_content? \"Thank You\"\n" +
|
10
|
+
" has_status_code? 200\n")
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'handles missing data without raising errors' do
|
14
|
+
data1 = {"success"=>{}}
|
15
|
+
data2 = {"success"=>{"headers"=>{"status"=>200}}}
|
16
|
+
data3 = {"success"=>{"body"=>{"contains"=>"Thank You"}}}
|
17
|
+
end
|
18
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: contact_congress_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nikias Kalpaxis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-09-
|
11
|
+
date: 2014-09-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -54,12 +54,16 @@ files:
|
|
54
54
|
- lib/contact_congress_parser.rb
|
55
55
|
- lib/contact_congress_parser/action.rb
|
56
56
|
- lib/contact_congress_parser/field.rb
|
57
|
+
- lib/contact_congress_parser/form.rb
|
57
58
|
- lib/contact_congress_parser/parser.rb
|
59
|
+
- lib/contact_congress_parser/success_conditions.rb
|
58
60
|
- lib/contact_congress_parser/version.rb
|
59
61
|
- spec/action_spec.rb
|
60
62
|
- spec/field_spec.rb
|
63
|
+
- spec/form_spec.rb
|
61
64
|
- spec/parser_spec.rb
|
62
65
|
- spec/spec_helper.rb
|
66
|
+
- spec/success_conditions_spec.rb
|
63
67
|
homepage: https://github.com/nhjk/contact_congress_parser
|
64
68
|
licenses:
|
65
69
|
- MIT
|
@@ -87,5 +91,7 @@ summary: Parser for https://github.com/unitedstates/contact-congress member file
|
|
87
91
|
test_files:
|
88
92
|
- spec/action_spec.rb
|
89
93
|
- spec/field_spec.rb
|
94
|
+
- spec/form_spec.rb
|
90
95
|
- spec/parser_spec.rb
|
91
96
|
- spec/spec_helper.rb
|
97
|
+
- spec/success_conditions_spec.rb
|