class Upfluence::HTTP::Endpoint::APIEndpoint

Constants

VALIDATION_ERROR_KLASS

Public Instance Methods

access_token() click to toggle source
# File lib/upfluence/http/endpoint/api_endpoint.rb, line 36
def access_token
  token = params[:access_token]

  unless token
    pattern = /^Bearer /
    header  = request.env['HTTP_AUTHORIZATION']
    token   = header.gsub(pattern, '') if header&.match(pattern)
  end

  token
end
json_params() click to toggle source
# File lib/upfluence/http/endpoint/api_endpoint.rb, line 76
def json_params
  ActiveSupport::HashWithIndifferentAccess.new(
    JSON.parse(request_body)
  )
end
ok() click to toggle source
# File lib/upfluence/http/endpoint/api_endpoint.rb, line 32
def ok
  [200, { status: 'OK' }.to_json]
end
request_body() click to toggle source
# File lib/upfluence/http/endpoint/api_endpoint.rb, line 83
def request_body
  @request_body ||= request.body.read
end
respond_with(resource, *args) click to toggle source
# File lib/upfluence/http/endpoint/api_endpoint.rb, line 48
def respond_with(resource, *args)
  if resource.respond_to?(:errors) && resource.errors.any?
    status = 422
    result = VALIDATION_ERROR_KLASS.from_model(
      resource
    ).to_json
  else
    status = 200
    opts = args.first || {}

    result = if resource.is_a? Enumerable
               USerializer::ArraySerializer.new(
                 resource, *args
               ).to_json
             elsif opts[:serializer]
               opts[:serializer].new(resource, *args).to_json
             elsif resource.respond_to?(:serialize)
               resource.serialize(*args).to_json
             else
               USerializer.serializer_for(resource).new(
                 resource, *args
               ).to_json
             end
  end

  halt [status, result]
end