Module: Cyclid::API::Plugins::ApiExtension::Helpers

Defined in:
app/cyclid/plugins/api.rb

Overview

Standard helpers for API extensions. Mostly the point is to try to hide as much of the underlying Sinatra implementation as possible and simplify (& therefore control) the plugins ability to interact with Sinatra.

Instance Method Summary collapse

Instance Method Details

#authorize(method) ⇒ Object

Wrapper around the standard Warden authn/authz

ApiExtension methods can choose to be authenticated or unauthenticated; for example a callback hook from an external SCM could accept unauthenticated POST’s that trigger some action.

The callback method implementations can choose to call authorize() if the endpoint would be authenticated, or not to call it in which case the method would be unauthenticated.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/cyclid/plugins/api.rb', line 121

def authorize(method)
  operation = if method.casecmp 'get'
                Operations::READ
              elsif method.casecmp 'put'
                Operations::WRITE
              elsif method.casecmp 'post' or
                    method.casecmp 'delete'
                Operations::ADMIN
              else
                raise "invalid method '#{method}'"
              end

  authorized_for!(params[:name], operation)
end

#http_headers(environment) ⇒ Object

Extract headers from the raw request & pretty them up



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/cyclid/plugins/api.rb', line 142

def http_headers(environment)
  http_headers = headers
  environment.each do |env|
    key, value = env
    match = key.match(/\AHTTP_(.*)\Z/)
    next unless match

    header = match[1].split('_').map(&:capitalize).join('-')
    http_headers[header] = value
  end

  return http_headers
end

#organization_nameObject

Return the current organization name



157
158
159
# File 'app/cyclid/plugins/api.rb', line 157

def organization_name
  params[:name]
end

#retrieve_organization(name = nil) ⇒ Object

Find & return the Organization model



162
163
164
165
166
167
168
# File 'app/cyclid/plugins/api.rb', line 162

def retrieve_organization(name = nil)
  name ||= organization_name
  org = Organization.find_by(name: name)
  halt_with_json_response(404, INVALID_ORG, 'organization does not exist') \
    if org.nil?
  return org
end

#return_failure(code, message) ⇒ Object

Return a standard Cyclid style failure.



137
138
139
# File 'app/cyclid/plugins/api.rb', line 137

def return_failure(code, message)
  halt_with_json_response(code, Errors::HTTPErrors::PLUGIN_ERROR, message)
end