Compare commits

...

7 Commits

Author SHA1 Message Date
Skud
d066801943 Merge pull request #616 from pmackay/api_v1
Api v1 - initial start
2015-01-13 10:50:01 +11:00
Paul Mackay
016feaf8bf #582: Change license checks to expect() syntax. 2015-01-12 10:25:00 +00:00
Paul Mackay
1b5d2bb898 #582: Fixes for review comments. 2015-01-11 19:25:40 +00:00
Paul Mackay
a3e3abecf3 Merge api-v1-jbuilder into new API branch. 2015-01-07 08:41:26 +00:00
Paul Mackay
223a06ef8b Some crop API tweaks, basic LD stuff. 2014-11-07 07:03:27 +00:00
Paul Mackay
f3eb5774ef Add spec test for 2 crop API calls, including license. 2014-11-03 16:45:37 +00:00
Paul Mackay
800a1f10e3 First commit for v1 API implementation based on jbuilder. 2014-11-03 15:03:59 +00:00
8 changed files with 111 additions and 3 deletions

View File

@@ -73,7 +73,7 @@ gem 'flickraw'
# gem 'bcrypt-ruby', '~> 3.0.0'
# To use Jbuilder templates for JSON
# gem 'jbuilder'
gem 'jbuilder'
# Use unicorn as the app server
# gem 'unicorn'
@@ -130,4 +130,5 @@ group :development, :test do
gem 'capybara-email' # integration tests for email
gem 'poltergeist', '~> 1.5.1' # for headless JS testing
gem 'i18n-tasks' # adds tests for finding missing and unused translations
gem 'json_spec' # extra ways to test JSON data
end

View File

@@ -158,6 +158,9 @@ GEM
slop (>= 3.5.0)
term-ansicolor
terminal-table
jbuilder (2.2.6)
activesupport (>= 3.0.0, < 5)
multi_json (~> 1.2)
journey (1.0.4)
jquery-rails (3.1.2)
railties (>= 3.0, < 5.0)
@@ -168,6 +171,9 @@ GEM
railties (>= 3.2)
sprockets-rails
json (1.7.7)
json_spec (1.1.4)
multi_json (~> 1.0)
rspec (>= 2.0, < 4.0)
kgio (2.9.2)
launchy (2.4.3)
addressable (~> 2.3)
@@ -256,6 +262,10 @@ GEM
rest-client (1.7.2)
mime-types (>= 1.16, < 3.0)
netrc (~> 0.7)
rspec (2.12.0)
rspec-core (~> 2.12.0)
rspec-expectations (~> 2.12.0)
rspec-mocks (~> 2.12.0)
rspec-core (2.12.2)
rspec-expectations (2.12.1)
diff-lcs (~> 1.1.3)
@@ -350,10 +360,12 @@ DEPENDENCIES
haml
haml-rails
i18n-tasks
jbuilder
jquery-rails
jquery-ui-rails
js-routes
json (~> 1.7.7)
json_spec
leaflet-markercluster-rails
leaflet-rails
less (~> 2.5.0)

View File

@@ -0,0 +1,13 @@
class Api::V1::CropsController < ApplicationController
# GET /api/v1/crops
def index
@crops = Crop.all
end
# GET /crops/1
def show
@crop = Crop.find(params[:id])
end
end

View File

@@ -0,0 +1,11 @@
json.version '1.0.0'
json.license do
json.name "Creative Commons Attribution ShareAlike 3.0 Unported"
json.short_name "CC-BY-SA"
json.url "http://creativecommons.org/licenses/by-sa/3.0/"
json.credit "Growstuff"
json.link "http://growstuff.org/"
json.easy_link '<a href="http://creativecommons.org/licenses/by-sa/3.0/">CC-BY-SA</a> <a href="http://growstuff.org/">Growstuff</a>'
end

View File

@@ -0,0 +1,7 @@
json.data @crops do |crop|
json.(crop, :id, :name)
json.set! '@id', url_for(:only_path => false) + '/' + crop.id.to_s
json.url crop_url(crop)
end
json.partial! 'api/v1/_partials/base'

View File

@@ -0,0 +1,8 @@
json.data do
json.(@crop, :id, :name, :creator_id, :en_wikipedia_url)
json.set! '@id', url_for(:only_path => false)
json.url crop_url(@crop)
json.set! '@type', 'Crop'
end
json.partial! 'api/v1/_partials/base'

View File

@@ -4,7 +4,7 @@ Growstuff::Application.routes.draw do
devise_for :members, :controllers => { :registrations => "registrations", :passwords => "passwords" }
resources :members
resources :members
resources :photos
@@ -84,6 +84,10 @@ Growstuff::Application.routes.draw do
match '/admin/newsletter' => 'admin#newsletter', :as => :admin_newsletter
match '/admin/:action' => 'admin#:action'
namespace :api, :defaults => {:format => :json} do
namespace :v1 do
resources :crops
end
end
end

View File

@@ -0,0 +1,52 @@
require 'spec_helper'
describe Api::V1::CropsController do
let(:crop) { FactoryGirl.create(:tomato) }
def check_license(response)
json = JSON.parse response.body
expect(json['license']).to_not eq(nil)
expect(json['license']['name']).to eq("Creative Commons Attribution ShareAlike 3.0 Unported")
expect(json['license']['short_name']).to eq("CC-BY-SA")
expect(json['license']['url']).to eq("http://creativecommons.org/licenses/by-sa/3.0/")
expect(json['license']['credit']).to eq("Growstuff")
expect(json['license']['link']).to eq("http://growstuff.org/")
expect(json['license']['easy_link']).to eq('<a href="http://creativecommons.org/licenses/by-sa/3.0/">CC-BY-SA</a> <a href="http://growstuff.org/">Growstuff</a>')
end
context "GET crops" do
it 'has empty list' do
get '/api/v1/crops'
expect(response.response_code).to eq(200)
expect(response.content_type).to eq("application/json")
expect(response.body).to have_json_size(0).at_path('data')
check_license response
end
it 'has 1 item list' do
# create a crop instance
crop
get '/api/v1/crops'
expect(response.response_code).to eq(200)
expect(response.content_type).to eq("application/json")
expect(response.body).to have_json_size(1).at_path('data')
check_license response
end
end
context "GET one crop" do
it 'has crop item' do
get '/api/v1/crops/' + crop.id.to_s
expect(response.response_code).to eq(200)
expect(response.content_type).to eq("application/json")
json = JSON.parse response.body
expect(response.body).to have_json_path("data")
expect(json['data']['name']).to eq("tomato")
check_license response
end
end
end