github_api 0.1.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. data/LICENSE.txt +20 -0
  2. data/README.rdoc +159 -0
  3. data/Rakefile +52 -0
  4. data/features/github.feature +9 -0
  5. data/features/step_definitions/github_steps.rb +0 -0
  6. data/features/support/env.rb +13 -0
  7. data/lib/github_api.rb +55 -0
  8. data/lib/github_api/api.rb +133 -0
  9. data/lib/github_api/api/extract_options.rb +17 -0
  10. data/lib/github_api/api/mime.rb +5 -0
  11. data/lib/github_api/api/utils.rb +9 -0
  12. data/lib/github_api/client.rb +35 -0
  13. data/lib/github_api/configuration.rb +84 -0
  14. data/lib/github_api/connection.rb +91 -0
  15. data/lib/github_api/error.rb +35 -0
  16. data/lib/github_api/gists.rb +199 -0
  17. data/lib/github_api/gists/comments.rb +74 -0
  18. data/lib/github_api/git_data.rb +26 -0
  19. data/lib/github_api/git_data/blobs.rb +9 -0
  20. data/lib/github_api/git_data/commits.rb +9 -0
  21. data/lib/github_api/git_data/references.rb +9 -0
  22. data/lib/github_api/git_data/tags.rb +9 -0
  23. data/lib/github_api/git_data/trees.rb +9 -0
  24. data/lib/github_api/issues.rb +201 -0
  25. data/lib/github_api/issues/comments.rb +98 -0
  26. data/lib/github_api/issues/events.rb +50 -0
  27. data/lib/github_api/issues/labels.rb +191 -0
  28. data/lib/github_api/issues/milestones.rb +119 -0
  29. data/lib/github_api/orgs.rb +90 -0
  30. data/lib/github_api/orgs/members.rb +109 -0
  31. data/lib/github_api/orgs/teams.rb +236 -0
  32. data/lib/github_api/pull_requests.rb +210 -0
  33. data/lib/github_api/pull_requests/comments.rb +134 -0
  34. data/lib/github_api/repos.rb +256 -0
  35. data/lib/github_api/repos/collaborators.rb +59 -0
  36. data/lib/github_api/repos/commits.rb +115 -0
  37. data/lib/github_api/repos/downloads.rb +77 -0
  38. data/lib/github_api/repos/forks.rb +29 -0
  39. data/lib/github_api/repos/hooks.rb +67 -0
  40. data/lib/github_api/repos/keys.rb +53 -0
  41. data/lib/github_api/repos/watching.rb +50 -0
  42. data/lib/github_api/request.rb +75 -0
  43. data/lib/github_api/request/oauth2.rb +33 -0
  44. data/lib/github_api/response.rb +10 -0
  45. data/lib/github_api/response/jsonize.rb +22 -0
  46. data/lib/github_api/response/mashify.rb +26 -0
  47. data/lib/github_api/response/raise_error.rb +33 -0
  48. data/lib/github_api/users.rb +82 -0
  49. data/lib/github_api/users/emails.rb +49 -0
  50. data/lib/github_api/users/followers.rb +98 -0
  51. data/lib/github_api/users/keys.rb +84 -0
  52. data/lib/github_api/version.rb +12 -0
  53. data/spec/fixtures/collaborators_list.json +6 -0
  54. data/spec/fixtures/commits_list.json +25 -0
  55. data/spec/fixtures/repos_branches_list.json +7 -0
  56. data/spec/fixtures/repos_list.json +27 -0
  57. data/spec/github/api_spec.rb +6 -0
  58. data/spec/github/client_spec.rb +6 -0
  59. data/spec/github/gists/comments_spec.rb +5 -0
  60. data/spec/github/gists_spec.rb +5 -0
  61. data/spec/github/git_data/blobs_spec.rb +5 -0
  62. data/spec/github/git_data/commits_spec.rb +5 -0
  63. data/spec/github/git_data/references_spec.rb +5 -0
  64. data/spec/github/git_data/tags_spec.rb +5 -0
  65. data/spec/github/git_data/trees_spec.rb +5 -0
  66. data/spec/github/git_data_spec.rb +5 -0
  67. data/spec/github/issues/comments_spec.rb +5 -0
  68. data/spec/github/issues/events_spec.rb +5 -0
  69. data/spec/github/issues/labels_spec.rb +5 -0
  70. data/spec/github/issues/milestones_spec.rb +5 -0
  71. data/spec/github/issues_spec.rb +5 -0
  72. data/spec/github/orgs/members_spec.rb +5 -0
  73. data/spec/github/orgs/teams_spec.rb +5 -0
  74. data/spec/github/orgs_spec.rb +5 -0
  75. data/spec/github/repos/collaborators_spec.rb +6 -0
  76. data/spec/github/repos/commits_spec.rb +5 -0
  77. data/spec/github/repos/downloads_spec.rb +5 -0
  78. data/spec/github/repos/forks_spec.rb +5 -0
  79. data/spec/github/repos/hooks_spec.rb +5 -0
  80. data/spec/github/repos/keys_spec.rb +5 -0
  81. data/spec/github/repos/watching_spec.rb +5 -0
  82. data/spec/github/repos_spec.rb +35 -0
  83. data/spec/github_spec.rb +5 -0
  84. data/spec/spec_helper.rb +15 -0
  85. metadata +284 -0
@@ -0,0 +1,33 @@
1
+ # encoding: utf-8
2
+
3
+ require 'faraday'
4
+
5
+ module Github
6
+ module Request
7
+ class OAuth2 < Faraday::Middleware
8
+ dependency 'oauth2'
9
+
10
+ def call(env)
11
+ puts "ENV: #{env.inspect}"
12
+ puts "TOKEN : #{@token}"
13
+ puts "APP: #{@app}"
14
+
15
+ # Extract parameters from the query
16
+ params = env[:url].query_values || {}
17
+
18
+ env[:url].query_values = { 'access_token' => @token }.merge(params)
19
+
20
+ token = env[:url].query_values['access_token']
21
+
22
+ env[:request_headers].merge!('Authorization' => "Token token=\"#{token}\"")
23
+
24
+ @app.call env
25
+ end
26
+
27
+ def initialize(app, *args)
28
+ @app = app
29
+ @token = args.shift
30
+ end
31
+ end
32
+ end
33
+ end # Github
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+
3
+ require 'faraday'
4
+
5
+ module Github
6
+ # Contains methods and attributes that act on the response returned from the
7
+ # request
8
+ class Response < Faraday::Response::Middleware
9
+ end # Response
10
+ end
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+
3
+ require 'faraday'
4
+
5
+ module Github
6
+ class Response::Jsonize < Faraday::Response::Middleware
7
+ dependency 'multi_json'
8
+
9
+ def parse(body)
10
+ case body
11
+ when ''
12
+ nil
13
+ when 'true'
14
+ true
15
+ when 'false'
16
+ false
17
+ else
18
+ ::MultiJson.decode(body)
19
+ end
20
+ end
21
+ end
22
+ end # Github
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+
3
+ require 'faraday'
4
+
5
+ module Github
6
+ class Response::Mashify < Faraday::Response::Middleware
7
+ dependency 'hashie/mash'
8
+
9
+ class << self
10
+ attr_accessor :mash_class
11
+ end
12
+
13
+ self.mash_class = ::Hashie::Mash
14
+
15
+ def parse(body)
16
+ case body
17
+ when Hash
18
+ self.class.mash_class.new(body)
19
+ when Array
20
+ body.map { |item| item.is_a?(Hash) ? self.class.mash_class.new(item) : item }
21
+ else
22
+ body
23
+ end
24
+ end
25
+ end
26
+ end # Github
@@ -0,0 +1,33 @@
1
+ # encoding: utf-8
2
+
3
+ require 'faraday'
4
+ require 'github_api/error'
5
+
6
+ module Github
7
+ class Response::RaiseError < Faraday::Response::Middleware
8
+
9
+ def on_complete(env)
10
+ case env[:status].to_i
11
+ when 400
12
+ raise Github::BadRequest.new(response_message(env), env[:response_headers])
13
+ when 401
14
+ raise Github::Unauthorised.new(response_message(env), env[:response_headers])
15
+ when 403
16
+ raise Github::Forbidden.new(response_message(env), env[:response_headers])
17
+ when 404
18
+ raise Github::ResourceNotFound.new(response_message(env), env[:response_headers])
19
+ when 500
20
+ raise Github::InternalServerError.new(response_message(env), env[:response_headers])
21
+ when 503
22
+ raise Github::ServiceUnavailable.new(response_message(env), env[:response_headers])
23
+ when 400...600
24
+ raise Github::Error.new(response_message(env), env[:response_headers])
25
+ end
26
+ end
27
+
28
+ def response_message(env)
29
+ "#{env[:method].to_s.upcase} #{env[:url].to_s}: #{env[:status]}#{env[:body]}"
30
+ end
31
+
32
+ end # Response::RaiseError
33
+ end # Github
@@ -0,0 +1,82 @@
1
+ # encoding: utf-8
2
+
3
+ module Github
4
+ class Users < API
5
+ extend AutoloadHelper
6
+
7
+ # Load all the modules after initializing Repos to avoid superclass mismatch
8
+ autoload_all 'github_api/users',
9
+ :Emails => 'emails',
10
+ :Followers => 'followers',
11
+ :Keys => 'keys'
12
+
13
+ include Github::Users::Emails
14
+ include Github::Users::Followers
15
+ include Github::Users::Keys
16
+
17
+ VALID_USER_PARAMS_NAMES = %w[
18
+ name
19
+ email
20
+ blog
21
+ company
22
+ location
23
+ hireable
24
+ bio
25
+ ].freeze
26
+
27
+ # Creates new Repos API
28
+ def initialize(options = {})
29
+ super(options)
30
+ end
31
+
32
+ # Get a single unauthenticated user
33
+ #
34
+ # = Examples
35
+ # @github = Github.new
36
+ # @github.users.get_user 'user-name'
37
+ #
38
+ # Get the authenticated user
39
+ #
40
+ # = Examples
41
+ # @github = Github.new :oauth_token => '...'
42
+ # @github.users.get_user
43
+ #
44
+ def get_user(user_name=nil, params={})
45
+ _normalize_params_keys(params)
46
+ if user_name
47
+ get("/users/#{user}", params)
48
+ else
49
+ get("/user", params)
50
+ end
51
+ end
52
+
53
+ # Update the authenticated user
54
+ #
55
+ # = Inputs
56
+ # * <tt>:name</tt> - Optional string
57
+ # * <tt>:email</tt> - Optional string - publically visible email address
58
+ # * <tt>:blog</tt> - Optional string
59
+ # * <tt>:company</tt> - Optional string
60
+ # * <tt>:location</tt> - Optional string
61
+ # * <tt>:hireable</tt> - Optional boolean
62
+ # * <tt>:bio</tt> - Optional string
63
+ #
64
+ # = Examples
65
+ # @github = Github.new :oauth_token => '..'
66
+ # @github.users.update_user
67
+ # "name" => "monalisa octocat",
68
+ # "email" => "octocat@github.com",
69
+ # "blog" => "https://github.com/blog",
70
+ # "company" => "GitHub",
71
+ # "location" => "San Francisco",
72
+ # "hireable" => true,
73
+ # "bio" => "There once..."
74
+ #
75
+ def update_user(params={})
76
+ _normalize_params_keys(params)
77
+ _filter_params_keys(VALID_USER_PARAMS_NAMES, params)
78
+ patch("/user", params)
79
+ end
80
+
81
+ end # Users
82
+ end # Github
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ module Github
4
+ class Users
5
+ module Emails
6
+
7
+ # List email addresses for the authenticated user
8
+ #
9
+ # = Examples
10
+ # @github = Github.new :oauth_token => '...'
11
+ # @github.users.emails
12
+ #
13
+ def emails(params={})
14
+ get("/user/emails", params)
15
+ end
16
+
17
+ # Add email address(es) for the authenticated user
18
+ #
19
+ # = Inputs
20
+ # You can include a single email address or an array of addresses
21
+ #
22
+ # = Examples
23
+ # @github = Github.new :oauth_token => '...'
24
+ # @github.users.add_email "octocat@github.com", "support@github.com"
25
+ #
26
+ def add_email(*args)
27
+ params = _extract_parameters(args)
28
+ params['data'] = [args].flatten
29
+ post("/user/emails", params)
30
+ end
31
+
32
+ # Delete email address(es) for the authenticated user
33
+ #
34
+ # = Inputs
35
+ # You can include a single email address or an array of addresses
36
+ #
37
+ # = Examples
38
+ # @github = Github.new :oauth_token => '...'
39
+ # @github.users.delete_email "octocat@github.com", "support@github.com"
40
+ #
41
+ def add_email(*args)
42
+ params = _extract_parameters(args)
43
+ params['data'] = [args].flatten
44
+ delete("/user/emails", params)
45
+ end
46
+
47
+ end # Emails
48
+ end # Users
49
+ end # Github
@@ -0,0 +1,98 @@
1
+ # encoding: utf-8
2
+
3
+ module Github
4
+ class Users
5
+ module Followers
6
+
7
+ # List a user's followers
8
+ #
9
+ # = Examples
10
+ # @github = Github.new
11
+ # @github.users.followers 'user-name'
12
+ # @github.users.followers 'user-name' { |user| ... }
13
+ #
14
+ # List the authenticated user's followers
15
+ #
16
+ # = Examples
17
+ # @github = Github.new :oauth_token => '...'
18
+ # @github.users.following
19
+ # @github.users.following { |user| ... }
20
+ #
21
+ def followers(user_name=nil, params={})
22
+ _normalize_params_keys(params)
23
+ response = if user_name
24
+ get("/users/#{user_name}/followers", params)
25
+ else
26
+ get("/user/followers", params)
27
+ end
28
+ return response unless block_given?
29
+ response.each { |el| yield el }
30
+ end
31
+
32
+ # List who a user is following
33
+ #
34
+ # = Examples
35
+ # @github = Github.new
36
+ # @github.users.following 'user-name'
37
+ # @github.users.following 'user-name' { |user| ... }
38
+ #
39
+ # List who the authenicated user is following
40
+ #
41
+ # = Examples
42
+ #
43
+ # @github = Github.new :oauth_token => '...'
44
+ # @github.users.following
45
+ #
46
+ def following(user_name=nil, params={})
47
+ _normalize_params_keys(params)
48
+ response = if user_name
49
+ get("/users/#{user_name}/following", params)
50
+ else
51
+ get("/user/following", params)
52
+ end
53
+ return response unless block_given?
54
+ response.each { |el| yield el }
55
+ end
56
+
57
+ # Check if you are following a user
58
+ #
59
+ # = Examples
60
+ # @github = Github.new :oauth_token => '...'
61
+ # @github.users.following? 'user-name'
62
+ #
63
+ def following?(user_name, params={})
64
+ _validate_presence_of user_name
65
+ _normalize_params_keys(params)
66
+ get("/user/following/#{user_name}", params)
67
+ true
68
+ rescue Github::ResourceNotFound
69
+ false
70
+ end
71
+
72
+ # Follow a user
73
+ #
74
+ # = Examples
75
+ # @github = Github.new :oauth_token => '...'
76
+ # @github.users.follow 'user-name'
77
+ #
78
+ def follow(user_name, params={})
79
+ _validate_presence_of user_name
80
+ _normalize_params_keys(params)
81
+ put("/user/following/#{user_name}", params)
82
+ end
83
+
84
+ # Unfollow a user
85
+ #
86
+ # = Examples
87
+ # @github = Github.new :oauth_token => '...'
88
+ # @github.users.unfollow 'user-name'
89
+ #
90
+ def unfollow(user_name, params={})
91
+ _validate_presence_of user_name
92
+ _normalize_params_keys(params)
93
+ delete("/user/following/#{user_name}", params)
94
+ end
95
+
96
+ end # Followers
97
+ end # Users
98
+ end # Github
@@ -0,0 +1,84 @@
1
+ # encoding: utf-8
2
+
3
+ module Github
4
+ class Users
5
+ module Keys
6
+
7
+ VALID_KEY_PARAM_NAMES = %w[ title key ].freeze
8
+
9
+ # List public keys for the authenticated user
10
+ #
11
+ # = Examples
12
+ # @github = Github.new :oauth_token => '...'
13
+ # @github.users.public_keys
14
+ # @github.users.public_keys { |key| ... }
15
+ #
16
+ def public_keys(params={})
17
+ _normalize_params_keys(params)
18
+ response = get("/user/keys", params)
19
+ return response unless block_given?
20
+ response.each { |el| yield el }
21
+ end
22
+
23
+ # Get a single pulic key for the authenticated user
24
+ #
25
+ # = Examples
26
+ # @github = Github.new :oauth_token => '...'
27
+ # @github.users.public_key 'key-id'
28
+ #
29
+ def public_key(key_id, params={})
30
+ _validate_presence_of key_id
31
+ _normalize_params_keys(params)
32
+ get("/user/keys/#{key_id}", params)
33
+ end
34
+
35
+ # Create a public key for the authenticated user
36
+ #
37
+ # = Inputs
38
+ # * <tt>:title</tt> - Required string
39
+ # * <tt>:key</tt> - Required string. sha key
40
+ #
41
+ # = Examples
42
+ # @github = Github.new :oauth_token => '...'
43
+ # @github.users.create_key "title" => "octocat@octomac",
44
+ # "key" => "ssh-rsa AAA..."
45
+ #
46
+ def create_key(params={})
47
+ _normalize_params_keys(params)
48
+ _filter_params_keys(VALID_KEY_PARAM_NAMES, params)
49
+ post("/user/keys", params)
50
+ end
51
+
52
+ # Update a public key for the authenticated user
53
+ #
54
+ # = Inputs
55
+ # * <tt>:title</tt> - Required string
56
+ # * <tt>:key</tt> - Required string. sha key
57
+ #
58
+ # = Examples
59
+ # @github = Github.new :oauth_token => '...'
60
+ # @github.users.update_key 'key-id', "title" => "octocat@octomac",
61
+ # "key" => "ssh-rsa AAA..."
62
+ #
63
+ def update_key(key_id, params={})
64
+ _validate_presence_of key_id
65
+ _normalize_params_keys(params)
66
+ _filter_params_keys(VALID_KEY_PARAM_NAMES, params)
67
+ patch("/user/keys/#{key_id}", params)
68
+ end
69
+
70
+ # Delete a public key for the authenticated user
71
+ #
72
+ # = Examples
73
+ # @github = Github.new :oauth_token => '...'
74
+ # @github.users.delete_key 'key-id'
75
+ #
76
+ def delete_key(key_id, params={})
77
+ _validate_presence_of key_id
78
+ _normalize_params_keys(params)
79
+ delete("/user/keys/#{key_id}", params)
80
+ end
81
+
82
+ end # Keys
83
+ end # Users
84
+ end # Github