Created work package representer & current_user helper method for API

pull/1435/head
Marek Takac 11 years ago
parent 5aad3c087a
commit f909e89687
  1. 1
      Gemfile
  2. 8
      Gemfile.lock
  3. 13
      app/api/api.rb
  4. 53
      app/api/work_packages/work_package_representer.rb
  5. 11
      app/api/work_packages/work_packages.rb

@ -190,6 +190,7 @@ end
# API gems
gem 'grape', '~> 0.7.0'
gem 'roar', '~> 0.12.6'
# Use the commented pure ruby gems, if you have not the needed prerequisites on
# board to compile the native ones. Note, that their use is discouraged, since

@ -336,7 +336,13 @@ GEM
json (~> 1.4)
redcarpet (3.0.0)
ref (1.0.5)
representable (1.8.1)
multi_json
nokogiri
uber
request_store (1.0.5)
roar (0.12.6)
representable (>= 1.6.0)
rspec (2.14.1)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
@ -410,6 +416,7 @@ GEM
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.38)
uber (0.0.4)
uglifier (2.1.1)
execjs (>= 0.3.0)
multi_json (~> 1.0, >= 1.0.2)
@ -492,6 +499,7 @@ DEPENDENCIES
rb-readline
rdoc (>= 2.4.2)
request_store
roar (~> 0.12.6)
rspec (~> 2.14)
rspec-example_disabler!
rspec-rails (~> 2.14)

@ -1,7 +1,20 @@
class API < Grape::API
use Rack::Session::Cookie
content_type 'hal+json', 'application/hal+json'
format 'hal+json'
helpers do
def current_user
return nil if env['rack.session'][:user_id].nil?
@current_user ||= User.find(env['rack.session'][:user_id])
end
def current_user=(user)
env['rack.session'][:user_id] = user.id unless user
@current_user = user
end
end
get do
"Entry point"
end

@ -0,0 +1,53 @@
require 'roar/decorator'
require 'roar/representer/json'
require 'roar/representer/json/hal'
module WorkPackages
class WorkPackageRepresenter < Roar::Decorator
include Roar::Representer::JSON
include Roar::Representer::Feature::Hypermedia
include Roar::Representer::JSON::HAL
property :id
property :subject
property :description
property :type, getter: lambda { |arg| type.try(:name) }
property :due_date
property :category, getter: lambda { |arg| category.try(:name) }
property :status, getter: lambda { |arg| status.try(:name) }
property :priority, getter: lambda { |arg| priority.try(:name) }
property :fixed_version, getter: lambda { |arg| fixed_version.try(:name) }
property :lock_version
property :done_ratio
property :estimated_hours
property :start_date
property :created_at
property :updated_at
link :self do
"/work_packages/#{represented.id}"
end
property :project, class: Project, embedded: true do
property :id
end
property :author, class: User, embedded: true do
property :mail
end
property :assigned_to, as: :assignee, class: User, embedded: true do
property :mail
end
property :responsible, class: User, embedded: true do
property :mail
end
# collection :children, class: WorkPackage, embedded: true do
# property :subject
# end
end
end

@ -3,18 +3,19 @@ module WorkPackages
resources :work_packages do
get do
WorkPackage.all
work_packages = WorkPackage.all
end
get :id do
WorkPackage.find(params[:id])
get ':id' do
work_package = current_user.work_packages.find(params[:id])
WorkPackageRepresenter.new(work_package).to_json
end
patch :id do
patch ':id' do
"work package update"
end
delete :id do
delete :':id' do
"work package delete"
end

Loading…
Cancel
Save