Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

make the user api slightly more robust

* Add an octokit client to the user model, https://github.com/pengwynn/octokit
* Add support for POST/PUT/DELETE through rest-client
  • Loading branch information...
commit 30bb0450dc7710e392ce32c75a8b6a63e49897f8 1 parent 17b0a41
@atmos authored
View
56 lib/warden-github/user.rb
@@ -1,4 +1,5 @@
require 'yajl'
+require 'octokit'
require 'rest-client'
module Warden
@@ -61,6 +62,58 @@ def team_member?(team_id)
false
end
+ # Send a V3 API PUT request to path and parses the response body
+ #
+ # path - the path on api.github.com to hit
+ # params - extra params for calling the api
+ #
+ def get(path, params)
+ github_request(path, params)
+ end
+
+ # Send a V3 API PUT request to path and parses the response body
+ #
+ # path - the path on api.github.com to hit
+ # params - extra params for calling the api
+ #
+ def post(path, params)
+ headers = {:Authorization => "token #{user.token}", :content_type => :json, :accept => :json}
+ res = RestClient.post("#{github_api_uri}/#{path}", params.to_json, headers)
+ Yajl.load(res)
+ end
+
+ # Send a V3 API PUT request to path and parses the response body
+ #
+ # path - the path on api.github.com to hit
+ # params - extra params for calling the api
+ #
+ def put(path, params)
+ headers = {:Authorization => "token #{user.token}", :content_type => :json, :accept => :json}
+ res = RestClient.put("#{github_api_uri}/#{path}", params.to_json, headers)
+ Yajl.load(res)
+ end
+
+ # Send a V3 API DELETE request to path and parses the response body
+ #
+ # path - the path on api.github.com to hit
+ # params - extra params for calling the api
+ #
+ def delete(path, params)
+ headers = {:Authorization => "token #{user.token}", :content_type => :json, :accept => :json}
+ res = RestClient.delete("#{github_api_uri}/#{path}", params.to_json, headers)
+ Yajl.load(res)
+ end
+
+ # Access the GitHub API from Octokit
+ #
+ # Octokit is a robust client library for the GitHub API
+ # https://github.com/pengwynn/octokit
+ #
+ # Returns a cached client object for easy use
+ def api
+ @api ||= Octokit::Client.new(:login => login, :oauth_token => token)
+ end
+
# Send a V3 API GET request to path and parse the response body
#
# path - the path on api.github.com to hit
@@ -91,7 +144,8 @@ def github_request(path, params = {})
# github_raw_request("/user/repos", {:page => 3})
# # => RestClient::Response
def github_raw_request(path, params = {})
- RestClient.get("#{github_api_uri}/#{path}", :params => params.merge({ :access_token => token }), :accept => :json)
+ headers = {:Authorization => "token #{token}", :accept => :json}
+ RestClient.get("#{github_api_uri}/#{path}", headers.merge(:params => params))
end
private
View
2  lib/warden-github/version.rb
@@ -1,5 +1,5 @@
module Warden
module Github
- VERSION = "0.5.1"
+ VERSION = "0.6.0"
end
end
View
14 spec/user_spec.rb
@@ -0,0 +1,14 @@
+require File.dirname(__FILE__) + '/spec_helper'
+
+describe "Warden::Github" do
+ let(:user) do
+ Warden::Github::Oauth::User.new({'login' => 'atmos'}, 'abcde')
+ end
+
+ it "knows the token" do
+ user.token.should eql('abcde')
+ end
+ it "can access the octokit object to make api calls" do
+ user.api.should_not be_nil
+ end
+end
View
1  warden-github.gemspec
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
s.add_dependency "json", "~>1.5"
s.add_dependency "warden", "~>1.0"
s.add_dependency "oauth2", "~>0.5.2"
+ s.add_dependency "octokit", "~>1.4.0"
s.add_dependency "rest-client", "~>1.6.1"
s.add_dependency "yajl-ruby", "~>1.1"
Please sign in to comment.
Something went wrong with that request. Please try again.