Class: Wpxf::Utility::BodyBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/wpxf/utility/body_builder.rb

Overview

A super strong body builder capable of building formatted form bodies for use with the Net::HttpClient mixin.

Instance Method Summary collapse

Constructor Details

#initializeBodyBuilder



10
11
12
13
# File 'lib/wpxf/utility/body_builder.rb', line 10

def initialize
  @fields = {}
  @temp_dir = File.join(Dir.tmpdir, "wpxf_#{object_id}")
end

Instance Method Details

#add_field(name, value) ⇒ Hash

Add a key-value pair to the field list.



19
20
21
# File 'lib/wpxf/utility/body_builder.rb', line 19

def add_field(name, value)
  @fields[name] = { type: :normal, value: value }
end

#add_file(name, path, remote_name = nil) ⇒ Hash

Add a file to the field list.



28
29
30
# File 'lib/wpxf/utility/body_builder.rb', line 28

def add_file(name, path, remote_name = nil)
  @fields[name] = { type: :file, path: path, remote_name: remote_name }
end

#add_file_from_string(name, value, remote_name) ⇒ Hash

Add a file to the field list that will upload a specific string as its file contents, rather than reading from disk.



38
39
40
41
42
43
44
# File 'lib/wpxf/utility/body_builder.rb', line 38

def add_file_from_string(name, value, remote_name)
  @fields[name] = {
    type: :mem_file,
    value: value,
    remote_name: remote_name
  }
end

#add_zip_file(name, files, remote_name) ⇒ Object

Generate a ZIP file and add it to the field list.



51
52
53
54
55
56
57
# File 'lib/wpxf/utility/body_builder.rb', line 51

def add_zip_file(name, files, remote_name)
  @fields[name] = {
    type: :mem_zip,
    value: files,
    remote_name: remote_name
  }
end

#create {|body| ... } ⇒ Nil

Create the body string and pass it to the specified block.

Yield Parameters:



63
64
65
66
67
68
69
# File 'lib/wpxf/utility/body_builder.rb', line 63

def create
  body = _prepare_fields
  yield(body)
ensure
  _cleanup_temp_files(body)
  nil
end