Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-shell
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-shell
Commits
f92a9c5a
Commit
f92a9c5a
authored
Feb 11, 2015
by
Douwe Maan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor: Remove #tap for readability and performance and DRY up get/post.
parent
f11e1bf9
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
41 deletions
+42
-41
lib/gitlab_net.rb
lib/gitlab_net.rb
+40
-40
spec/gitlab_net_spec.rb
spec/gitlab_net_spec.rb
+2
-1
No files found.
lib/gitlab_net.rb
View file @
f92a9c5a
...
...
@@ -64,63 +64,61 @@ class GitlabNet
"
#{
config
.
gitlab_url
}
/api/v3/internal"
end
def
http_client_for
(
ur
l
)
Net
::
HTTP
.
new
(
url
.
host
,
url
.
port
).
tap
do
|
http
|
if
URI
::
HTTPS
===
url
http
.
use_ssl
=
true
http
.
cert_store
=
cert_stor
e
http
.
verify_mode
=
OpenSSL
::
SSL
::
VERIFY_NONE
if
config
.
http_settings
[
'self_signed_cert'
]
end
def
http_client_for
(
ur
i
)
http
=
Net
::
HTTP
.
new
(
uri
.
host
,
uri
.
port
)
if
uri
.
is_a?
(
URI
::
HTTPS
)
http
.
use_ssl
=
tru
e
http
.
cert_store
=
cert_store
http
.
verify_mode
=
OpenSSL
::
SSL
::
VERIFY_NONE
if
config
.
http_settings
[
'self_signed_cert'
]
end
http
end
def
http_request_for
(
url
,
method
=
:get
)
def
http_request_for
(
method
,
uri
,
params
=
{})
request_klass
=
method
==
:get
?
Net
::
HTTP
::
Get
:
Net
::
HTTP
::
Post
request
=
request_klass
.
new
(
uri
.
request_uri
)
user
=
config
.
http_settings
[
'user'
]
password
=
config
.
http_settings
[
'password'
]
request
.
basic_auth
(
user
,
password
)
if
user
&&
password
request
.
set_form_data
(
params
.
merge
(
secret_token:
secret_token
))
request
end
def
request
(
method
,
url
,
params
=
{})
$logger
.
debug
"Performing
#{
method
.
to_s
.
upcase
}
#{
url
}
"
uri
=
URI
.
parse
(
url
)
http
=
http_client_for
(
uri
)
request
=
http_request_for
(
method
,
uri
,
params
)
if
method
==
:get
Net
::
HTTP
::
Get
.
new
(
url
.
request_uri
).
tap
{
|
r
|
r
.
basic_auth
(
user
,
password
)
if
user
&&
password
}
response
=
http
.
start
{
http
.
request
(
request
)
}
if
response
.
code
==
"200"
$logger
.
debug
"Received response
#{
response
.
code
}
=> <
#{
response
.
body
}
>."
else
Net
::
HTTP
::
Post
.
new
(
url
.
request_uri
).
tap
{
|
r
|
r
.
basic_auth
(
user
,
password
)
if
user
&&
password
}
$logger
.
error
"API call <
#{
method
.
to_s
.
upcase
}
#{
url
}
> failed:
#{
response
.
code
}
=> <
#{
response
.
body
}
>."
end
response
end
def
get
(
url
)
$logger
.
debug
"Performing GET
#{
url
}
"
url
=
URI
.
parse
(
url
)
http
=
http_client_for
url
request
=
http_request_for
url
request
.
set_form_data
(
secret_token:
secret_token
)
http
.
start
{
|
http
|
http
.
request
(
request
)
}.
tap
do
|
resp
|
if
resp
.
code
==
"200"
$logger
.
debug
{
"Received response
#{
resp
.
code
}
=> <
#{
resp
.
body
}
>."
}
else
$logger
.
error
{
"API call <GET
#{
url
}
> failed:
#{
resp
.
code
}
=> <
#{
resp
.
body
}
>."
}
end
end
request
(
:get
,
url
)
end
def
post
(
url
,
params
)
$logger
.
debug
"Performing POST
#{
url
}
"
url
=
URI
.
parse
(
url
)
http
=
http_client_for
(
url
)
request
=
http_request_for
(
url
,
:post
)
request
.
set_form_data
(
params
.
merge
(
secret_token:
secret_token
))
http
.
start
{
|
http
|
http
.
request
(
request
)
}.
tap
do
|
resp
|
if
resp
.
code
==
"200"
$logger
.
debug
{
"Received response
#{
resp
.
code
}
=> <
#{
resp
.
body
}
>."
}
else
$logger
.
error
{
"API call <POST
#{
url
}
> failed:
#{
resp
.
code
}
=> <
#{
resp
.
body
}
>."
}
end
end
request
(
:post
,
url
,
params
)
end
def
cert_store
@cert_store
||=
OpenSSL
::
X509
::
Store
.
new
.
tap
do
|
store
|
@cert_store
||=
begin
store
=
OpenSSL
::
X509
::
Store
.
new
store
.
set_default_paths
if
ca_file
=
config
.
http_settings
[
'ca_file'
]
...
...
@@ -130,6 +128,8 @@ class GitlabNet
if
ca_path
=
config
.
http_settings
[
'ca_path'
]
store
.
add_path
(
ca_path
)
end
store
end
end
...
...
spec/gitlab_net_spec.rb
View file @
f92a9c5a
...
...
@@ -139,12 +139,13 @@ describe GitlabNet, vcr: true do
let
(
:user
)
{
'user'
}
let
(
:password
)
{
'password'
}
let
(
:url
)
{
URI
'http://localhost/'
}
subject
{
gitlab_net
.
send
:http_request_for
,
url
}
subject
{
gitlab_net
.
send
:http_request_for
,
:get
,
url
}
before
do
gitlab_net
.
send
(
:config
).
http_settings
.
stub
(
:[]
).
with
(
'user'
)
{
user
}
gitlab_net
.
send
(
:config
).
http_settings
.
stub
(
:[]
).
with
(
'password'
)
{
password
}
get
.
should_receive
(
:basic_auth
).
with
(
user
,
password
).
once
get
.
should_receive
(
:set_form_data
).
with
(
hash_including
(
secret_token:
'a123'
)).
once
end
it
{
should_not
be_nil
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment