class GrapeHalIntegration

Public Class Methods

build_current_action(name) click to toggle source
# File lib/grape_hal_integration.rb, line 28
def self.build_current_action(name)
  "#{self.name}##{name.to_s}"
end
endpoint(name) click to toggle source
# File lib/grape_hal_integration.rb, line 84
def self.endpoint(name)
  @@endpoints[build_current_action(name.to_s)]
end
endpoint_url(name) click to toggle source
# File lib/grape_hal_integration.rb, line 88
def self.endpoint_url(name)
  endpoint(name)[:self]
end
implement(name, url, links = [], &block) click to toggle source
# File lib/grape_hal_integration.rb, line 32
def self.implement(name, url, links = [], &block)
  name = name.to_s
  http_verb, url = url.split ' '

  @@endpoints[build_current_action(name)] = {
      name: name,
      links: links,
      block: block,
      self: {
          href: url,
          method: http_verb.upcase
      }
  }
  @@endpoints[build_current_action(name)][:self][:templated] = true if url.include? '{'

  class_name = self.name.constantize
  get_params_name = url.scan(/\{([^}]*)\}/).flatten
  wrapper_block = lambda do
    get_params_values = get_params_name.map do |get_param_name|
      result = get_param_name.match(/(.+)_id/)
      value = params[get_param_name.intern]
      unless result.blank?
        begin
          klass = result[1].singularize.classify.constantize
          value = klass.find(value) if klass.class == Class
        rescue NameError
        end
      end
      value
    end

    hal = {_links: HalIntegrator.links(class_name, name)}
    response = self.instance_exec *get_params_values, &block
    hal.merge(response)
  end

  resource url.gsub('{', ':').gsub('}', '') do
    case http_verb.upcase
      when 'POST'
        post &wrapper_block
      when 'PUT'
        put &wrapper_block
      when 'DELETE'
        delete &wrapper_block
      when 'GET'
        get &wrapper_block
      else
        raise "Undefined http verb '#{http_verb}'"
    end
  end
end
method_missing(method_name, *arguments, &block) click to toggle source
# File lib/grape_hal_integration.rb, line 92
def self.method_missing(method_name, *arguments, &block)
  raise "Unknown name '#{build_current_action(method_name)}'" unless @@endpoints.has_key? build_current_action(method_name)
  context = arguments.shift
  context.instance_exec *arguments, &endpoint(method_name)[:block]
end