Skip to content

Commit

Permalink
Add method to push gems
Browse files Browse the repository at this point in the history
Closes rubygems#3.
  • Loading branch information
sferik committed Jul 2, 2011
1 parent bad804b commit 7eeac65
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 18 deletions.
1 change: 0 additions & 1 deletion .gitignore
@@ -1,4 +1,3 @@
*.gem
*.rbc
.DS_Store
.bundle
Expand Down
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -76,6 +76,9 @@ Usage Examples
# Test fire a webhook.
Gems.fire_web_hook 'rails', 'http://example.com'

# Submit a gem to RubyGems.org.
Gems.push File.new 'gemcutter-0.2.1.gem'

# Remove a gem from RubyGems.org's index.
# Defaults to the latest version if no version is specified.
Gems.yank 'bills', '0.0.1'
Expand Down
10 changes: 10 additions & 0 deletions lib/gems/client.rb
Expand Up @@ -173,6 +173,16 @@ def fire_web_hook(gem_name, url)
post("/api/v1/web_hooks/fire", {:gem_name => gem_name, :url => url}, :raw)
end

# Submit a gem to RubyGems.org
#
# @param gem [File] A built gem.
# @return [String]
# @example
# Gems.push(File.new("pkg/gemcutter-0.2.1.gem"))
def push(gem)
post("/api/v1/gems", gem.read, :raw, 'application/octet-stream')
end

# Remove a gem from RubyGems.org's index
#
# @param gem_name [String] The name of a gem.
Expand Down
9 changes: 5 additions & 4 deletions lib/gems/configuration.rb
Expand Up @@ -17,14 +17,15 @@ module Configuration
# Set the default response format appended to the path
#
# @note JSON is preferred over XML because it is more concise and faster to parse.
DEFAULT_FORMAT = :json
DEFAULT_FORMAT = :json.freeze

DEFAULT_HOST = ENV['RUBYGEMS_HOST'] ? ENV['RUBYGEMS_HOST'] : 'https://rubygems.org'
# Set the default API endpoint
DEFAULT_HOST = ENV['RUBYGEMS_HOST'] ? ENV['RUBYGEMS_HOST'].freeze : 'https://rubygems.org'.freeze

# Set the default credentials
DEFAULT_KEY = Gem.configuration.rubygems_api_key
DEFAULT_KEY = Gem.configuration.rubygems_api_key.freeze

# Set the default value sent in the 'User-Agent' header
# Set the default 'User-Agent' HTTP header
DEFAULT_USER_AGENT = "Gems #{Gems::VERSION}".freeze

attr_accessor *VALID_OPTIONS_KEYS
Expand Down
6 changes: 4 additions & 2 deletions lib/gems/connection.rb
Expand Up @@ -2,7 +2,7 @@

module Gems
module Connection
def connection(format=format)
def connection(content_length=nil, content_type=nil, format=foramt)
options = {
:headers => {
:user_agent => user_agent,
Expand All @@ -11,10 +11,12 @@ def connection(format=format)
:url => host,
}

options[:headers].merge!({:content_length => content_length}) if content_length
options[:headers].merge!({:content_type => content_type}) if content_type
options[:headers].merge!({:authorization => key}) if key

connection = Faraday.new(options) do |connection|
connection.use Faraday::Request::UrlEncoded
connection.use Faraday::Request::UrlEncoded unless content_type
case format.to_s.downcase
when 'json'
connection.use Faraday::Response::ParseJson
Expand Down
28 changes: 17 additions & 11 deletions lib/gems/request.rb
@@ -1,31 +1,37 @@
require 'rubygems'

module Gems
module Request
def delete(path, options={}, format=format)
request(:delete, path, options, format)
def delete(path, options={}, format=format, content_type=nil)
request(:delete, path, options, format, content_type)
end

def get(path, options={}, format=format)
request(:get, path, options, format)
def get(path, options={}, format=format, content_type=nil)
request(:get, path, options, format, content_type)
end

def post(path, options={}, format=format)
request(:post, path, options, format)
def post(path, options={}, format=format, content_type=nil)
request(:post, path, options, format, content_type)
end

def put(path, options={}, format=format)
request(:put, path, options, format)
def put(path, options={}, format=format, content_type=nil)
request(:put, path, options, format, content_type)
end

private

def request(method, path, options, format)
response = connection(format).send(method) do |request|
def request(method, path, options, format, content_type)
content_length = case content_type
when 'application/octet-stream'
options.size
end
response = connection(content_length, content_type, format).send(method) do |request|
case method
when :delete, :get
request.url(formatted_path(path, format), options)
when :post, :put
request.path = formatted_path(path, format)
request.body = options unless options.empty?
request.body = options unless options == {}
end
end
response.body
Expand Down
Binary file added spec/fixtures/gems-0.0.8.gem
Binary file not shown.
1 change: 1 addition & 0 deletions spec/fixtures/push
@@ -0,0 +1 @@
Successfully registered gem: gems (0.0.8)
14 changes: 14 additions & 0 deletions spec/gems/client_spec.rb
Expand Up @@ -289,6 +289,20 @@
end
end

describe ".push" do
before do
stub_post("/api/v1/gems").
to_return(:body => fixture("push"))
end

it "should submit a gem to RubyGems.org" do
push = Gems.push(File.new(File.expand_path("../../fixtures/gems-0.0.8.gem", __FILE__), "rb"))
a_post("/api/v1/gems").
should have_been_made
push.should == "Successfully registered gem: gems (0.0.8)"
end
end

describe ".yank" do
context "with no version specified" do
before do
Expand Down

0 comments on commit 7eeac65

Please sign in to comment.