Module: DataBindings::Readers

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

Overview

This defines the default readers used.

Instance Method Summary collapse

Methods included from GemRequirement

gentle_require_gem, included

Instance Method Details

#file(path) ⇒ Object

Takes a file path and returns it’s contents.

Parameters:

  • path (String)

    The file path

Returns:

  • The contents of the file



29
30
31
# File 'lib/data_bindings/converters.rb', line 29

def file(path)
  File.read(path)
end

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

Takes a URL and returns it’s contents. Uses HTTPParty underlyingly.

Parameters:

  • url (String)

    The URL to request from

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

    The options to pass in to HTTPParty

Returns:

  • The body of the response from the URL as a String

See Also:



38
39
40
41
42
43
44
45
46
# File 'lib/data_bindings/converters.rb', line 38

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

#io(i) ⇒ Object

Takes an IO object and reads it’s contents.

Parameters:

  • i (IO)

    The IO object to read from

Returns:

  • The contents of the IO object



21
22
23
24
# File 'lib/data_bindings/converters.rb', line 21

def io(i)
  i.rewind
  i.read
end