Module: DataBindings::Writers

Includes:
GemRequirement
Defined in:
lib/data_bindings/converters.rb

Overview

This defines the default writers used.

Instance Method Summary collapse

Methods included from GemRequirement

gentle_require_gem, included

Instance Method Details

#file(data, path) ⇒ Object

Takes data and a file path and writes it’s contents to it.

Parameters:

  • data (String)

    The data to be written

  • path (String)

    The IO object to write to



64
65
66
# File 'lib/data_bindings/converters.rb', line 64

def file(data, path)
  File.open(path, 'w') { |f| f << data }
end

#http(data, url, opts = {}) ⇒ Object

Takes a URL and posts the contents of your data to it. Uses HTTPParty underlyingly.

Parameters:

  • data (String)

    The data to send to

  • url (String)

    The URL to send your request to

  • opts (Hash) (defaults to: {})

    The options to pass in to HTTPParty

See Also:



73
74
75
76
77
78
79
80
# File 'lib/data_bindings/converters.rb', line 73

def http(data, url, opts = {})
  method = opts[:method] || :post
  opts[:data] = data
  response = HTTParty.send(method, url, opts)
  unless (200..299).include?(response.code)
    raise HttpError.new("Bad response: #{response.code} #{response.body}", response)
  end
end

#io(data, io) ⇒ Object

Takes data and an IO object and writes it’s contents to it.

Parameters:

  • data (String)

    The data to be written

  • i (IO)

    The IO object to write to



57
58
59
# File 'lib/data_bindings/converters.rb', line 57

def io(data, io)
  io.write(obj)
end