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

Returns a new instance of BodyBuilder



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.

Parameters:

  • name

    the name of the form item.

  • value

    the value of the form item.

Returns:

  • (Hash)

    the newly added form item.



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.

Parameters:

  • name

    the name of the form item.

  • path

    the local path of the file to be uploaded.

  • remote_name (optional) (defaults to: nil)

    the file name to transmit the file as.

Returns:

  • (Hash)

    the newly added form item.



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.

Parameters:

  • name

    the name of the form item.

  • value

    the contents of the file.

  • remote_name

    the file name to transmit the file as.

Returns:

  • (Hash)

    the newly added form item.



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.

Parameters:

  • name

    the name of the form item.

  • files (Hash)

    a hash of file names as keys and their content as values.

  • remote_name (String)

    the file name to transmit the file as.



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:

Returns:

  • (Nil)

    nothing, the body must be accessed by using a block when calling the method.



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