class Peictt::Http::Http

Constants

CONTROLLER_ACTION_REGEXP
INVALID_ARG_ERROR
INVALID_ROUTE_ARG
INVALID_URL_FORMAT

Attributes

action[R]
controller[R]
placeholders[R]
regexp[R]
url[R]
verb[R]

Public Class Methods

new(*args) click to toggle source
# File lib/peictt/http/http.rb, line 12
def initialize(*args)
  @verb = "GET"
  @placeholders = []
  separate_args args
  process_url @url
  process_args if @args
end

Private Instance Methods

args_valid?(args) click to toggle source
# File lib/peictt/http/http.rb, line 27
def args_valid?(args)
  unless (args.is_a? String) || (args.is_a? Hash)
    raise ArgumentError.new(INVALID_ARG_ERROR)
  end
end
correct_format?(str) click to toggle source
# File lib/peictt/http/http.rb, line 98
def correct_format?(str)
  CONTROLLER_ACTION_REGEXP === str
end
get_placeholders(url_parts) click to toggle source
# File lib/peictt/http/http.rb, line 65
def get_placeholders(url_parts)
  url_parts.map do |part|
    if part[0] == ":"
      @placeholders << part[1..-1]
      "([A-Za-z0-9_]+)"
    else
      part
    end
  end
end
get_url_regexp(url) click to toggle source
# File lib/peictt/http/http.rb, line 57
def get_url_regexp(url)
  return "/" if url == "/"
  url_parts = url.split("/")
  url_parts.select! { |part| !part.empty? }
  regexp_parts = get_placeholders url_parts
  regexp_parts.join("/")
end
process_args() click to toggle source
# File lib/peictt/http/http.rb, line 33
def process_args
  args_valid? @args

  properties_from_string(@args) && return if @args.is_a? String
  properties_from_hash @args
end
process_url(url) click to toggle source
# File lib/peictt/http/http.rb, line 40
def process_url(url)
  if correct_format?(url) && @args.empty?
    @url = url.tr("#", "/")
    url =~ CONTROLLER_ACTION_REGEXP
    set_controller_and_action($1, $2)
  elsif !correct_format?(url) && @args.empty?
    raise ArgumentError.new(INVALID_ROUTE_ARG)
  end
  @regexp = Regexp.new("#{get_url_regexp(@url)}$")
end
properties_from_hash(hash) click to toggle source
# File lib/peictt/http/http.rb, line 82
def properties_from_hash(hash)
  raise ArgumentError.new(INVALID_URL_FORMAT) if correct_format? @url

  keys = hash.keys
  if (keys.include? :controller) && (keys.include? :action)
    set_controller_and_action hash[:controller], hash[:action]
    return
  end

  if (keys.include? :to) && (correct_format? hash[:to])
    hash[:to] =~ CONTROLLER_ACTION_REGEXP
    set_controller_and_action($1, $2)
    return
  end
end
properties_from_string(str) click to toggle source
# File lib/peictt/http/http.rb, line 76
def properties_from_string(str)
  raise ArgumentError.new(INVALID_URL_FORMAT) unless correct_format?(str)
  str =~ CONTROLLER_ACTION_REGEXP
  set_controller_and_action($1, $2)
end
separate_args(args) click to toggle source
# File lib/peictt/http/http.rb, line 22
def separate_args(args)
  @args = args[1] if args.size > 1
  @url = args[0]
end
set_controller_and_action(controller, action) click to toggle source
# File lib/peictt/http/http.rb, line 51
def set_controller_and_action(controller, action)
  controller_name = controller.to_camel_case
  @controller = Object.const_get("#{controller_name}Controller")
  @action = action
end