class Facile::Request

Attributes

body[RW]
headers[RW]
method[RW]
options[RW]
params[RW]
url[RW]

Public Class Methods

method_missing(method, *args, &block) click to toggle source
# File lib/facile/request.rb, line 4
def self.method_missing(method, *args, &block)
  supported_methods = [:get, :post, :delete, :options, :patch, :put]

  raise NoMethodError, "undefined method '#{method}'" if !supported_methods.include?(method)

  args = args.count == 0 ? {} : args
  args.merge!({:method => method || :get })

  new(args, &block).go
end
new(options = {}, &block) click to toggle source
# File lib/facile/request.rb, line 15
def initialize(options = {}, &block)
  options.merge!({ :method => :get }) unless options.has_key?(:method)

  options.each do |k, v|
    self.send("#{k}=", v) if respond_to?("#{k}=")
  end

  instance_eval(&block) if block_given?
end

Public Instance Methods

go() click to toggle source
# File lib/facile/request.rb, line 25
def go
  begin
    conn = Faraday.new(options || {})

    res = conn.send(method) do |req|
      req.url url

      headers.map { |k,v| req.headers[k] = v } if !headers.nil?
      params.map { |k,v| req.params[k] = v } if !params.nil?
      req.body = body if !body.nil?
    end
  rescue => e
    raise e
  end

  parse_response(res)
end
parse_response(res) click to toggle source
# File lib/facile/request.rb, line 43
def parse_response(res)
  ::Facile::Response.new({
    :request => self,
    :response => res,
    :status => res.status,
    :headers => res.headers,
    :raw_body => res.body,
  })
end