Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add support for creating beers and breweries.

  • Loading branch information...
commit 0d7a520d5b1724cb827858ca186eee91a27c3d4c 1 parent f48d288
Tristan Dunn tristandunn authored
12 lib/bartender/beer.rb
View
@@ -12,6 +12,18 @@ def self.all(options = {})
Bartender::Request.get("/beers", options)
end
+ # Create a beer.
+ #
+ # @param [Hash] attributes The attributes for the beer.
+ # @option attributes [Integer] :brewery_id The ID of the brewery the beer belongs to.
+ # @option attributes [Hash] :beer The attributes for the beer.
+ # @param [Hash] options The options for the API request.
+ # @option options [String] :token A private token to override the current configuration with.
+ # @return [String,Hash,false] The location of the new beer if created, errors hash for validation errors, +false+ otherwise.
+ def self.create(attributes = {}, options = {})
+ Bartender::Request.post("/beers", attributes, options)
+ end
+
# Retrieve a specific beer.
#
# @param [Fixnum] id The ID of the beer to retrieve.
10 lib/bartender/brewery.rb
View
@@ -12,6 +12,16 @@ def self.all(options = {})
Bartender::Request.get("/breweries", options)
end
+ # Create a brewery.
+ #
+ # @param [Hash] attributes The attributes for the brewery.
+ # @param [Hash] options The options for the API request.
+ # @option options [String] :token A private token to override the current configuration with.
+ # @return [String,Hash,false] The location of the new brewery if created, errors hash for validation errors, +false+ otherwise.
+ def self.create(attributes = {}, options = {})
+ Bartender::Request.post("/breweries", { :brewery => attributes }, options)
+ end
+
# Retrieve a specific brewery.
#
# @param [Fixnum] id The ID of the brewery to retrieve.
15 lib/bartender/request.rb
View
@@ -14,6 +14,21 @@ def self.get(path, options = {})
JSON.parse(request.get(uri(path, options)).body)
end
+ def self.post(path, attributes = {}, options = {})
+ options[:token] ||= Bartender.configuration.private_token
+
+ response = request.post(uri(path, options), attributes.to_json, "Content-Type" => "application/json")
+
+ case response.code.to_i
+ when 201
+ response["Location"]
+ when 400
+ JSON.parse(response.body)["errors"]
+ else
+ false
+ end
+ end
+
def self.request
http = Net::HTTP.new(Bartender.configuration.host, Bartender.configuration.port)
http.open_timeout = Bartender.configuration.http_open_timeout
14 spec/bartender/beer_spec.rb
View
@@ -16,6 +16,20 @@
end
end
+describe Bartender::Beer, ".create" do
+ let(:options) { { :token => "Fake" } }
+ let(:attributes) { { :name => "Awesome" } }
+
+ before do
+ Bartender::Request.stubs(:post)
+ end
+
+ it "creates a beer" do
+ Bartender::Beer.create(attributes, options)
+ Bartender::Request.should have_received(:post).with("/beers", attributes, options)
+ end
+end
+
describe Bartender::Beer, ".find" do
before do
Bartender::Request.stubs(:get)
14 spec/bartender/brewery_spec.rb
View
@@ -16,6 +16,20 @@
end
end
+describe Bartender::Brewery, ".create" do
+ let(:options) { { :token => "Fake" } }
+ let(:attributes) { { :name => "Awesome" } }
+
+ before do
+ Bartender::Request.stubs(:post)
+ end
+
+ it "creates a brewery" do
+ Bartender::Brewery.create(attributes, options)
+ Bartender::Request.should have_received(:post).with("/breweries", { :brewery => attributes }, options)
+ end
+end
+
describe Bartender::Brewery, ".find" do
before do
Bartender::Request.stubs(:get)
68 spec/bartender/request_spec.rb
View
@@ -1,19 +1,30 @@
require "spec_helper"
describe Bartender::Request, ".get" do
- let(:request) { stub("Request", :get => response) }
- let(:response) { stub("Response", :body => "{}") }
+ let(:request) { stub("Request", :get => response) }
+ let(:response) { stub("Response", :body => "{}") }
+ let(:public_token) { "a1b2c3" }
before do
Bartender::Request.stubs(:uri).returns("/")
Bartender::Request.stubs(:request).returns(request)
-
- Bartender.configuration.public_token = nil
end
it "constructs the request URI" do
- Bartender::Request.get("/", {})
- Bartender::Request.should have_received(:uri).with("/", {})
+ Bartender::Request.get("/", :option => 1)
+ Bartender::Request.should have_received(:uri).with("/", :option => 1)
+ end
+
+ it "includes public token when configured" do
+ Bartender.configuration.public_token = public_token
+ Bartender::Request.get("/")
+ Bartender::Request.should have_received(:uri).with("/", :token => public_token)
+ end
+
+ it "allows overriding of configured public token" do
+ Bartender.configuration.public_token = public_token
+ Bartender::Request.get("/", :token => "x1y2z3")
+ Bartender::Request.should have_received(:uri).with("/", :token => "x1y2z3")
end
it "makes an API request" do
@@ -24,18 +35,49 @@
it "returns the parsed JSON response body" do
Bartender::Request.get("/").should == {}
end
+end
- it "includes public token when configured" do
- Bartender.configuration.public_token = "a1b2c3"
- Bartender::Request.get("/")
- Bartender::Request.should have_received(:uri).with("/", :token => "a1b2c3")
+describe Bartender::Request, ".post" do
+ let(:headers) { { "Content-Type" => "application/json" } }
+ let(:request) { stub("Request", :post => response) }
+ let(:location) { "/v1/records/1" }
+ let(:response) { stub("Response", :code => 201, :[] => location) }
+ let(:private_token) { "a1b2c3" }
+
+ before do
+ Bartender::Request.stubs(:uri).returns("/")
+ Bartender::Request.stubs(:request).returns(request)
+ Bartender.configuration.private_token = private_token
end
- it "allows overriding of configured public token" do
- Bartender.configuration.public_token = "x1y2z3"
- Bartender::Request.get("/")
+ it "constructs the request URI" do
+ Bartender::Request.post("/", {}, :option => 1)
+ Bartender::Request.should have_received(:uri).with("/", :option => 1, :token => private_token)
+ end
+
+ it "allows overriding of configured private token" do
+ Bartender::Request.post("/", {}, :token => "x1y2z3")
Bartender::Request.should have_received(:uri).with("/", :token => "x1y2z3")
end
+
+ it "makes an API request" do
+ Bartender::Request.post("/", :name => "Example")
+ request.should have_received(:post).with("/", { :name => "Example" }.to_json, headers)
+ end
+
+ it "returns the location header for a created response" do
+ Bartender::Request.post("/").should == location
+ end
+
+ it "returns an errors hash for a bad request response" do
+ response.stubs(:code => "400", :body => '{"errors":{}}')
+ Bartender::Request.post("/").should == {}
+ end
+
+ it "returns false for all other responses" do
+ response.stubs(:code => "401")
+ Bartender::Request.post("/").should == false
+ end
end
describe Bartender::Request, ".request" do
Please sign in to comment.
Something went wrong with that request. Please try again.