mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-05-25 01:13:03 -04:00
Compare commits
7 Commits
feature-us
...
api_v1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d066801943 | ||
|
|
016feaf8bf | ||
|
|
1b5d2bb898 | ||
|
|
a3e3abecf3 | ||
|
|
223a06ef8b | ||
|
|
f3eb5774ef | ||
|
|
800a1f10e3 |
3
Gemfile
3
Gemfile
@@ -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
|
||||
|
||||
12
Gemfile.lock
12
Gemfile.lock
@@ -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)
|
||||
|
||||
13
app/controllers/api/v1/crops_controller.rb
Normal file
13
app/controllers/api/v1/crops_controller.rb
Normal 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
|
||||
11
app/views/api/v1/_partials/_base.json.jbuilder
Normal file
11
app/views/api/v1/_partials/_base.json.jbuilder
Normal 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
|
||||
7
app/views/api/v1/crops/index.json.jbuilder
Normal file
7
app/views/api/v1/crops/index.json.jbuilder
Normal 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'
|
||||
8
app/views/api/v1/crops/show.json.jbuilder
Normal file
8
app/views/api/v1/crops/show.json.jbuilder
Normal 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'
|
||||
@@ -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
|
||||
|
||||
52
spec/api/v1/controllers/api_crops_controller_spec.rb
Normal file
52
spec/api/v1/controllers/api_crops_controller_spec.rb
Normal 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
|
||||
Reference in New Issue
Block a user