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 27, 2018
1 parent 9fcf7a1 commit 2ae5d78
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/controllers/api/mixins/pictures.rb
Expand Up @@ -7,7 +7,9 @@ def fetch_picture(resource)

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)
normalize_hash(:picture, picture_hash)
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions app/controllers/api/pictures_controller.rb
Expand Up @@ -16,5 +16,13 @@ def create_resource(_type, _id, data)
def set_additional_attributes
@additional_attributes = %w(image_href extension)
end

def attribute_selection
if !@req.attributes.empty? || @additional_attributes
@req.attributes | Array(@additional_attributes) | ID_ATTRS
else
Picture.attribute_names - %w(content)
end
end
end
end
42 changes: 42 additions & 0 deletions spec/requests/pictures_spec.rb
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,25 @@ 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

it 'will return only the requested physical attribute with the set additional attributes' do
api_basic_authorize

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

expect(response).to have_http_status(:ok)
expect(response.parsed_body.keys).to match_array(%w(md5 href id extension image_href))
end
end

describe 'POST /api/pictures' do
Expand Down

0 comments on commit 2ae5d78

Please sign in to comment.