github_api 0.3.7 → 0.3.8

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.
@@ -119,7 +119,24 @@ module Github
119
119
  end
120
120
 
121
121
  def _filter_params_keys(keys, params) # :nodoc:
122
- params.reject! { |k,v| !keys.include? k }
122
+ # params.reject! { |k,v| !keys.include? k }
123
+ case params
124
+ when Hash
125
+ params.keys.each do |k, v|
126
+ unless keys.include? k
127
+ params.delete(k)
128
+ else
129
+ _filter_params_keys(keys, params[k])
130
+ end
131
+ end
132
+ when Array
133
+ params.map! do |el|
134
+ _filter_params_keys(keys, el)
135
+ end
136
+ else
137
+ params
138
+ end
139
+ return params
123
140
  end
124
141
 
125
142
  def _hash_traverse(hash, &block)
@@ -19,10 +19,11 @@ module Github
19
19
  _update_user_repo_params(user_name, repo_name)
20
20
  _validate_user_repo_params(user, repo) unless user? && repo?
21
21
  _validate_presence_of sha
22
- _normalize_params_keys(params)
22
+ _normalize_params_keys params
23
23
 
24
24
  get("/repos/#{user}/#{repo}/git/blobs/#{sha}", params)
25
25
  end
26
+ alias :get_blob :blob
26
27
 
27
28
  # Create a blob
28
29
  #
@@ -35,9 +36,10 @@ module Github
35
36
  # "content" => "Content of the blob",
36
37
  # "encoding" => "utf-8"
37
38
  #
38
- def create_blob(user_name, repo_name=nil, params={})
39
+ def create_blob(user_name, repo_name, params={})
39
40
  _update_user_repo_params(user_name, repo_name)
40
41
  _validate_user_repo_params(user, repo) unless user? && repo?
42
+
41
43
  _normalize_params_keys(params)
42
44
  _filter_params_keys(VALID_BLOB_PARAM_NAMES, params)
43
45
 
@@ -12,6 +12,7 @@ module Github
12
12
  type
13
13
  sha
14
14
  content
15
+ url
15
16
  ].freeze
16
17
 
17
18
  VALID_TREE_PARAM_VALUES = {
@@ -28,6 +29,12 @@ module Github
28
29
  # file.path
29
30
  # end
30
31
  #
32
+ # Get a tree recursively
33
+ #
34
+ # = Examples
35
+ # @github = Github.new
36
+ # @github.git_data.tree 'user-name', 'repo-name', 'sha', 'recursive' => true
37
+ #
31
38
  def tree(user_name, repo_name, sha, params={})
32
39
  _update_user_repo_params(user_name, repo_name)
33
40
  _validate_user_repo_params(user, repo) unless user? && repo?
@@ -43,6 +50,7 @@ module Github
43
50
  return response unless block_given?
44
51
  response.tree.each { |el| yield el }
45
52
  end
53
+ alias :get_tree :tree
46
54
 
47
55
  # Create a tree
48
56
  #
@@ -73,11 +81,13 @@ module Github
73
81
  # ...
74
82
  # ]
75
83
  #
76
- def create_tree(user_name=nil, repo_name=nil, params={})
84
+ def create_tree(user_name, repo_name, params={})
77
85
  _update_user_repo_params(user_name, repo_name)
78
86
  _validate_user_repo_params(user, repo) unless user? && repo?
79
87
  _normalize_params_keys(params)
80
88
 
89
+ raise ArgumentError, "Required param: 'tree'" unless _validate_inputs(%w[ tree ], params)
90
+
81
91
  _filter_params_keys(VALID_TREE_PARAM_NAMES, params['tree'])
82
92
  _validate_params_values(VALID_TREE_PARAM_VALUES, params['tree'])
83
93
 
@@ -171,10 +171,11 @@ module Github
171
171
  #
172
172
  def replace_labels(user_name, repo_name, issue_id, *args)
173
173
  params = args.last.is_a?(Hash) ? args.pop : {}
174
- params['data'] = [args].flatten unless args.nil?
174
+ params['data'] = args unless args.empty?
175
+
175
176
  _update_user_repo_params(user_name, repo_name)
176
177
  _validate_user_repo_params(user, repo) unless user? && repo?
177
- _validate_presence_of(issue_id)
178
+ _validate_presence_of issue_id
178
179
  _normalize_params_keys(params)
179
180
 
180
181
  put("/repos/#{user}/#{repo}/issues/#{issue_id}/labels", params)
@@ -4,7 +4,7 @@ module Github
4
4
  module VERSION
5
5
  MAJOR = 0
6
6
  MINOR = 3
7
- PATCH = 7
7
+ PATCH = 8
8
8
  BUILD = nil
9
9
 
10
10
  STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.');
@@ -0,0 +1,4 @@
1
+ {
2
+ "content": "Content of the blob",
3
+ "encoding": "utf-8"
4
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "sha": "3a0f86fb8db8eea7ccbb9a95f325ddbedfb25e15"
3
+ }
@@ -0,0 +1,29 @@
1
+ {
2
+ "sha": "9fb037999f264ba9a7fc6274d15fa3ae2ab98312",
3
+ "url": "https://api.github.com/repo/octocat/Hello-World/trees/9fb037999f264ba9a7fc6274d15fa3ae2ab98312",
4
+ "tree": [
5
+ {
6
+ "path": "file.rb",
7
+ "mode": "100644",
8
+ "type": "blob",
9
+ "size": 30,
10
+ "sha": "44b4fc6d56897b048c772eb4087f854f46256132",
11
+ "url": "https://api.github.com/octocat/Hello-World/git/blobs/44b4fc6d56897b048c772eb4087f854f46256132"
12
+ },
13
+ {
14
+ "path": "subdir",
15
+ "mode": "040000",
16
+ "type": "tree",
17
+ "sha": "f484d249c660418515fb01c2b9662073663c242e",
18
+ "url": "https://api.github.com/octocat/Hello-World/git/blobs/f484d249c660418515fb01c2b9662073663c242e"
19
+ },
20
+ {
21
+ "path": "exec_file",
22
+ "mode": "100755",
23
+ "type": "blob",
24
+ "size": 75,
25
+ "sha": "45b983be36b73c0788dc9cbcb76cbb80fc7bb057",
26
+ "url": "https://api.github.com/octocat/Hello-World/git/blobs/45b983be36b73c0788dc9cbcb76cbb80fc7bb057"
27
+ }
28
+ ]
29
+ }
@@ -1,5 +1,112 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Github::GitData::Blobs do
4
- pending
5
- end
4
+ include SpecHelpers::Base
5
+
6
+ let(:sha) { "3a0f86fb8db8eea7ccbb9a95f325ddbedfb25e15" }
7
+
8
+ it { described_class::VALID_BLOB_PARAM_NAMES.should_not be_nil }
9
+
10
+ describe "blob" do
11
+ it { github.git_data.should respond_to :blob }
12
+ it { github.git_data.should respond_to :get_blob }
13
+
14
+ context "resource found" do
15
+ before do
16
+ stub_get("/repos/#{user}/#{repo}/git/blobs/#{sha}").
17
+ to_return(:body => fixture('git_data/blob.json'), :status => 200, :headers => {:content_type => "application/json; charset=utf-8"})
18
+ end
19
+
20
+ it "should fail to get resource without sha" do
21
+ expect { github.git_data.blob(user, repo, nil)}.to raise_error(ArgumentError)
22
+ end
23
+
24
+ it "should get the resource" do
25
+ github.git_data.blob user, repo, sha
26
+ a_get("/repos/#{user}/#{repo}/git/blobs/#{sha}").should have_been_made
27
+ end
28
+
29
+ it "should get blob information" do
30
+ blob = github.git_data.blob user, repo, sha
31
+ blob.content.should eql "Content of the blob"
32
+ end
33
+
34
+ it "should return mash" do
35
+ blob = github.git_data.blob user, repo, sha
36
+ blob.should be_a Hashie::Mash
37
+ end
38
+ end
39
+
40
+ context "resource not found" do
41
+ before do
42
+ stub_get("/repos/#{user}/#{repo}/git/blobs/#{sha}").
43
+ to_return(:body => fixture('git_data/blob.json'), :status => 404, :headers => {:content_type => "application/json; charset=utf-8"})
44
+ end
45
+
46
+ it "should fail to retrive resource" do
47
+ expect {
48
+ github.git_data.blob user, repo, sha
49
+ }.to raise_error(Github::ResourceNotFound)
50
+ end
51
+ end
52
+ end # blob
53
+
54
+ describe "create_blob" do
55
+ let(:inputs) {
56
+ {
57
+ "content" => "Content of the blob",
58
+ "encoding" => "utf-8"
59
+ }
60
+ }
61
+
62
+ context "resouce created" do
63
+ before do
64
+ stub_post("/repos/#{user}/#{repo}/git/blobs").
65
+ with(:body => JSON.generate(inputs)).
66
+ to_return(:body => fixture('git_data/blob_sha.json'), :status => 201, :headers => {:content_type => "application/json; charset=utf-8"})
67
+ end
68
+
69
+ it "should fail to create resource if 'content' input is missing" do
70
+ expect {
71
+ github.git_data.create_blob user, repo, inputs.except('content')
72
+ }.to raise_error(ArgumentError)
73
+ end
74
+
75
+ it "should fail to create resource if 'encoding' input is missing" do
76
+ expect {
77
+ github.git_data.create_blob user, repo, inputs.except('encoding')
78
+ }.to raise_error(ArgumentError)
79
+ end
80
+
81
+ it "should create resource successfully" do
82
+ github.git_data.create_blob user, repo, inputs
83
+ a_post("/repos/#{user}/#{repo}/git/blobs").with(inputs).should have_been_made
84
+ end
85
+
86
+ it "should return the resource" do
87
+ blob_sha = github.git_data.create_blob user, repo, inputs
88
+ blob_sha.should be_a Hashie::Mash
89
+ end
90
+
91
+ it "should get the blob information" do
92
+ blob_sha = github.git_data.create_blob user, repo, inputs
93
+ blob_sha.sha.should == sha
94
+ end
95
+ end
96
+
97
+ context "failed to create resource" do
98
+ before do
99
+ stub_post("/repos/#{user}/#{repo}/git/blobs").with(inputs).
100
+ to_return(:body => fixture('git_data/blob_sha.json'), :status => 404, :headers => {:content_type => "application/json; charset=utf-8"})
101
+
102
+ end
103
+
104
+ it "should faile to retrieve resource" do
105
+ expect {
106
+ github.git_data.create_blob user, repo, inputs
107
+ }.to raise_error(Github::ResourceNotFound)
108
+ end
109
+ end
110
+ end # create_blob
111
+
112
+ end # Github::GitData::Blobs
@@ -1,5 +1,134 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Github::GitData::Trees do
4
- pending
5
- end
4
+ include SpecHelpers::Base
5
+
6
+ let(:sha) { "9fb037999f264ba9a7fc6274d15fa3ae2ab98312" }
7
+
8
+ it { described_class::VALID_TREE_PARAM_NAMES.should_not be_nil }
9
+
10
+ describe "tree" do
11
+ it { github.git_data.should respond_to :tree }
12
+ it { github.git_data.should respond_to :get_tree }
13
+
14
+ context "non-resursive" do
15
+ before do
16
+ stub_get("/repos/#{user}/#{repo}/git/trees/#{sha}").
17
+ to_return(:body => fixture('git_data/tree.json'), :status => 200, :headers => {:content_type => "application/json; charset=utf-8"})
18
+ end
19
+
20
+ it "should fail to get resource without sha" do
21
+ expect { github.git_data.tree(user, repo, nil)}.to raise_error(ArgumentError)
22
+ end
23
+
24
+ it "should get the resource" do
25
+ github.git_data.tree user, repo, sha
26
+ a_get("/repos/#{user}/#{repo}/git/trees/#{sha}").should have_been_made
27
+ end
28
+
29
+ it "should get tree information" do
30
+ tree = github.git_data.tree user, repo, sha
31
+ tree.sha.should eql sha
32
+ end
33
+
34
+ it "should return mash" do
35
+ tree = github.git_data.tree user, repo, sha
36
+ tree.should be_a Hashie::Mash
37
+ end
38
+ end
39
+
40
+ context "resursive" do
41
+ before do
42
+ stub_get("/repos/#{user}/#{repo}/git/trees/#{sha}?recursive=1").
43
+ to_return(:body => fixture('git_data/tree.json'), :status => 200, :headers => {:content_type => "application/json; charset=utf-8"})
44
+ end
45
+
46
+ it "should get the resource" do
47
+ github.git_data.tree user, repo, sha, 'recursive' => true
48
+ a_get("/repos/#{user}/#{repo}/git/trees/#{sha}?recursive=1").should have_been_made
49
+ end
50
+
51
+ it "should get tree information" do
52
+ tree = github.git_data.tree user, repo, sha, 'recursive' => true
53
+ tree.sha.should eql sha
54
+ end
55
+
56
+ it "should return mash" do
57
+ tree = github.git_data.tree user, repo, sha, 'recursive' => true
58
+ tree.should be_a Hashie::Mash
59
+ end
60
+ end
61
+
62
+ context "resource not found" do
63
+ before do
64
+ stub_get("/repos/#{user}/#{repo}/git/trees/#{sha}").
65
+ to_return(:body => fixture('git_data/tree.json'), :status => 404, :headers => {:content_type => "application/json; charset=utf-8"})
66
+ end
67
+
68
+ it "should fail to retrive resource" do
69
+ expect {
70
+ github.git_data.tree user, repo, sha
71
+ }.to raise_error(Github::ResourceNotFound)
72
+ end
73
+ end
74
+ end # tree
75
+
76
+ describe "create_tree" do
77
+ let(:inputs) {
78
+ {
79
+ "tree" => [
80
+ {
81
+ "path" => "file.rb",
82
+ "mode" => "100644",
83
+ "type" => "blob",
84
+ "sha" => "44b4fc6d56897b048c772eb4087f854f46256132"
85
+ }
86
+ ]
87
+ }
88
+ }
89
+
90
+ context "resouce created" do
91
+ before do
92
+ stub_post("/repos/#{user}/#{repo}/git/trees").
93
+ with(:body => JSON.generate(inputs)).
94
+ to_return(:body => fixture('git_data/tree.json'), :status => 201, :headers => {:content_type => "application/json; charset=utf-8"})
95
+ end
96
+
97
+ it "should fail to create resource if 'content' input is missing" do
98
+ expect {
99
+ github.git_data.create_tree user, repo, inputs.except('tree')
100
+ }.to raise_error(ArgumentError)
101
+ end
102
+
103
+ it "should create resource successfully" do
104
+ github.git_data.create_tree user, repo, inputs
105
+ a_post("/repos/#{user}/#{repo}/git/trees").with(inputs).should have_been_made
106
+ end
107
+
108
+ it "should return the resource" do
109
+ tree_sha = github.git_data.create_tree user, repo, inputs
110
+ tree_sha.should be_a Hashie::Mash
111
+ end
112
+
113
+ it "should get the tree information" do
114
+ tree_sha = github.git_data.create_tree user, repo, inputs
115
+ tree_sha.sha.should == sha
116
+ end
117
+ end
118
+
119
+ context "failed to create resource" do
120
+ before do
121
+ stub_post("/repos/#{user}/#{repo}/git/trees").with(inputs).
122
+ to_return(:body => fixture('git_data/tree.json'), :status => 404, :headers => {:content_type => "application/json; charset=utf-8"})
123
+
124
+ end
125
+
126
+ it "should faile to retrieve resource" do
127
+ expect {
128
+ github.git_data.create_tree user, repo, inputs
129
+ }.to raise_error(Github::ResourceNotFound)
130
+ end
131
+ end
132
+ end # create_tree
133
+
134
+ end # Github::GitData::Trees
@@ -3,10 +3,8 @@ require 'spec_helper'
3
3
  describe Github::Issues::Milestones do
4
4
  include SpecHelpers::Base
5
5
 
6
- let(:milestones_api) { Github::Issues::Milestones }
7
-
8
- it { milestones_api::VALID_MILESTONE_OPTIONS.should_not be_nil }
9
- it { milestones_api::VALID_MILESTONE_INPUTS.should_not be_nil }
6
+ it { described_class::VALID_MILESTONE_OPTIONS.should_not be_nil }
7
+ it { described_class::VALID_MILESTONE_INPUTS.should_not be_nil }
10
8
 
11
9
  describe 'milestones' do
12
10
  it { github.issues.should respond_to :milestones }
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: github_api
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.7
5
+ version: 0.3.8
6
6
  platform: ruby
7
7
  authors:
8
8
  - Piotr Murach
@@ -233,6 +233,9 @@ files:
233
233
  - spec/fixtures/auths/authorization.json
234
234
  - spec/fixtures/auths/authorizations.json
235
235
  - spec/fixtures/events/events.json
236
+ - spec/fixtures/git_data/blob.json
237
+ - spec/fixtures/git_data/blob_sha.json
238
+ - spec/fixtures/git_data/tree.json
236
239
  - spec/fixtures/issues/comment.json
237
240
  - spec/fixtures/issues/comments.json
238
241
  - spec/fixtures/issues/event.json