Skip to content

Commit

Permalink
feat: add .available? api
Browse files Browse the repository at this point in the history
  • Loading branch information
icyleaf committed Nov 15, 2018
1 parent 371c242 commit 6099e67
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ For more information, refer to [API Documentation](http://icyleaf.github.io/gitl

#### Completed

- Service Status (**Not Gitlab API**)
- Check service available - `available?`
- Users
- List Users - `users`
- Single user - `user(user_id)`
Expand Down Expand Up @@ -276,7 +278,6 @@ For more information, refer to [API Documentation](http://icyleaf.github.io/gitl
- Runners
- Pipelines


## Help and Discussion

You can browse the API documents:
Expand Down
7 changes: 7 additions & 0 deletions spec/gitlab/client_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,11 @@ describe Gitlab::Client do
end
end
end

describe ".available?" do
it "should return true if service works" do
stub_get("/user", "user")
client.available?.should be_true
end
end
end
4 changes: 2 additions & 2 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ def load_fixture(name : String?)
end

# GET
def stub_get(path, fixture, params = nil, response_headers = {} of String => String)
def stub_get(path, fixture, params = nil, response_headers = {} of String => String, status = 200)
query = "?#{HTTP::Params.encode(params)}" if params

response_headers.merge!({"Content-Type" => "application/json"})
WebMock.stub(:get, "#{client.endpoint}#{path}#{query}")
.with(headers: {"Private-Token" => client.token})
.to_return(body: load_fixture(fixture), headers: response_headers)
.to_return(status: status, body: load_fixture(fixture), headers: response_headers)
end

# POST
Expand Down
22 changes: 18 additions & 4 deletions src/gitlab/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module Gitlab
# Create a new client
#
# ```
# Gitlab.Client.new("<endpoint>", "<token")
# Gitlab::Client.new("<endpoint>", "<token")
# ```
def initialize(@endpoint : String, @token : String)
if @endpoint.includes?("api/v5")
Expand All @@ -33,7 +33,7 @@ module Gitlab
end

{% for verb in %w(get head) %}
# Return a Gitlab::Response by sending a {{verb.id.upcase}} method http request
# Return a Halite::Response by sending a {{verb.id.upcase}} method http request
#
# ```
# client.{{ verb.id }}("/path", params: {
Expand All @@ -50,7 +50,7 @@ module Gitlab
{% end %}

{% for verb in %w(post put patch delete) %}
# Return a `Gitlab::Response` by sending a {{verb.id.upcase}} http request
# Return a `Halite::Response` by sending a {{verb.id.upcase}} http request
#
# ```
# client.{{ verb.id }}("/path", form: {
Expand All @@ -66,8 +66,22 @@ module Gitlab
end
{% end %}

# Return a `Bool` status by gitlab api service
#
# - Return `Bool`
#
# ```
# client.available? # => true
# ```
def available?
get("/user")
true
rescue Halite::Exception::ConnectionError
false
end

# {% for method in [:get, :post, :put, :delete] %}
# # Return a Gitlab::Response by sending a {{method.id.upcase}} method http request
# # Return a Halite::Response by sending a {{method.id.upcase}} method http request
# #
# # ```
# # client.{{method.id}}("/path", { "key" => "value"})
Expand Down

0 comments on commit 6099e67

Please sign in to comment.