Permalink
Browse files

Add support for YAML response format

  • Loading branch information...
1 parent a1a4f6f commit cb7812ee458c4936caa62302558039ffee810804 @sferik committed Jul 2, 2011
Showing with 203 additions and 184 deletions.
  1. +1 −1 lib/gems/client.rb
  2. +2 −0 lib/gems/connection.rb
  3. +1 −1 lib/gems/request.rb
  4. +2 −0 spec/fixtures/owners.yaml
  5. +197 −182 spec/gems/client_spec.rb
View
2 lib/gems/client.rb
@@ -105,7 +105,7 @@ def gems
# @example
# Gems.owners('gemcutter')
def owners(gem_name)
- get("/api/v1/gems/#{gem_name}/owners", {}, :json)
+ get("/api/v1/gems/#{gem_name}/owners")
end
# Add an owner to a RubyGem you own, giving that user permission to manage it
View
2 lib/gems/connection.rb
@@ -23,6 +23,8 @@ def connection(format=format)
connection.use Faraday::Response::ParseMarshal
when 'xml'
connection.use Faraday::Response::ParseXml
+ when 'yaml'
+ connection.use Faraday::Response::ParseYaml
end
connection.use Faraday::Response::RaiseError
connection.adapter Faraday.default_adapter
View
2 lib/gems/request.rb
@@ -29,7 +29,7 @@ def request(method, path, options, format)
def formatted_path(path, format)
case format.to_s.downcase
- when 'json', 'xml'
+ when 'json', 'xml', 'yaml'
[path, format].compact.join('.')
when 'marshal', 'raw'
path
View
2 spec/fixtures/owners.yaml
@@ -0,0 +1,2 @@
+---
+- email: sferik@gmail.com
View
379 spec/gems/client_spec.rb
@@ -1,23 +1,23 @@
require 'helper'
describe Gems::Client do
- %w(json xml).each do |format|
- context ".new(:format => '#{format}')" do
- before do
- Gems.configure do |config|
- config.format = format
- config.key = '701243f217cdf23b1370c7b66b65ca97'
- config.username = 'nick@gemcutter.org'
- config.password = 'schwwwwing'
- end
- end
+ before do
+ Gems.configure do |config|
+ config.key = '701243f217cdf23b1370c7b66b65ca97'
+ config.username = 'nick@gemcutter.org'
+ config.password = 'schwwwwing'
+ end
+ end
- after do
- Gems.reset
- end
+ after do
+ Gems.reset
+ end
- describe ".info" do
+ describe ".info" do
+ %w(json xml).each do |format|
+ context "with format #{format}" do
before do
+ Gems.format = format
stub_get("/api/v1/gems/rails.#{format}").
to_return(:body => fixture("rails.#{format}"))
end
@@ -29,9 +29,14 @@
info.name.should == 'rails'
end
end
+ end
+ end
- describe ".search" do
+ describe ".search" do
+ %w(json xml).each do |format|
+ context "with format #{format}" do
before do
+ Gems.format = format
stub_get("/api/v1/search.#{format}").
with(:query => {"query" => "cucumber"}).
to_return(:body => fixture("search.#{format}"))
@@ -45,101 +50,106 @@
search.first.name.should == 'cucumber'
end
end
+ end
+ end
- describe ".versions" do
- before do
- stub_get("/api/v1/versions/coulda.json").
- to_return(:body => fixture("coulda.json"))
- end
+ describe ".versions" do
+ before do
+ stub_get("/api/v1/versions/coulda.json").
+ to_return(:body => fixture("coulda.json"))
+ end
- it "should return an array of gem version details" do
- versions = Gems.versions 'coulda'
- a_get("/api/v1/versions/coulda.json").
- should have_been_made
- versions.first.number.should == '0.6.3'
- end
+ it "should return an array of gem version details" do
+ versions = Gems.versions 'coulda'
+ a_get("/api/v1/versions/coulda.json").
+ should have_been_made
+ versions.first.number.should == '0.6.3'
+ end
+ end
+
+ describe ".downloads" do
+ context "with no dates specified" do
+ before do
+ stub_get("/api/v1/versions/coulda-0.6.3/downloads.json").
+ to_return(:body => fixture("downloads.json"))
end
- describe ".downloads" do
- context "with no dates specified" do
- before do
- stub_get("/api/v1/versions/coulda-0.6.3/downloads.json").
- to_return(:body => fixture("downloads.json"))
- end
-
- it "should return the number of downloads by day for a particular gem version" do
- downloads = Gems.downloads 'coulda', '0.6.3'
- a_get("/api/v1/versions/coulda-0.6.3/downloads.json").
- should have_been_made
- downloads["2011-06-22"].should == 8
- end
- end
+ it "should return the number of downloads by day for a particular gem version" do
+ downloads = Gems.downloads 'coulda', '0.6.3'
+ a_get("/api/v1/versions/coulda-0.6.3/downloads.json").
+ should have_been_made
+ downloads["2011-06-22"].should == 8
+ end
+ end
- context "with from date specified" do
- before do
- stub_get("/api/v1/versions/coulda-0.6.3/downloads/search.json").
- with(:query => {"from" => "2011-01-01", "to" => Date.today.to_s}).
- to_return(:body => fixture("downloads.json"))
- end
-
- it "should return the number of downloads by day for a particular gem version" do
- downloads = Gems.downloads 'coulda', '0.6.3', Date.parse('2011-01-01')
- a_get("/api/v1/versions/coulda-0.6.3/downloads/search.json").
- with(:query => {"from" => "2011-01-01", "to" => Date.today.to_s}).
- should have_been_made
- downloads["2011-06-22"].should == 8
- end
- end
+ context "with from date specified" do
+ before do
+ stub_get("/api/v1/versions/coulda-0.6.3/downloads/search.json").
+ with(:query => {"from" => "2011-01-01", "to" => Date.today.to_s}).
+ to_return(:body => fixture("downloads.json"))
+ end
- context "with from and to dates specified" do
- before do
- stub_get("/api/v1/versions/coulda-0.6.3/downloads/search.json").
- with(:query => {"from" => "2011-01-01", "to" => "2011-06-28"}).
- to_return(:body => fixture("downloads.json"))
- end
-
- it "should return the number of downloads by day for a particular gem version" do
- downloads = Gems.downloads 'coulda', '0.6.3', Date.parse('2011-01-01'), Date.parse('2011-06-28')
- a_get("/api/v1/versions/coulda-0.6.3/downloads/search.json").
- with(:query => {"from" => "2011-01-01", "to" => "2011-06-28"}).
- should have_been_made
- downloads["2011-06-22"].should == 8
- end
- end
+ it "should return the number of downloads by day for a particular gem version" do
+ downloads = Gems.downloads 'coulda', '0.6.3', Date.parse('2011-01-01')
+ a_get("/api/v1/versions/coulda-0.6.3/downloads/search.json").
+ with(:query => {"from" => "2011-01-01", "to" => Date.today.to_s}).
+ should have_been_made
+ downloads["2011-06-22"].should == 8
end
+ end
- describe ".dependencies" do
- before do
- stub_get("/api/v1/dependencies").
- with(:query => {"gems" => "rails,thor"}).
- to_return(:body => fixture("dependencies"))
- end
+ context "with from and to dates specified" do
+ before do
+ stub_get("/api/v1/versions/coulda-0.6.3/downloads/search.json").
+ with(:query => {"from" => "2011-01-01", "to" => "2011-06-28"}).
+ to_return(:body => fixture("downloads.json"))
+ end
- it "should return an array of hashes for all versions of given gems" do
- dependencies = Gems.dependencies 'rails', 'thor'
- a_get("/api/v1/dependencies").
- with(:query => {"gems" => "rails,thor"}).
- should have_been_made
- dependencies.first.number.should == "3.0.9"
- end
+ it "should return the number of downloads by day for a particular gem version" do
+ downloads = Gems.downloads 'coulda', '0.6.3', Date.parse('2011-01-01'), Date.parse('2011-06-28')
+ a_get("/api/v1/versions/coulda-0.6.3/downloads/search.json").
+ with(:query => {"from" => "2011-01-01", "to" => "2011-06-28"}).
+ should have_been_made
+ downloads["2011-06-22"].should == 8
end
+ end
+ end
- describe ".api_key" do
- before do
- stub_get("/api/v1/api_key").
- to_return(:body => fixture("api_key"))
- end
+ describe ".dependencies" do
+ before do
+ stub_get("/api/v1/dependencies").
+ with(:query => {"gems" => "rails,thor"}).
+ to_return(:body => fixture("dependencies"))
+ end
- it "should retrieve an API key" do
- api_key = Gems.api_key
- a_get("/api/v1/api_key").
- should have_been_made
- api_key.should == "701243f217cdf23b1370c7b66b65ca97"
- end
- end
+ it "should return an array of hashes for all versions of given gems" do
+ dependencies = Gems.dependencies 'rails', 'thor'
+ a_get("/api/v1/dependencies").
+ with(:query => {"gems" => "rails,thor"}).
+ should have_been_made
+ dependencies.first.number.should == "3.0.9"
+ end
+ end
- describe ".gems" do
+ describe ".api_key" do
+ before do
+ stub_get("/api/v1/api_key").
+ to_return(:body => fixture("api_key"))
+ end
+
+ it "should retrieve an API key" do
+ api_key = Gems.api_key
+ a_get("/api/v1/api_key").
+ should have_been_made
+ api_key.should == "701243f217cdf23b1370c7b66b65ca97"
+ end
+ end
+
+ describe ".gems" do
+ %w(json xml).each do |format|
+ context "with format #{format}" do
before do
+ Gems.format = format
stub_get("/api/v1/gems.#{format}").
to_return(:body => fixture("gems.#{format}"))
end
@@ -151,114 +161,119 @@
gems.first.name.should == "congress"
end
end
+ end
+ end
- describe ".owners" do
+ describe ".owners" do
+ %w(json yaml).each do |format|
+ context "with format #{format}" do
before do
- stub_get("/api/v1/gems/gems/owners.json").
- to_return(:body => fixture("owners.json"))
+ Gems.format = format
+ stub_get("/api/v1/gems/gems/owners.#{format}").
+ to_return(:body => fixture("owners.#{format}"))
end
it "should list all owners of a gem" do
owners = Gems.owners("gems")
- a_get("/api/v1/gems/gems/owners.json").
+ a_get("/api/v1/gems/gems/owners.#{format}").
should have_been_made
owners.first.email.should == "sferik@gmail.com"
end
end
+ end
+ end
- describe ".add_owner" do
- before do
- stub_post("/api/v1/gems/gems/owners").
- with(:body => {:email => "sferik@gmail.com"}).
- to_return(:body => fixture("add_owner.json"))
- end
+ describe ".add_owner" do
+ before do
+ stub_post("/api/v1/gems/gems/owners").
+ with(:body => {:email => "sferik@gmail.com"}).
+ to_return(:body => fixture("add_owner.json"))
+ end
- it "should add an owner to a RubyGem" do
- owner = Gems.add_owner("gems", "sferik@gmail.com")
- a_post("/api/v1/gems/gems/owners").
- with(:body => {:email => "sferik@gmail.com"}).
- should have_been_made
- owner.should == "Owner added successfully."
- end
- end
+ it "should add an owner to a RubyGem" do
+ owner = Gems.add_owner("gems", "sferik@gmail.com")
+ a_post("/api/v1/gems/gems/owners").
+ with(:body => {:email => "sferik@gmail.com"}).
+ should have_been_made
+ owner.should == "Owner added successfully."
+ end
+ end
- describe ".remove_owner" do
- before do
- stub_delete("/api/v1/gems/gems/owners").
- with(:query => {:email => "sferik@gmail.com"}).
- to_return(:body => fixture("remove_owner.json"))
- end
+ describe ".remove_owner" do
+ before do
+ stub_delete("/api/v1/gems/gems/owners").
+ with(:query => {:email => "sferik@gmail.com"}).
+ to_return(:body => fixture("remove_owner.json"))
+ end
- it "should remove an owner from a RubyGem" do
- owner = Gems.remove_owner("gems", "sferik@gmail.com")
- a_delete("/api/v1/gems/gems/owners").
- with(:query => {:email => "sferik@gmail.com"}).
- should have_been_made
- owner.should == "Owner removed successfully."
- end
- end
+ it "should remove an owner from a RubyGem" do
+ owner = Gems.remove_owner("gems", "sferik@gmail.com")
+ a_delete("/api/v1/gems/gems/owners").
+ with(:query => {:email => "sferik@gmail.com"}).
+ should have_been_made
+ owner.should == "Owner removed successfully."
+ end
+ end
- describe ".web_hooks" do
- before do
- stub_get("/api/v1/web_hooks.json").
- to_return(:body => fixture("web_hooks.json"))
- end
+ describe ".web_hooks" do
+ before do
+ stub_get("/api/v1/web_hooks.json").
+ to_return(:body => fixture("web_hooks.json"))
+ end
- it "should list the webhooks registered under your account" do
- web_hooks = Gems.web_hooks
- a_get("/api/v1/web_hooks.json").
- should have_been_made
- web_hooks.rails.first.url.should == "http://example.com"
- end
- end
+ it "should list the webhooks registered under your account" do
+ web_hooks = Gems.web_hooks
+ a_get("/api/v1/web_hooks.json").
+ should have_been_made
+ web_hooks.rails.first.url.should == "http://example.com"
+ end
+ end
- describe ".add_web_hook" do
- before do
- stub_post("/api/v1/web_hooks").
- with(:body => {:gem_name => "*", :url => "http://example.com"}).
- to_return(:body => fixture("add_web_hook"))
- end
+ describe ".add_web_hook" do
+ before do
+ stub_post("/api/v1/web_hooks").
+ with(:body => {:gem_name => "*", :url => "http://example.com"}).
+ to_return(:body => fixture("add_web_hook"))
+ end
- it "should add a web hook" do
- add_web_hook = Gems.add_web_hook("*", "http://example.com")
- a_post("/api/v1/web_hooks").
- with(:body => {:gem_name => "*", :url => "http://example.com"}).
- should have_been_made
- add_web_hook.should == "Successfully created webhook for all gems to http://example.com"
- end
- end
+ it "should add a web hook" do
+ add_web_hook = Gems.add_web_hook("*", "http://example.com")
+ a_post("/api/v1/web_hooks").
+ with(:body => {:gem_name => "*", :url => "http://example.com"}).
+ should have_been_made
+ add_web_hook.should == "Successfully created webhook for all gems to http://example.com"
+ end
+ end
- describe ".remove_web_hook" do
- before do
- stub_delete("/api/v1/web_hooks/remove").
- with(:query => {:gem_name => "*", :url => "http://example.com"}).
- to_return(:body => fixture("remove_web_hook"))
- end
+ describe ".remove_web_hook" do
+ before do
+ stub_delete("/api/v1/web_hooks/remove").
+ with(:query => {:gem_name => "*", :url => "http://example.com"}).
+ to_return(:body => fixture("remove_web_hook"))
+ end
- it "should remove a web hook" do
- remove_web_hook = Gems.remove_web_hook("*", "http://example.com")
- a_delete("/api/v1/web_hooks/remove").
- with(:query => {:gem_name => "*", :url => "http://example.com"}).
- should have_been_made
- remove_web_hook.should == "Successfully removed webhook for all gems to http://example.com"
- end
- end
+ it "should remove a web hook" do
+ remove_web_hook = Gems.remove_web_hook("*", "http://example.com")
+ a_delete("/api/v1/web_hooks/remove").
+ with(:query => {:gem_name => "*", :url => "http://example.com"}).
+ should have_been_made
+ remove_web_hook.should == "Successfully removed webhook for all gems to http://example.com"
+ end
+ end
- describe ".fire_web_hook" do
- before do
- stub_post("/api/v1/web_hooks/fire").
- with(:body => {:gem_name => "*", :url => "http://example.com"}).
- to_return(:body => fixture("fire_web_hook"))
- end
+ describe ".fire_web_hook" do
+ before do
+ stub_post("/api/v1/web_hooks/fire").
+ with(:body => {:gem_name => "*", :url => "http://example.com"}).
+ to_return(:body => fixture("fire_web_hook"))
+ end
- it "should fire a web hook" do
- fire_web_hook = Gems.fire_web_hook("*", "http://example.com")
- a_post("/api/v1/web_hooks/fire").
- with(:body => {:gem_name => "*", :url => "http://example.com"}).
- should have_been_made
- fire_web_hook.should == "Successfully deployed webhook for gemcutter to http://example.com"
- end
- end
+ it "should fire a web hook" do
+ fire_web_hook = Gems.fire_web_hook("*", "http://example.com")
+ a_post("/api/v1/web_hooks/fire").
+ with(:body => {:gem_name => "*", :url => "http://example.com"}).
+ should have_been_made
+ fire_web_hook.should == "Successfully deployed webhook for gemcutter to http://example.com"
end
end
end

0 comments on commit cb7812e

Please sign in to comment.