Skip to content

Commit

Permalink
Added possibilty to create a private gist.
Browse files Browse the repository at this point in the history
  • Loading branch information
alloy committed Aug 21, 2009
1 parent db69a75 commit 0c35619
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
11 changes: 7 additions & 4 deletions lib/cocoa_gist.rb
Expand Up @@ -20,8 +20,8 @@ def self.credentials
}.reject { |_, v| v.empty? }
end

def start(content, syntax = 'ruby')
request = post_request(params(content, syntax))
def start(content, syntax = 'ruby', private_gist = false)
request = post_request(params(content, syntax, private_gist))
@connection = OSX::NSURLConnection.alloc.initWithRequest_delegate(request, self)
end

Expand Down Expand Up @@ -54,11 +54,14 @@ def post_request(body)
request
end

def params(content, syntax)
self.class.credentials.merge({
def params(content, syntax, private_gist)
params = self.class.credentials.merge({
'file_contents[gistfile1]' => content,
'file_ext[gistfile1]' => syntax_ext(syntax)
}).inject('') { |body, (key, value)| body << "#{key}=#{CGI.escape(value)}&" }.chop

params << '&private=on' if private_gist
params
end

def syntax_ext(syntax)
Expand Down
14 changes: 4 additions & 10 deletions test/cocoa_gist_test.rb
Expand Up @@ -39,18 +39,12 @@
end

it "should serialize the parameters" do
@gist.send(:params, 'the content', 'ruby').split('&').sort.should ==
%w{ file_contents[gistfile1]=the+content file_ext[gistfile1]=.rb login=alloy token=secret }.sort
end

it "should omit empty values from the serialized parameters" do
CocoaGist.stubs(:credentials).returns({})
@gist.send(:params, 'the content', 'html').split('&').sort.should ==
%w{ file_contents[gistfile1]=the+content file_ext[gistfile1]=.htm }.sort
@gist.send(:params, 'the content', 'ruby', true).split('&').sort.should ==
%w{ file_contents[gistfile1]=the+content file_ext[gistfile1]=.rb login=alloy token=secret private=on }.sort
end

it "should post the paste contents" do
@gist.expects(:params).with('the content', 'ruby').returns('the parameters')
@gist.expects(:params).with('the content', 'ruby', true).returns('the parameters')

request = mock('NSMutableURLRequest')
OSX::NSMutableURLRequest.expects(:requestWithURL_cachePolicy_timeoutInterval).with do |url, policy, timeout|
Expand All @@ -65,7 +59,7 @@
connection = mock('NSURLConnection')
OSX::NSURLConnection.any_instance.expects(:initWithRequest_delegate).with(request, @gist).returns(connection)

@gist.start('the content', 'ruby')
@gist.start('the content', 'ruby', true)
@gist.connection.should.be connection
end

Expand Down

0 comments on commit 0c35619

Please sign in to comment.