Class: Github::Connection
- Inherits:
-
Object
- Object
- Github::Connection
- Defined in:
- lib/hubdate/connection.rb
Instance Attribute Summary collapse
-
#pass ⇒ Object
readonly
Returns the value of attribute pass.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#user ⇒ Object
Allows us to call for the connections user later.
Instance Method Summary collapse
-
#authenticated? ⇒ Boolean
Check is user is authenticated.
- #delete ⇒ Object
- #edit ⇒ Object
-
#get(path, params = {}, creds = @creds, server = @server) ⇒ Object
Send HTTP Get request.
-
#initialize(args = {}) ⇒ Connection
constructor
Check for argument errors and format credentials.
- #post(path, params = {}, creds = @creds, server = @server) ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Connection
Check for argument errors and format credentials
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/hubdate/connection.rb', line 7 def initialize(args={}) raise ArgumentError.new("Github::Connection does not take any arguments of type #{args.class}.") unless args.is_a?(Hash) args.keys.each do |key| raise ArgumentError.new("Unknown option '#{key}'.") unless [:user, :pass, :token].include? key end if args.keys.include?(:user) || args.keys.include?(:pass) unless args.keys.include?(:user) && args.keys.include?(:pass) raise ArgumentError.new("When using basic authentication, both :user and :pass are required.") end end if args.keys.include?(:token) if args.keys.include?(:user) || args.keys.include?(:pass) raise ArgumentError.new("Both OAuth parameters and basic authenctication parameters have been previded.") end end @user = args[:user] @pass = args[:pass] @token = args[:token] @server = "api.github.com" if !@token.nil? @creds = {:token => @token} elsif !@user.nil? @creds = {:user => @user, :pass => @pass} elsif @token.nil? && @user.nil? @creds = {} end end |
Instance Attribute Details
#pass ⇒ Object (readonly)
Returns the value of attribute pass.
4 5 6 |
# File 'lib/hubdate/connection.rb', line 4 def pass @pass end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
4 5 6 |
# File 'lib/hubdate/connection.rb', line 4 def token @token end |
#user ⇒ Object
Allows us to call for the connections user later
3 4 5 |
# File 'lib/hubdate/connection.rb', line 3 def user @user end |
Instance Method Details
#authenticated? ⇒ Boolean
Check is user is authenticated
42 43 44 45 46 47 48 |
# File 'lib/hubdate/connection.rb', line 42 def authenticated? if (user && pass) || token true else false end end |
#delete ⇒ Object
78 79 |
# File 'lib/hubdate/connection.rb', line 78 def delete end |
#edit ⇒ Object
75 76 |
# File 'lib/hubdate/connection.rb', line 75 def edit end |
#get(path, params = {}, creds = @creds, server = @server) ⇒ Object
Send HTTP Get request
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/hubdate/connection.rb', line 51 def get(path, params = {}, creds = @creds, server = @server) path = linkPath(path, params) if params != {} path = linkPath(path, params, creds[:token]) if params != {} && creds.keys.include?(:token) http = Net::HTTP.new(server, 443) req = Net::HTTP::Get.new(path) http.use_ssl = true req.basic_auth creds[:user], creds[:pass] if creds.keys.include?(:user) response = http.request(req) return JSON.parse(response.body) end |
#post(path, params = {}, creds = @creds, server = @server) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/hubdate/connection.rb', line 63 def post(path, params={}, creds = @creds, server = @server) path = linkPath(path, params, creds[:token]) if params != {} && creds.keys.include?(:token) http = Net::HTTP.new(server, 443) req = Net::HTTP::Post.new(path) http.use_ssl = true req.body = params.to_json req.basic_auth creds[:user], creds[:pass] if creds.keys.include?(:user) response = http.request(req) return JSON.parse(response.body) end |