github_api 0.1.0.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,17 @@
1
+ # encoding: utf-8
2
+
3
+ class Hash
4
+ def extractable_options?
5
+ instance_of?(Hash)
6
+ end
7
+ end
8
+
9
+ class Array
10
+ def extract_options!
11
+ if last.is_a?(Hash) && last.extractable_options?
12
+ pop
13
+ else
14
+ {}
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ module Github
2
+ class Mime < API
3
+
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ module Github
2
+ class API
3
+ module Utils
4
+
5
+ private
6
+
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+
3
+ module Github
4
+ class Client < API
5
+
6
+ def gists(options = {})
7
+ @gists ||= Github::Gists.new(options)
8
+ end
9
+
10
+ def git_data(options)
11
+ @git_data ||= Github::GitData.new(options)
12
+ end
13
+
14
+ def issues(options = {})
15
+ @issues ||= Github::Issues.new(options)
16
+ end
17
+
18
+ def orgs(options = {})
19
+ @orgs ||= Github::Orgs.new(options)
20
+ end
21
+
22
+ def pull_requests(options = {})
23
+ @pull_requests ||= Github::PullRequests.new(options)
24
+ end
25
+
26
+ def repos(options = {})
27
+ @repos ||= Github::Repos.new(options)
28
+ end
29
+
30
+ def users(options = {})
31
+ @users ||= Github::Users.new(options)
32
+ end
33
+
34
+ end # Client
35
+ end # Github
@@ -0,0 +1,84 @@
1
+ # encoding: utf-8
2
+
3
+ module Github
4
+ module Configuration
5
+
6
+ VALID_OPTIONS_KEYS = [
7
+ :adapter,
8
+ :client_id,
9
+ :client_secret,
10
+ :oauth_token,
11
+ :endpoint,
12
+ :format,
13
+ :resource,
14
+ :user_agent,
15
+ :faraday_options,
16
+ :repo,
17
+ :user
18
+ ].freeze
19
+
20
+ # Other adapters are :typhoeus, :patron, :em_synchrony, :excon, :test
21
+ DEFAULT_ADAPTER = :net_http
22
+
23
+ # By default, don't set an application key
24
+ DEFAULT_CLIENT_ID = nil
25
+
26
+ # By default, don't set an application secret
27
+ DEFAULT_CLIENT_SECRET = nil
28
+
29
+ # By default, don't set a user oauth access token
30
+ DEFAULT_OAUTH_TOKEN = nil
31
+
32
+ # The endpoint used to connect to GitHub if none is set
33
+ DEFAULT_ENDPOINT = 'https://api.github.com/'.freeze
34
+
35
+ # The value sent in the http header for 'User-Agent' if none is set
36
+ DEFAULT_USER_AGENT = "Github Ruby Gem #{Github::VERSION::STRING}".freeze
37
+
38
+ DEFAULT_FORMAT = :json
39
+
40
+ # By default,
41
+ DEFAULT_RESOURCE = nil
42
+
43
+ DEFAULT_FARADAY_OPTIONS = {}
44
+
45
+ # By default, don't set user name
46
+ DEFAULT_USER = nil
47
+
48
+ # By default, don't set repository name
49
+ DEFAULT_REPO = nil
50
+
51
+ attr_accessor *VALID_OPTIONS_KEYS
52
+
53
+ # Convenience method to allow for global setting of configuration options
54
+ def configure
55
+ yield self
56
+ end
57
+
58
+ def self.extended(base)
59
+ base.set_defaults
60
+ end
61
+
62
+ def options
63
+ options = {}
64
+ VALID_OPTIONS_KEYS.each { |k| options[k] = send(k) }
65
+ options
66
+ end
67
+
68
+ def set_defaults
69
+ self.adapter = DEFAULT_ADAPTER
70
+ self.client_id = DEFAULT_CLIENT_ID
71
+ self.client_secret = DEFAULT_CLIENT_SECRET
72
+ self.oauth_token = DEFAULT_OAUTH_TOKEN
73
+ self.endpoint = DEFAULT_ENDPOINT
74
+ self.user_agent = DEFAULT_USER_AGENT
75
+ self.faraday_options = DEFAULT_FARADAY_OPTIONS
76
+ self.format = DEFAULT_FORMAT
77
+ self.resource = DEFAULT_RESOURCE
78
+ self.user = DEFAULT_USER
79
+ self.repo = DEFAULT_REPO
80
+ self
81
+ end
82
+
83
+ end # Configuration
84
+ end # Github
@@ -0,0 +1,91 @@
1
+ # encoding: utf-8
2
+
3
+ require 'faraday'
4
+ require 'github_api/response'
5
+ require 'github_api/response/mashify'
6
+ require 'github_api/response/jsonize'
7
+ require 'github_api/response/raise_error'
8
+ require 'github_api/request/oauth2'
9
+
10
+ module Github
11
+ module Connection
12
+
13
+ # Available resources
14
+ RESOURCES = {
15
+ :issue => 'vnd.github-issue.',
16
+ :issuecomment => 'vnd.github-issuecomment.',
17
+ :commitcomment => 'vnd.github-commitcomment',
18
+ :pull => 'vnd.github-pull.',
19
+ :pullcomment => 'vnd.github-pullcomment.',
20
+ :gistcomment => 'vnd.github-gistcomment.'
21
+ }
22
+
23
+ # Mime types used by resources
24
+ RESOURCE_MIME_TYPES = {
25
+ :raw => 'raw+json',
26
+ :text => 'text+json',
27
+ :html => 'html+json',
28
+ :full => 'html+full'
29
+ }
30
+
31
+ BLOB_MIME_TYPES = {
32
+ :raw => 'vnd.github-blob.raw',
33
+ :json => 'json'
34
+ }
35
+
36
+ def default_faraday_options()
37
+ {
38
+ :headers => {
39
+ 'Accept' => "application/#{resource}#{format}",
40
+ 'User-Agent' => user_agent
41
+ },
42
+ :ssl => { :verify => false },
43
+ :url => endpoint
44
+ }
45
+ end
46
+
47
+ # TODO Write mime format conversion
48
+
49
+ # Create cache hash and store connection there and then pass it to @connection
50
+ # add method to invalidate it if previous options are different from current
51
+
52
+ def clear_cache
53
+ @connection = nil
54
+ end
55
+
56
+ def caching?
57
+ !@connection.nil?
58
+ end
59
+
60
+ def connection(options = {})
61
+ merged_options = faraday_options.merge(default_faraday_options)
62
+
63
+ clear_cache unless options.empty?
64
+
65
+ @connection ||= begin
66
+ Faraday.new(merged_options) do |builder|
67
+
68
+ puts options.inspect
69
+
70
+ builder.use Faraday::Request::JSON
71
+ builder.use Faraday::Request::Multipart
72
+ builder.use Faraday::Request::UrlEncoded
73
+ builder.use Faraday::Response::Logger
74
+
75
+ builder.use Github::Request::OAuth2, oauth_token if oauth_token?
76
+
77
+ unless options[:raw]
78
+ builder.use Github::Response::Mashify
79
+ builder.use Github::Response::Jsonize
80
+ end
81
+
82
+ builder.use Github::Response::RaiseError
83
+ builder.adapter adapter
84
+ end
85
+ end
86
+
87
+ end
88
+
89
+ end # Connection
90
+ end # Github
91
+
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+
3
+ module Github
4
+ class Error < StandardError
5
+ attr_reader :response_message, :response_headers
6
+
7
+ def initialize(message, headers)
8
+ @response_message = message
9
+ super message
10
+ end
11
+
12
+ def inspect
13
+ %(#<#{self.class}>)
14
+ end
15
+ end # Error
16
+
17
+ # Raised when Github returns the HTTP status code 400
18
+ class BadRequest < Error; end
19
+
20
+ # Raised when Github returns the HTTP status code 401
21
+ class Unauthorised < Error; end
22
+
23
+ # Raised when Github returns the HTTP status code 403
24
+ class Forbidden < Error; end
25
+
26
+ # Raised when Github returns the HTTP status code 404
27
+ class ResourceNotFound < Error; end
28
+
29
+ # Raised when Github returns the HTTP status code 500
30
+ class InternalServerError < Error; end
31
+
32
+ # Raised when Github returns the HTTP status code 503
33
+ class ServiceUnavailable < Error; end
34
+
35
+ end # Github
@@ -0,0 +1,199 @@
1
+ # encoding: utf-8
2
+
3
+ module Github
4
+ class Gists < API
5
+ extend AutoloadHelper
6
+
7
+ autoload_all 'github_api/gists',
8
+ :Comments => 'comments'
9
+
10
+ include Github::Gists::Comments
11
+
12
+ REQUIRED_GIST_INPUTS = %w[ description public files content ]
13
+
14
+ # Creates new Gists API
15
+ def initialize(options = {})
16
+ super(options)
17
+ end
18
+
19
+ # List a user's gists.
20
+ #
21
+ # = Examples
22
+ # @github = Github.new :user => 'user-name'
23
+ # @github.gists.gists
24
+ #
25
+ # List the authenticated user’s gists or if called anonymously,
26
+ # this will returns all public gists
27
+ #
28
+ # = Examples
29
+ # @github = Github.new :oauth_token => '...'
30
+ # @github.gists.gists
31
+ #
32
+ def gists(user_name=nil, params={})
33
+ _update_user_repo_params(user_name)
34
+ _normalize_params_keys(params)
35
+
36
+ response = if user
37
+ get("/users/#{user}/gists", params)
38
+ elsif oauth_token
39
+ get("/gists", params)
40
+ else
41
+ get("/gists/public", params)
42
+ end
43
+ return response unless block_given?
44
+ response.each { |el| yield el }
45
+ end
46
+
47
+ # List the authenticated user's starred gists
48
+ #
49
+ # = Examples
50
+ # @github = Github.new :oauth_token => '...'
51
+ # @github.gists.starred
52
+ #
53
+ def starred(params={})
54
+ _normalize_params_keys(params)
55
+ get("/gists/starred", params)
56
+ end
57
+
58
+ # Get a single gist
59
+ #
60
+ # = Examples
61
+ # @github = Github.new :oauth_token => '...'
62
+ # @github.gists.get_gist 'gist-id'
63
+ #
64
+ def get_gist(gist_id, params={})
65
+ _normalize_params_keys(params)
66
+ get("/gists/#{gist_id}", params)
67
+ end
68
+
69
+ # Create a gist
70
+ #
71
+ # = Inputs
72
+ # <tt>:description</tt> - Optional string
73
+ # <tt>:public</tt> - Required boolean
74
+ # <tt>:files</tt> - Required hash - Files that make up this gist.
75
+ # The key of which should be a required string filename and
76
+ # the value another required hash with parameters:
77
+ # <tt>:content</tt> - Required string - File contents.
78
+ #
79
+ # = Examples
80
+ # @github = Github.new :oauth_token => '...'
81
+ # @github.gists.create_gist
82
+ # 'description' => 'the description for this gist',
83
+ # 'public' => true,
84
+ # 'files' => {
85
+ # 'file1.txt' => {
86
+ # 'content' => 'String file contents'
87
+ # }
88
+ # }
89
+ #
90
+ def create_gist(params={})
91
+ _normalize_params_keys(params)
92
+ _filter_params_keys(REQUIRED_GIST_INPUTS, params)
93
+
94
+ post("/gists", params)
95
+ end
96
+
97
+ # Edit a gist
98
+ #
99
+ # = Inputs
100
+ # <tt>:description</tt> - Optional string
101
+ # <tt>:files</tt> - Optional hash - Files that make up this gist.
102
+ # The key of which should be a optional string filename and
103
+ # the value another optional hash with parameters:
104
+ # <tt>:content</tt> - Updated string - Update file contents.
105
+ # <tt>:filename</tt> - Optional string - New name for this file.
106
+ #
107
+ # = Examples
108
+ # @github = Github.new :oauth_token => '...'
109
+ # @github.gists.edit_gist 'gist-id',
110
+ # 'description' => 'the description for this gist',
111
+ # 'files' => {
112
+ # 'file1.txt' => {
113
+ # 'content' => 'Updated file contents'
114
+ # },
115
+ # 'old_name.txt' => {
116
+ # 'filename' => 'new_name.txt',
117
+ # 'content' => 'modified contents'
118
+ # },
119
+ # 'new_file.txt' => {
120
+ # 'content' => 'a new file contents'
121
+ # },
122
+ # 'delete_the_file.txt' => nil
123
+ # }
124
+ #
125
+ def edit_gist(gist_id, params={})
126
+ _validate_presence_of(gist_id)
127
+ _normalize_params_keys(params)
128
+ _filter_params_keys(REQUIRED_GIST_INPUTS, params)
129
+
130
+ patch("/gists/#{gist_id}", params)
131
+ end
132
+
133
+ # Star a gist
134
+ #
135
+ # = Examples
136
+ # @github = Github.new
137
+ # @github.gists.star 'gist-id'
138
+ #
139
+ def star(gist_id, params={})
140
+ _validate_presence_of(gist_id)
141
+ _normalize_params_keys(params)
142
+
143
+ put("/gists/#{gist_id}/star", params)
144
+ end
145
+
146
+ # Unstar a gist
147
+ #
148
+ # = Examples
149
+ # @github = Github.new
150
+ # @github.gists.unstar 'gist-id'
151
+ #
152
+ def unstar_gist(gist_id, params={})
153
+ _validate_presence_of(gist_id)
154
+ _normalize_params_keys(params)
155
+
156
+ delete("/gists/#{gist_id}/star", params)
157
+ end
158
+
159
+ # Check if a gist is starred
160
+ #
161
+ # = Examples
162
+ # @github = Github.new
163
+ # @github.gists.unstar 'gist-id'
164
+ #
165
+ def starred?(gist_id, params={})
166
+ _validate_presence_of(gist_id)
167
+ _normalize_params_keys(params)
168
+
169
+ get("/gists/#{gist_id}/star", params)
170
+ end
171
+
172
+ # Fork a gist
173
+ #
174
+ # = Examples
175
+ # @github = Github.new
176
+ # @github.gists.fork 'gist-id'
177
+ #
178
+ def fork(gist_id, params={})
179
+ _validate_presence_of(gist_id)
180
+ _normalize_params_keys(params)
181
+
182
+ post("/gists/#{gist_id}/fork", params)
183
+ end
184
+
185
+ # Delete a gist
186
+ #
187
+ # = Examples
188
+ # @github = Github.new
189
+ # @github.gists.delete_gist 'gist-id'
190
+ #
191
+ def delete_gist(gist_id, params={})
192
+ _validate_presence_of(gist_id)
193
+ _normalize_params_keys(params)
194
+
195
+ delete("/gists/#{gist_id}", params)
196
+ end
197
+
198
+ end # Gists
199
+ end # Github