class Unrest

Constants

FILE_TYPES
VERSION

Attributes

args[RW]
url[RW]

Public Class Methods

config() click to toggle source
# File lib/unrest/configuration.rb, line 11
def self.config
  Configuration.instance.data
end
configure(options = nil, &block) click to toggle source
# File lib/unrest/configuration.rb, line 5
def self.configure(options = nil, &block)
  if !options.nil?
    Configuration.instance.configure(options)
  end
end
new(url, **args) click to toggle source
# File lib/unrest.rb, line 17
def initialize url, **args
  @url = url
  @args = args.deep_symbolize_keys
  @options = @args[:options]
  @arguments = @args[:arguments]

  call = caller_locations.first.absolute_path
  files = Dir[File.join(File.dirname(call), '**', '*') ]
          .reject{ |path| File.directory? path }
          .reject{ |file| !Unrest::FILE_TYPES.include?(File.extname(file))}

  files.each do |file|
    path = Pathname.new(file).relative_path_from(Pathname.new(call))
    path = path.to_s[0..-(File.extname(path).length + 1)].split('/')[1..-1]

    Unrest.class_eval do
      define_method path.join('_').downcase do |args|
        args.deep_symbolize_keys!

        http_request(file, path.join('/'),
           (@options.merge(args[:options]) rescue @options),
           (@arguments.merge(args[:arguments]) rescue @arguments)
        )
      end
    end
  end
end

Private Instance Methods

configuration() click to toggle source
# File lib/unrest.rb, line 137
def configuration
  Unrest.config
end
http_client(uri, options={}) click to toggle source
# File lib/unrest.rb, line 55
def http_client uri, options={}
  http_client = Net::HTTP.new uri.host, uri.port

  http_client.use_ssl = ssl?(uri)
  http_client.open_timeout = configuration.timeout
  http_client.read_timeout = configuration.timeout

  http_client.key = OpenSSL::PKey::RSA.new(options.fetch(:http_client_key, nil))
  http_client.cert = OpenSSL::X509::Certificate.new(options.fetch(:http_client_cert, nil))

  http_client.set_debug_output($stdout)

  http_client
end
http_request(file, path, options={}) click to toggle source
# File lib/unrest.rb, line 70
def http_request file, path, options={}, arguments={}
  uri = uri(path)
  http_request = http_request_method(options[:http_request_method]).new uri.path

  if configuration.authentication[:username] and configuration.authentication[:password]
    http_request.basic_auth(
      configuration.authentication[:username],
      configuration.authentication[:password]
    )
  end

  http_request_headers = options[:http_request_headers] || {}

  http_request_headers.merge(http_request_headers(File.extname(file))).each do |attribute, value|
    http_request[attribute] = value
  end

  http_request.body = http_request_body file, arguments

  http_client(uri, options).request http_request
rescue Timeout::Error
  raise Unrest::RequestTimeout
rescue Errno::EHOSTUNREACH, Errno::ETIMEDOUT, Errno::ENETUNREACH
  raise Unrest::NetworkError
end
http_request_body(file, arguments={}) click to toggle source
# File lib/unrest.rb, line 96
def http_request_body file, arguments={}
  template = Liquid::Template.parse(File.read(file))
  template.render(arguments.deep_stringify_keys)
end
http_request_format(extention) click to toggle source
# File lib/unrest.rb, line 101
def http_request_format extention
  case extention
  when '.xml'
    http_request_format = 'text/xml'
  when 'html'
    http_request_format = 'text/html'
  when 'json'
    http_request_format = 'text/json'
  else
    http_request_format = 'text/plain'
  end

  { 'Content-Type' => http_request_format }
end
http_request_headers(extention=nil) click to toggle source
# File lib/unrest.rb, line 133
def http_request_headers extention=nil
  configuration.headers.merge(http_request_format(extention))
end
http_request_method(method='get') click to toggle source
# File lib/unrest.rb, line 116
def http_request_method method='get'
  case method
  when 'get'
    Net::HTTP::Get
  when 'put'
    Net::HTTP::Put
  when 'post'
    Net::HTTP::Post
  when 'patch'
    Net::HTTP::Patch
  when 'delete'
    Net::HTTP::Delete
  else
    raise Unrest::ConfigurationError
  end
end
ssl?(uri) click to toggle source
# File lib/unrest.rb, line 51
def ssl? uri
  uri.scheme.casecmp('https') == 0
end
uri(path=nil) click to toggle source
# File lib/unrest.rb, line 47
def uri path=nil
  URI.parse([@url, path].compact.join('/'))
end