web_merge 1.0.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c514ab84c84abc4f88133dde2fce21ba8e2a1b81
4
+ data.tar.gz: 3bb31dea086300f008e05eac0f0d0981151eaeb7
5
+ SHA512:
6
+ metadata.gz: f7a5c30238783927772bee0ebb347ac51bb5d81060d6a62954d1fd6ab57e69d1e761ffa69e38f088343c2e0aa5c37bd1fa326785598159e361d20afd9d929501
7
+ data.tar.gz: 514fb4ab7d9c16cc3b3cd669d5f176014e643072ce42e1d4f4aeb67a8d752c2c160b87fd79767e876a247797a637269a923a682ed0e8d36420fbf04f8c0686be
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ vendor
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in web_merge.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Joe Heth
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,54 @@
1
+ # WebMerge Ruby
2
+
3
+ Manage and merge Documents using the WebMerge.me REST API
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'web_merge'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install web_merge
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ config = {
25
+ key: "N64EFIM0000000000000",
26
+ secret: "ABCDEFGH"
27
+ }
28
+
29
+ api = WebMerge::API.new(config)
30
+
31
+ # Get Document List
32
+ api.get_documents
33
+ # => [{"id" => 1, "key" => "abcd", "type" => "pdf", ...}, ...]
34
+
35
+ # Get Single Document
36
+ api.get_document(30036)
37
+ # => {"id" => 1, "key" => "abcd", "type" => "pdf", ...}
38
+
39
+ # Merge Fields with Document
40
+ fields = {
41
+ first_name: "John",
42
+ last_name: "Doe",
43
+ }
44
+
45
+ api.merge_document(doc_id=10000, key="4y7e1h", fields, {test: 1})
46
+ ```
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it ( https://github.com/[my-github-username]/web_merge/fork )
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,126 @@
1
+ module WebMerge
2
+ class API
3
+
4
+ def initialize(options={})
5
+ @api_secret = options[:secret] || ENV['WEB_MERGE_API_SECRET']
6
+ @api_key = options[:key] || ENV['WEB_MERGE_API_KEY']
7
+ @force_test_mode = options[:force_test_mode] || ENV['WEB_MERGE_FORCE_TEST_MODE']
8
+ @verbose = options[:verbose] || false
9
+ end
10
+
11
+ #
12
+ # DOCUMENTS
13
+ #
14
+ def create_document(form_data, &block)
15
+ post("#{WebMerge::Constants::DOCUMENTS}", form_data, &block)
16
+ end
17
+
18
+ def update_document(doc_id, form_data, &block)
19
+ put("#{WebMerge::Constants::DOCUMENTS}/#{doc_id}", form_data, &block)
20
+ end
21
+
22
+ def delete_document(doc_id, &block)
23
+ delete("#{WebMerge::Constants::DOCUMENTS}/#{doc_id}", &block)
24
+ end
25
+
26
+ def get_documents(&block)
27
+ get("#{WebMerge::Constants::DOCUMENTS}", &block)
28
+ end
29
+
30
+ def get_document(doc_id, &block)
31
+ get("#{WebMerge::Constants::DOCUMENTS}/#{doc_id}", &block)
32
+ end
33
+
34
+ def get_document_fields(doc_id, &block)
35
+ get("#{WebMerge::Constants::DOCUMENTS}/#{doc_id}/fields", &block)
36
+ end
37
+
38
+ # doc_id The Document ID
39
+ # example: 436346
40
+ # doc_key The Document Key
41
+ # example: firm3
42
+ # field_mappings The data to be merged in name/value pairs
43
+ # example: { name: "John Smith", occupation: "Plumber" }
44
+ #
45
+ # options[:test] Merges the document in "test mode"
46
+ # default: false.
47
+ # options[:download] Will return the merged document in response
48
+ # default: false
49
+ #
50
+ def merge_document(doc_id, doc_key, field_mappings, options = {}, &block)
51
+ post("#{WebMerge::Constants::MERGE_ENDPOINT}/#{doc_id}/#{doc_key}?download=#{download(options)}&test=#{test(options)}", field_mappings, &block)
52
+ end
53
+
54
+ #
55
+ # ROUTES
56
+ #
57
+ def get_routes(&block)
58
+ get("#{WebMerge::Constants::ROUTES}", &block)
59
+ end
60
+
61
+ def get_route(route_id, &block)
62
+ get("#{WebMerge::Constants::ROUTES}/#{route_id}", &block)
63
+ end
64
+
65
+ def get_route_fields(route_id, &block)
66
+ get("#{WebMerge::Constants::ROUTES}/#{route_id}/fields", &block)
67
+ end
68
+
69
+ def merge_route(route_id, route_key, field_mappings, options = {}, &block)
70
+ post("#{WebMerge::Constants::ROUTE_ENDPOINT}/#{route_id}/#{route_key}?download=#{download(options)}&test=#{test(options)}", field_mappings, &block)
71
+ end
72
+
73
+ # internal helpers
74
+ def post(url_string, form_data, &block)
75
+ request("post", url_string, form_data, &block)
76
+ end
77
+
78
+ def put(url_string, form_data, &block)
79
+ request("put", url_string, form_data, &block)
80
+ end
81
+
82
+ def delete(url_string, &block)
83
+ request("delete", url_string, &block)
84
+ end
85
+
86
+ def get(url_string, &block)
87
+ request("get", url_string, &block)
88
+ end
89
+
90
+ def request(verb, url_string, form_data = nil, &block)
91
+ parsed_response_body = nil
92
+ uri = URI.parse(url_string)
93
+ Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
94
+ action_klass = "Net::HTTP::#{verb.camelize}".constantize
95
+ request = action_klass.new(uri.request_uri)
96
+ request.basic_auth(@api_key, @api_secret)
97
+ request.set_form_data(form_data) if form_data.present?
98
+ http.request(request) do |response|
99
+ if block_given?
100
+ return block.call(response)
101
+ else
102
+ begin
103
+ parsed_response_body = JSON.parse(response.body)
104
+ rescue
105
+ parsed_response_body = "Unable to parse response body as JSON perhaps you'd like to pass a block to process the response?"
106
+ parsed_response_body << "#{response.body}"
107
+ end
108
+ end
109
+ end
110
+ end
111
+ parsed_response_body
112
+ end
113
+
114
+ def download(options)
115
+ options[:download] && true?(options[:download]) ? 1 : 0
116
+ end
117
+
118
+ def test(options)
119
+ true?(@force_test_mode) || (options[:test] && true?(options[:test])) ? 1 : 0
120
+ end
121
+
122
+ def true?(value)
123
+ value.to_s.match(/(true|t|yes|y|1)$/i).present?
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,33 @@
1
+ module WebMerge
2
+ class Constants
3
+ WEB_MERGE = "https://www.webmerge.me"
4
+
5
+ API_ENDPOINT = "#{WEB_MERGE}/api"
6
+
7
+ MERGE_ENDPOINT = "#{WEB_MERGE}/merge"
8
+ ROUTE_ENDPOINT = "#{WEB_MERGE}/route"
9
+
10
+ DOCUMENTS = "#{API_ENDPOINT}/documents"
11
+
12
+ ROUTES = "#{API_ENDPOINT}/routes"
13
+
14
+ API_SECRET = ENV['WEB_MERGE_API_SECRET']
15
+ API_KEY = ENV['WEB_MERGE_API_KEY']
16
+
17
+ FORCE_TEST_MODE = ENV['WEB_MERGE_FORCE_TEST_MODE']
18
+
19
+ PDF = 'pdf'
20
+ HTML = 'html'
21
+ DOCX = 'docx'
22
+ EMAIL = 'email'
23
+
24
+ SECURITY_LEVEL_LOW = 'low'
25
+ SECURITY_LEVEL_MEDIUM = 'medium'
26
+ SECURITY_LEVEL_HIGH = 'high'
27
+
28
+ SUPPORTED_TYPES = [HTML, PDF, DOCX]
29
+ SUPPORTED_OUTPUTS = [PDF, DOCX, EMAIL]
30
+
31
+ SECURITY_LEVELS = [SECURITY_LEVEL_LOW, SECURITY_LEVEL_MEDIUM, SECURITY_LEVEL_HIGH]
32
+ end
33
+ end
@@ -0,0 +1,139 @@
1
+ module WebMerge
2
+ class Document
3
+ include ::ActiveModel::Validations
4
+
5
+ attr_accessor :name, :type, :output, :output_name, :size_width, :size_height, :notification, :file_path
6
+ attr_reader :id, :key, :size, :active, :url
7
+
8
+ validates_presence_of :name, :type, :output
9
+ validates_presence_of :file_path
10
+ validates :type, inclusion: { in: WebMerge::Constants::SUPPORTED_TYPES }
11
+ validates :output, inclusion: { in: WebMerge::Constants::SUPPORTED_OUTPUTS }
12
+
13
+ def initialize(name: required(:name), type: required(:type), output: WebMerge::Constants::PDF, file_path: nil, options: {})
14
+ @name = name
15
+ @type = type
16
+ @output = output
17
+ @file_path = file_path
18
+ @output_name = options[:output_name]
19
+ @size_width = options[:size_width]
20
+ @size_height = options[:size_height]
21
+ end
22
+
23
+ def self.find(doc_id)
24
+ instance = empty_instance
25
+ instance.send(:id=, doc_id)
26
+ instance.reload
27
+ end
28
+
29
+ def self.each
30
+ all.each do |doc|
31
+ yield doc if block_given?
32
+ end
33
+ end
34
+
35
+ def self.all
36
+ WebMerge::API.get_documents.map do |doc_hash|
37
+ instance = empty_instance
38
+ instance.send(:update_instance, doc_hash)
39
+ instance
40
+ end
41
+ end
42
+
43
+ def html?
44
+ type == WebMerge::Constants::HTML
45
+ end
46
+
47
+ def new_document?
48
+ id.blank?
49
+ end
50
+
51
+ def save
52
+ return false unless valid?
53
+ response = if new_document?
54
+ WebMerge::API.create_document(as_form_data)
55
+ else
56
+ WebMerge::API.update_document(id, as_form_data)
57
+ end
58
+ update_instance(response.symbolize_keys)
59
+ true
60
+ end
61
+
62
+ def save!
63
+ raise "Document contains errors: #{errors.full_messages.join(", ")}" unless save
64
+ end
65
+
66
+ def reload
67
+ raise "Cannot reload a new document, perhaps you'd like to call `save' first?" if new_document?
68
+ response = WebMerge::API.get_document(id)
69
+ update_instance(response)
70
+ self
71
+ end
72
+
73
+ def delete
74
+ delete = false
75
+ WebMerge::API.delete_document(id) { |response| delete = JSON(response.body)["success"] } unless new_document?
76
+ delete
77
+ end
78
+
79
+ def fields
80
+ raise "Cannot fetch fields for an unpersisted document, perhaps you'd like to call `save' first?" if new_document?
81
+ @fields ||= WebMerge::API.get_document_fields(id)
82
+ end
83
+
84
+ def field_names
85
+ fields.map { |field| field["name"] }
86
+ end
87
+
88
+ def merge(field_mappings, options = {}, &block)
89
+ raise "Cannot merge an unpersisted document, perhaps you'd like to call `save' first?" if new_document?
90
+ WebMerge::API.merge_document(id, key, field_mappings, options, &block)
91
+ end
92
+
93
+ def as_form_data
94
+ request_params = {
95
+ name: name,
96
+ type: type,
97
+ output: output
98
+ }
99
+ [:output_name, :size_width, :size_height].each do |key|
100
+ value = send(key)
101
+ request_params.merge!(key => value) if value.present?
102
+ end
103
+ merge_file_contents!(request_params)
104
+ merge_notification!(request_params) if notification.present?
105
+ request_params
106
+ end
107
+
108
+ private
109
+ attr_writer :id, :key, :size, :active, :url
110
+
111
+ def self.empty_instance
112
+ new(name: "", type: "", output: "")
113
+ end
114
+
115
+ def update_instance(response)
116
+ response.each_pair do |key, value|
117
+ send("#{key}=".to_sym, value) if respond_to?("#{key}=".to_sym, true)
118
+ end
119
+ end
120
+
121
+ def merge_file_contents!(request_params)
122
+ if html?
123
+ html_string = IO.binread(file_path)
124
+ request_params.merge!(html: html_string)
125
+ else
126
+ encoded = Base64.encode64(IO.binread(file_path))
127
+ request_params.merge!(file_contents: encoded)
128
+ end
129
+ request_params
130
+ end
131
+
132
+ def merge_notification!(request_params)
133
+ notification.as_form_data.each_pair do |key, value|
134
+ request_params.merge!("notification[#{key}]" => value)
135
+ end
136
+ end
137
+
138
+ end
139
+ end
@@ -0,0 +1,38 @@
1
+ module WebMerge
2
+ class Notification
3
+ include ::ActiveModel::Validations
4
+
5
+ attr_accessor :to, :from, :subject, :html, :security, :password
6
+
7
+ validates_presence_of :to, :from
8
+ validates_format_of :to, :from, :with => /^(|(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6})$/i
9
+ validates_presence_of :password, if: :requires_password?
10
+ validates :security, inclusion: { in: WebMerge::Constants::SECURITY_LEVELS }
11
+
12
+ def initialize(to: required(:to), from: required(:from), subject: "WebMerge Notification", security: WebMerge::Constants::SECURITY_LEVEL_LOW, html: nil, password: nil)
13
+ @to = to
14
+ @from = from
15
+ @subject = subject
16
+ @security = security
17
+ @html = html
18
+ @password = password
19
+ end
20
+
21
+ def requires_password?
22
+ security == WebMerge::Constants::SECURITY_LEVEL_MEDIUM || security == WebMerge::Constants::SECURITY_LEVEL_HIGH
23
+ end
24
+
25
+ def as_form_data
26
+ request_params = {
27
+ to: to,
28
+ from: from,
29
+ subject: subject,
30
+ html: html,
31
+ security: security
32
+ }
33
+ request_params.merge!(password: password) if requires_password?
34
+ request_params
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module WebMerge
2
+ VERSION = "1.0.0"
3
+ end
data/lib/web_merge.rb ADDED
@@ -0,0 +1,13 @@
1
+ require "web_merge/version"
2
+
3
+ require 'json'
4
+ require 'active_model'
5
+
6
+ require 'web_merge/api'
7
+ require 'web_merge/constants'
8
+ require 'web_merge/document'
9
+ require 'web_merge/notification'
10
+
11
+ module WebMerge
12
+
13
+ end
data/web_merge.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'web_merge/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "web_merge"
8
+ spec.version = WebMerge::VERSION
9
+ spec.authors = ["Jeremy Clarke"]
10
+ spec.email = ["JeremyClarke@webmerge.me"]
11
+ spec.summary = %q{WebMerge REST API Wrapper}
12
+ spec.description = %q{Manage and merge Documents using the WebMerge.me REST API}
13
+ spec.homepage = "http://www.webmerge.me"
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.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "activemodel", ">= 3.0"
24
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: web_merge
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeremy Clarke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-08 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activemodel
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Manage and merge Documents using the WebMerge.me REST API
56
+ email:
57
+ - JeremyClarke@webmerge.me
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/web_merge.rb
68
+ - lib/web_merge/api.rb
69
+ - lib/web_merge/constants.rb
70
+ - lib/web_merge/document.rb
71
+ - lib/web_merge/notification.rb
72
+ - lib/web_merge/version.rb
73
+ - web_merge.gemspec
74
+ homepage: http://www.webmerge.me
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.4.5
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: WebMerge REST API Wrapper
98
+ test_files: []