Skip to content

Commit

Permalink
Allow for specifying of additional attributes for pictures
Browse files Browse the repository at this point in the history
  • Loading branch information
Jillian Tullo committed Feb 25, 2018
1 parent 9fcf7a1 commit f7f4580
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
13 changes: 12 additions & 1 deletion app/controllers/api/mixins/pictures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@ def fetch_picture(resource)
format_picture_response(resource.picture)
end

def normalize_hash(reftype, resource, normalize_options = {})
if resource.kind_of?(Picture)
format_picture_response(resource)
else
super(reftype, resource, normalize_options)
end
end

def format_picture_response(picture)
return unless picture
picture.attributes.except('content').merge('image_href' => picture.image_href)
picture_hash = picture.attributes.except('content').merge('image_href' => picture.image_href,
'href' => picture.href_slug,
'extension' => picture.extension)
normalize_hash(:picture, picture_hash)
end
end
end
Expand Down
8 changes: 0 additions & 8 deletions app/controllers/api/pictures_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,11 @@ module Api
class PicturesController < BaseController
include Api::Mixins::Pictures

before_action :set_additional_attributes, :only => [:index, :show]

def create_resource(_type, _id, data)
picture = Picture.create_from_base64(data)
format_picture_response(picture)
rescue => err
raise BadRequestError, "Failed to create Picture - #{err}"
end

private

def set_additional_attributes
@additional_attributes = %w(image_href extension)
end
end
end
33 changes: 33 additions & 0 deletions spec/requests/pictures_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,29 @@ def expect_result_to_include_picture_href(source_id)
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
end

it 'allows specifying of additional attributes' do
api_basic_authorize

expected = {
'resources' => [
a_hash_including('href_slug' => @picture.href_slug,
'region_number' => @picture.region_number)
]
}
get(api_pictures_url, :params => { :expand => 'resources', :attributes => 'href_slug,region_number'})

expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
end

it 'only allows specifying of valid attributes' do
api_basic_authorize

get(api_pictures_url, :params => { :expand => 'resources', :attributes => 'bad_attr'})

expect(response).to have_http_status(:bad_request)
end
end

describe 'GET /api/pictures/:id' do
Expand All @@ -109,6 +132,16 @@ def expect_result_to_include_picture_href(source_id)
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include('image_href' => a_string_including(@picture.image_href), 'extension' => @picture.extension)
end

it 'allows specifying of additional attributes' do
api_basic_authorize

get(api_picture_url(nil, @picture), :params => { :attributes => 'href_slug,region_number' })

expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include('href_slug' => @picture.href_slug,
'region_number' => @picture.region_number)
end
end

describe 'POST /api/pictures' do
Expand Down

0 comments on commit f7f4580

Please sign in to comment.