Class: WebMerge::API

Inherits:
Object
  • Object
show all
Defined in:
lib/web_merge/api.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ API

Returns a new instance of API.



4
5
6
7
8
9
# File 'lib/web_merge/api.rb', line 4

def initialize(options={})
  @api_secret = options[:secret] || ENV['WEB_MERGE_API_SECRET']
  @api_key = options[:key] || ENV['WEB_MERGE_API_KEY']
  @force_test_mode = options[:force_test_mode] || ENV['WEB_MERGE_FORCE_TEST_MODE']
  @verbose = options[:verbose] || false
end

Instance Method Details

#create_document(form_data, &block) ⇒ Object

DOCUMENTS



14
15
16
# File 'lib/web_merge/api.rb', line 14

def create_document(form_data, &block)
  post("#{WebMerge::Constants::DOCUMENTS}", form_data, &block)
end

#delete(url_string, &block) ⇒ Object



92
93
94
# File 'lib/web_merge/api.rb', line 92

def delete(url_string, &block)
  request("delete", url_string, &block)
end

#delete_document(doc_id, &block) ⇒ Object



22
23
24
# File 'lib/web_merge/api.rb', line 22

def delete_document(doc_id, &block)
  delete("#{WebMerge::Constants::DOCUMENTS}/#{doc_id}", &block)
end

#download(options) ⇒ Object



124
125
126
# File 'lib/web_merge/api.rb', line 124

def download(options)
  options[:download] && true?(options[:download]) ? 1 : 0
end

#get(url_string, &block) ⇒ Object



96
97
98
# File 'lib/web_merge/api.rb', line 96

def get(url_string, &block)
  request("get", url_string, &block)
end

#get_document(doc_id, &block) ⇒ Object



30
31
32
# File 'lib/web_merge/api.rb', line 30

def get_document(doc_id, &block)
  get("#{WebMerge::Constants::DOCUMENTS}/#{doc_id}", &block)
end

#get_document_fields(doc_id, &block) ⇒ Object



34
35
36
# File 'lib/web_merge/api.rb', line 34

def get_document_fields(doc_id, &block)
  get("#{WebMerge::Constants::DOCUMENTS}/#{doc_id}/fields", &block)
end

#get_document_file(doc_id, &block) ⇒ Object



38
39
40
# File 'lib/web_merge/api.rb', line 38

def get_document_file(doc_id, &block)
  get("#{WebMerge::Constants::DOCUMENTS}/#{doc_id}/file", &block)
end

#get_documents(&block) ⇒ Object



26
27
28
# File 'lib/web_merge/api.rb', line 26

def get_documents(&block)
  get("#{WebMerge::Constants::DOCUMENTS}", &block)
end

#get_route(route_id, &block) ⇒ Object



71
72
73
# File 'lib/web_merge/api.rb', line 71

def get_route(route_id, &block)
  get("#{WebMerge::Constants::ROUTES}/#{route_id}", &block)
end

#get_route_fields(route_id, &block) ⇒ Object



75
76
77
# File 'lib/web_merge/api.rb', line 75

def get_route_fields(route_id, &block)
  get("#{WebMerge::Constants::ROUTES}/#{route_id}/fields", &block)
end

#get_routes(&block) ⇒ Object

ROUTES



67
68
69
# File 'lib/web_merge/api.rb', line 67

def get_routes(&block)
  get("#{WebMerge::Constants::ROUTES}", &block)
end

#merge_document(doc_id, doc_key, field_mappings, options = {}, &block) ⇒ Object

doc_id The Document ID

example: 436346

doc_key The Document Key

example: firm3

field_mappings The data to be merged in name/value pairs

example: { name: "John Smith", occupation: "Plumber" }

options Merges the document in “test mode”

default: false.

options Will return the merged document in response

default: false

options Will return the merged document flattened (with no editing capabilities)

default: 0


56
57
58
59
60
61
62
# File 'lib/web_merge/api.rb', line 56

def merge_document(doc_id, doc_key, field_mappings, options = {}, &block)
  query = ""
  if options.present?
    query = "?" + URI.encode(options.map{|k,v| "#{k}=#{v}"}.join("&"))
  end
  post("#{WebMerge::Constants::MERGE_ENDPOINT}/#{doc_id}/#{doc_key}#{query}", field_mappings, &block)
end

#merge_route(route_id, route_key, field_mappings, options = {}, &block) ⇒ Object



79
80
81
# File 'lib/web_merge/api.rb', line 79

def merge_route(route_id, route_key, field_mappings, options = {}, &block)
  post("#{WebMerge::Constants::ROUTE_ENDPOINT}/#{route_id}/#{route_key}?download=#{download(options)}&test=#{test(options)}", field_mappings, &block)
end

#post(url_string, form_data, &block) ⇒ Object

internal helpers



84
85
86
# File 'lib/web_merge/api.rb', line 84

def post(url_string, form_data, &block)
  request("post", url_string, form_data, &block)
end

#put(url_string, form_data, &block) ⇒ Object



88
89
90
# File 'lib/web_merge/api.rb', line 88

def put(url_string, form_data, &block)
  request("put", url_string, form_data, &block)
end

#request(verb, url_string, form_data = nil, &block) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/web_merge/api.rb', line 100

def request(verb, url_string, form_data = nil, &block)
  parsed_response_body = nil
  uri = URI.parse(url_string)
  Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
    action_klass = "Net::HTTP::#{verb.camelize}".constantize
    request = action_klass.new(uri.request_uri, 'Content-Type' => 'application/json')
    request.basic_auth(@api_key, @api_secret)
    request.body = form_data.to_json if form_data.present?
    http.request(request) do |response|
      if block_given?
        return block.call(response)
      else
        begin
          parsed_response_body = JSON.parse(response.body)
        rescue
          parsed_response_body = "Unable to parse response body as JSON perhaps you'd like to pass a block to process the response?"
          parsed_response_body << "#{response.body}"
        end
      end
    end
  end
  parsed_response_body
end

#test(options) ⇒ Object



128
129
130
# File 'lib/web_merge/api.rb', line 128

def test(options)
  true?(@force_test_mode) || (options[:test] && true?(options[:test])) ? 1 : 0
end

#true?(value) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/web_merge/api.rb', line 132

def true?(value)
  value.to_s.match(/(true|t|yes|y|1)$/i).present?
end

#update_document(doc_id, form_data, &block) ⇒ Object



18
19
20
# File 'lib/web_merge/api.rb', line 18

def update_document(doc_id, form_data, &block)
  put("#{WebMerge::Constants::DOCUMENTS}/#{doc_id}", form_data, &block)
end