github_api 0.6.1 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -75,6 +75,7 @@ module Github
75
75
  :Users => 'users',
76
76
  :Events => 'events',
77
77
  :Search => 'search',
78
+ :Markdown => 'markdown',
78
79
  :CoreExt => 'core_ext',
79
80
  :MimeType => 'mime_type',
80
81
  :Authorization => 'authorization',
@@ -24,6 +24,7 @@ module Github
24
24
  include Normalizer
25
25
 
26
26
  attr_reader *Configuration::VALID_OPTIONS_KEYS
27
+
27
28
  attr_accessor *VALID_API_KEYS
28
29
 
29
30
  # Callback to update global configuration options
@@ -37,8 +38,10 @@ module Github
37
38
  end
38
39
 
39
40
  # Creates new API
40
- def initialize(options = {}, &block)
41
+ def initialize(options={}, &block)
42
+ super()
41
43
  options = Github.options.merge(options)
44
+
42
45
  Configuration::VALID_OPTIONS_KEYS.each do |key|
43
46
  send("#{key}=", options[key])
44
47
  end
@@ -93,6 +96,7 @@ module Github
93
96
  { 'user' => self.user, 'repo' => self.repo }.merge!(params)
94
97
  end
95
98
 
99
+ # TODO: See whether still needed, consider adding to core_exts
96
100
  def _hash_traverse(hash, &block)
97
101
  hash.each do |key, val|
98
102
  block.call(key)
@@ -3,6 +3,12 @@
3
3
  module Github
4
4
  class Client < API
5
5
 
6
+ # This is a read-only API to the GitHub events.
7
+ # These events power the various activity streams on the site.
8
+ def events(options = {})
9
+ @events ||= ApiFactory.new 'Events', options
10
+ end
11
+
6
12
  def gists(options = {})
7
13
  @gists ||= ApiFactory.new 'Gists', options
8
14
  end
@@ -19,6 +25,17 @@ module Github
19
25
  @issues ||= ApiFactory.new 'Issues', options
20
26
  end
21
27
 
28
+ def markdown(options = {})
29
+ @markdown ||= ApiFactory.new 'Markdown', options
30
+ end
31
+
32
+ # An API for users to manage their own tokens. You can only access your own
33
+ # tokens, and only through Basic Authentication.
34
+ def oauth(options = {})
35
+ @oauth ||= ApiFactory.new 'Authorizations', options
36
+ end
37
+ alias :authorizations :oauth
38
+
22
39
  def orgs(options = {})
23
40
  @orgs ||= ApiFactory.new 'Orgs', options
24
41
  end
@@ -33,28 +50,15 @@ module Github
33
50
  end
34
51
  alias :repositories :repos
35
52
 
36
- # Many of the resources on the users API provide a shortcut for getting
37
- # information about the currently authenticated user.
38
- def users(options = {})
39
- @users ||= ApiFactory.new 'Users', options
40
- end
41
-
42
- # This is a read-only API to the GitHub events.
43
- # These events power the various activity streams on the site.
44
- def events(options = {})
45
- @events ||= ApiFactory.new 'Events', options
46
- end
47
-
48
53
  def search(options = {})
49
54
  @search ||= ApiFactory.new 'Search', options
50
55
  end
51
56
 
52
- # An API for users to manage their own tokens. You can only access your own
53
- # tokens, and only through Basic Authentication.
54
- def oauth(options = {})
55
- @oauth ||= ApiFactory.new 'Authorizations', options
57
+ # Many of the resources on the users API provide a shortcut for getting
58
+ # information about the currently authenticated user.
59
+ def users(options = {})
60
+ @users ||= ApiFactory.new 'Users', options
56
61
  end
57
- alias :authorizations :oauth
58
62
 
59
63
  end # Client
60
64
  end # Github
@@ -12,29 +12,34 @@ require 'github_api/request/jsonize'
12
12
 
13
13
  module Github
14
14
  module Connection
15
+ extend self
16
+ include Github::Constants
15
17
 
16
- ALLOWED_OPTIONS = [
17
- :headers,
18
- :url,
19
- :params,
20
- :request,
21
- :ssl
22
- ].freeze
23
-
24
- private
18
+ ALLOWED_OPTIONS = [
19
+ :headers,
20
+ :url,
21
+ :params,
22
+ :request,
23
+ :ssl
24
+ ].freeze
25
25
 
26
26
  def default_options(options={}) # :nodoc:
27
27
  {
28
28
  :headers => {
29
- :accept => '*/*', #accepts,
30
- :user_agent => user_agent,
31
- :content_type => 'application/x-www-form-urlencoded'
29
+ ACCEPT => "application/vnd.github.v3.raw+json," \
30
+ "application/vnd.github.beta.raw+json;q=0.5," \
31
+ "application/json;q=0.1",
32
+ ACCEPT_CHARSET => "utf-8",
33
+ USER_AGENT => user_agent,
34
+ CONTENT_TYPE => 'application/x-www-form-urlencoded'
32
35
  },
33
36
  :ssl => { :verify => false },
34
- :url => endpoint
35
- }
37
+ :url => options.fetch(:endpoint) { Github.endpoint }
38
+ }.merge(options)
36
39
  end
37
40
 
41
+ @connection = nil
42
+
38
43
  def clear_cache # :nodoc:
39
44
  @connection = nil
40
45
  end
@@ -45,12 +50,12 @@ module Github
45
50
 
46
51
  def connection(options = {}) # :nodoc:
47
52
 
48
- merged_options = filter! ALLOWED_OPTIONS, default_options.merge(options)
53
+ conn_options = default_options(options)
49
54
  clear_cache unless options.empty?
50
55
 
51
56
  @connection ||= begin
52
- Faraday.new(merged_options.merge(connection_options)) do |builder|
53
- puts options.inspect if ENV['DEBUG']
57
+ Faraday.new(conn_options) do |builder|
58
+ puts "OPTIONS:#{conn_options.inspect}" if ENV['DEBUG']
54
59
 
55
60
  builder.use Github::Request::Jsonize
56
61
  builder.use Faraday::Request::Multipart
@@ -21,6 +21,12 @@ module Github
21
21
 
22
22
  LOCATION = 'Location'.freeze
23
23
 
24
+ USER_AGENT = 'User-Agent'.freeze
25
+
26
+ ACCEPT = 'Accept'.freeze
27
+
28
+ ACCEPT_CHARSET = 'Accept-Charset'.freeze
29
+
24
30
  # Link headers
25
31
  HEADER_LINK = "Link".freeze
26
32
 
@@ -0,0 +1,64 @@
1
+ # encoding: utf-8
2
+
3
+ module Github
4
+ class Markdown < API
5
+
6
+ # Creates new Markdown API
7
+ def initialize(options = {})
8
+ super(options)
9
+ end
10
+
11
+ # Render an arbritrary Markdown document
12
+ #
13
+ # = Parameters
14
+ # <tt>:text</tt> - Required string - The Markdown text to render
15
+ # <tt>:mode<tt> - Optional string - The rendering mode
16
+ # * <tt>markdown</tt> to render a document as plain Markdown, just
17
+ # like README files are rendered.
18
+ # * <tt>gfm</tt> to render a document as user-content, e.g. like user
19
+ # comments or issues are rendered. In GFM mode, hard line breaks are
20
+ # always taken into account, and issue and user mentions are
21
+ # linked accordingly.
22
+ # <tt>:context<tt> - Optional string - The repository context, only taken
23
+ # into account when rendering as <tt>gfm</tt>
24
+ #
25
+ # = Examples
26
+ # github = Github.new
27
+ # github.markdown.render
28
+ # "text": "Hello world github/linguist#1 **cool**, and #1!",
29
+ # "mode": "gfm",
30
+ # "context": "github/gollum"
31
+ #
32
+ def render(*args)
33
+ params = args.extract_options!
34
+ normalize! params
35
+
36
+ assert_required_keys ['text'], params
37
+ post_request("markdown", params, :raw => true)
38
+ end
39
+
40
+
41
+ # Render a Markdown document in raw mode
42
+ #
43
+ # = Input
44
+ # The raw API it not JSON-based. It takes a Markdown document as plaintext
45
+ # <tt>text/plain</tt> or <tt>text/x-markdown</tt> and renders it as plain
46
+ # Markdown without a repository context (just like a README.md file is
47
+ # rendered – this is the simplest way to preview a readme online)
48
+ #
49
+ # = Examples
50
+ # github = Github.new
51
+ # github.markdown.render_raw "Hello github/linguist#1 **cool**, and #1!",
52
+ # "mime": "text/plain",
53
+ #
54
+ def render_raw(*args)
55
+ params = args.extract_options!
56
+ normalize! params
57
+ mime_type, params['data'] = params['mime'], args.shift
58
+
59
+ post_request("markdown/raw", params, :raw => true,
60
+ :headers => {'Content-Type' => mime_type || 'text/plain'})
61
+ end
62
+
63
+ end # Markdown
64
+ end # Github
@@ -108,6 +108,19 @@ module Github
108
108
  end
109
109
  alias :list_branches :branches
110
110
 
111
+ # Get branch
112
+ #
113
+ # = Examples
114
+ #
115
+ # github = Github.new
116
+ # github.repos.branch 'user-name', 'repo-name', 'branch-name'
117
+ #
118
+ def branch(user_name, repo_name, branch, params={})
119
+ normalize! params
120
+
121
+ get_request("repos/#{user_name}/#{repo_name}/branches/#{branch}", params)
122
+ end
123
+
111
124
  # Create a new repository for the autheticated user.
112
125
  #
113
126
  # = Parameters
@@ -22,6 +22,9 @@ module Github
22
22
  #
23
23
  # This method returns the contents of any file or directory in a repository.
24
24
  #
25
+ # = Parameters
26
+ # * <tt>:ref</tt> - Optional string - valid Git reference, defaults to master
27
+ #
25
28
  # = Examples
26
29
  # github = Github.new
27
30
  # github.repos.contents.get 'user-name', 'repo-name', 'path'
@@ -31,18 +31,21 @@ module Github
31
31
  if !METHODS.include?(method)
32
32
  raise ArgumentError, "unkown http method: #{method}"
33
33
  end
34
- _extract_mime_type(params, options)
34
+ # _extract_mime_type(params, options)
35
35
 
36
36
  puts "EXECUTED: #{method} - #{path} with #{params} and #{options}" if ENV['DEBUG']
37
37
 
38
- response = connection(options).send(method) do |request|
38
+ conn = connection(options)
39
+ path = (conn.path_prefix + path).gsub(/\/\//,'/') if conn.path_prefix != '/'
40
+
41
+ response = conn.send(method) do |request|
39
42
  case method.to_sym
40
43
  when *(METHODS - METHODS_WITH_BODIES)
41
44
  request.body = params.delete('data') if params.has_key?('data')
42
45
  request.url(path, params)
43
46
  when *METHODS_WITH_BODIES
44
47
  request.path = path
45
- request.body = _process_params(params) unless params.empty?
48
+ request.body = extract_data_from_params(params) unless params.empty?
46
49
  end
47
50
  end
48
51
  response.body
@@ -50,7 +53,7 @@ module Github
50
53
 
51
54
  private
52
55
 
53
- def _process_params(params) # :nodoc:
56
+ def extract_data_from_params(params) # :nodoc:
54
57
  return params['data'] if params.has_key?('data') and !params['data'].nil?
55
58
  return params
56
59
  end
@@ -4,7 +4,7 @@ module Github
4
4
  module VERSION
5
5
  MAJOR = 0
6
6
  MINOR = 6
7
- PATCH = 1
7
+ PATCH = 2
8
8
  BUILD = nil
9
9
 
10
10
  STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.');
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "master",
3
+ "commit": {
4
+ "sha": "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
5
+ "commit": {
6
+ "author": {
7
+ "name": "The Octocat",
8
+ "date": "2012-03-06T15:06:50-08:00",
9
+ "email": "octocat@nowhere.com"
10
+ },
11
+ "url": "https://api.github.com/repos/octocat/Hello-World/git/commits/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
12
+ "message": "Merge pull request #6 from Spaceghost/patch-1\n\nNew line at end of file.",
13
+ "tree": {
14
+ "sha": "b4eecafa9be2f2006ce1b709d6857b07069b4608",
15
+ "url": "https://api.github.com/repos/octocat/Hello-World/git/trees/b4eecafa9be2f2006ce1b709d6857b07069b4608"
16
+ },
17
+ "committer": {
18
+ "name": "The Octocat",
19
+ "date": "2012-03-06T15:06:50-08:00",
20
+ "email": "octocat@nowhere.com"
21
+ }
22
+ },
23
+ "author": {
24
+ "gravatar_id": "7ad39074b0584bc555d0417ae3e7d974",
25
+ "avatar_url": "https://secure.gravatar.com/avatar/7ad39074b0584bc555d0417ae3e7d974?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png",
26
+ "url": "https://api.github.com/users/octocat",
27
+ "id": 583231,
28
+ "login": "octocat"
29
+ },
30
+ "parents": [
31
+ {
32
+ "sha": "553c2077f0edc3d5dc5d17262f6aa498e69d6f8e",
33
+ "url": "https://api.github.com/repos/octocat/Hello-World/commits/553c2077f0edc3d5dc5d17262f6aa498e69d6f8e"
34
+ },
35
+ {
36
+ "sha": "762941318ee16e59dabbacb1b4049eec22f0d303",
37
+ "url": "https://api.github.com/repos/octocat/Hello-World/commits/762941318ee16e59dabbacb1b4049eec22f0d303"
38
+ }
39
+ ],
40
+ "url": "https://api.github.com/repos/octocat/Hello-World/commits/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
41
+ "committer": {
42
+ "gravatar_id": "7ad39074b0584bc555d0417ae3e7d974",
43
+ "avatar_url": "https://secure.gravatar.com/avatar/7ad39074b0584bc555d0417ae3e7d974?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png",
44
+ "url": "https://api.github.com/users/octocat",
45
+ "id": 583231,
46
+ "login": "octocat"
47
+ }
48
+ },
49
+ "_links": {
50
+ "html": "https://github.com/octocat/Hello-World/tree/master",
51
+ "self": "https://api.github.com/repos/octocat/Hello-World/branches/master"
52
+ }
53
+ }
@@ -121,7 +121,7 @@ describe Github::Gists::Comments do
121
121
  context "resouce created" do
122
122
  before do
123
123
  stub_post("/gists/#{gist_id}/comments").
124
- with(:body => JSON.generate(inputs.except('unrelated'))).
124
+ with(inputs.except('unrelated')).
125
125
  to_return(:body => fixture('gists/comment.json'),
126
126
  :status => 201,
127
127
  :headers => {:content_type => "application/json; charset=utf-8"})
@@ -173,7 +173,7 @@ describe Github::Gists::Comments do
173
173
  context "resouce edited" do
174
174
  before do
175
175
  stub_patch("/gists/comments/#{comment_id}").
176
- with(:body => JSON.generate(inputs.except('unrelated'))).
176
+ with(inputs.except('unrelated')).
177
177
  to_return(:body => fixture('gists/comment.json'),
178
178
  :status => 201,
179
179
  :headers => {:content_type => "application/json; charset=utf-8"})
@@ -200,10 +200,10 @@ describe Github::Gists do
200
200
  context "resouce created" do
201
201
  before do
202
202
  stub_post("/gists").
203
- with(:body => JSON.generate(inputs)).
203
+ with(inputs).
204
204
  to_return(:body => fixture('gists/gist.json'),
205
- :status => 201,
206
- :headers => {:content_type => "application/json; charset=utf-8"})
205
+ :status => 201,
206
+ :headers => {:content_type => "application/json; charset=utf-8"})
207
207
  end
208
208
 
209
209
  it "should fail to create resource if 'files' input is missing" do
@@ -280,10 +280,10 @@ describe Github::Gists do
280
280
  context "resouce edited" do
281
281
  before do
282
282
  stub_patch("/gists/#{gist_id}").
283
- with(:body => JSON.generate(inputs)).
283
+ with(inputs).
284
284
  to_return(:body => fixture('gists/gist.json'),
285
- :status => 200,
286
- :headers => {:content_type => "application/json; charset=utf-8"})
285
+ :status => 200,
286
+ :headers => {:content_type => "application/json; charset=utf-8"})
287
287
  end
288
288
 
289
289
  it "should edit resource successfully" do
@@ -73,7 +73,7 @@ describe Github::GitData::Blobs do
73
73
  context "resouce created" do
74
74
  before do
75
75
  stub_post("/repos/#{user}/#{repo}/git/blobs").
76
- with(:body => JSON.generate(inputs.except('unrelated'))).
76
+ with(inputs.except('unrelated')).
77
77
  to_return(:body => fixture('git_data/blob_sha.json'),
78
78
  :status => 201,
79
79
  :headers => {:content_type => "application/json; charset=utf-8"})
@@ -79,7 +79,7 @@ describe Github::GitData::Commits, :type => :base do
79
79
  context "resouce created" do
80
80
  before do
81
81
  stub_post("/repos/#{user}/#{repo}/git/commits").
82
- with(:body => JSON.generate(inputs.except('unrelated'))).
82
+ with(inputs.except('unrelated')).
83
83
  to_return(:body => fixture('git_data/commit.json'), :status => 201, :headers => {:content_type => "application/json; charset=utf-8"})
84
84
  end
85
85
 
@@ -154,7 +154,7 @@ describe Github::GitData::References do
154
154
  context "resouce created" do
155
155
  before do
156
156
  stub_post("/repos/#{user}/#{repo}/git/refs").
157
- with(:body => JSON.generate(inputs.except('unrelated'))).
157
+ with(inputs.except('unrelated')).
158
158
  to_return(:body => fixture('git_data/reference.json'), :status => 201,
159
159
  :headers => {:content_type => "application/json; charset=utf-8"})
160
160
  end
@@ -220,7 +220,7 @@ describe Github::GitData::References do
220
220
  context "resouce updated" do
221
221
  before do
222
222
  stub_patch("/repos/#{user}/#{repo}/git/refs/#{ref}").
223
- with(:body => JSON.generate(inputs.except('unrelated'))).
223
+ with(inputs.except('unrelated')).
224
224
  to_return(:body => fixture('git_data/reference.json'), :status => 201, :headers => {:content_type => "application/json; charset=utf-8"})
225
225
  end
226
226