Module: Cuba::Tools::Api::Helpers

Defined in:
lib/cuba/tools/api.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.setup(app) ⇒ Object



32
33
34
# File 'lib/cuba/tools/api.rb', line 32

def self.setup app
  require 'yaml'
end

Instance Method Details

#add_attributes_for(params) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/cuba/tools/api.rb', line 72

def add_attributes_for params
  params.dup.each do |key, value|
    if !key[/_attributes$/] && value.is_a?(Hash)
      params["#{key}_attributes"] = add_attributes_for value
      params.delete key
    end
  end

  params
end

#api_params(key, klass) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cuba/tools/api.rb', line 36

def api_params key, klass
  params = add_attributes_for req.params[key.to_s]

  @api_params ||= YAML.load_file "#{Api.config.path}/#{key}.yml"

  form = klass.restrict!(current_user).new

  if form.validates params, as: current_user
    lambda { captures << form; captures << params }
  else
    raise ValidationError.new(form.errors), 'Api Validation Failed'
  end
end

#api_signatureObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cuba/tools/api.rb', line 50

def api_signature
  api_request = Signature::Request.new(
    req.request_method,
    req.path,
    req.params,
    req.env
  )

  user = false

  api_request.authenticate do |key|
    user = user_class.where(api_key: key).first
    Signature::Token.new key, user.try(:api_secret)
  end

  if defined? Rails
    @request = req
  end

   user
end