Skip to content

Commit

Permalink
Styling fixes for rubocop warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
mtchavez committed Feb 16, 2016
1 parent ac33cf7 commit 92cbb91
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 233 deletions.
26 changes: 14 additions & 12 deletions lib/circleci.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@
require 'rest-client'
require 'net/http'

files = [
'build',
'config',
'http',
'project',
'request_error',
'response',
'user'
files = %w[
build
config
http
project
request_error
response
user
]

files.each { |path| require_relative "./circleci/#{path}" }


##
#
# CircleCi module configured to for endpoint interactions
module CircleCi
module_function

extend self

##
#
# @example Configure CircleCi with your token
# CircleCi.configure do |config|
Expand All @@ -29,6 +31,7 @@ def configure
yield config
end

##
#
# @return [CircleCi::Config]

Expand All @@ -53,5 +56,4 @@ def http # @private
def organization(name, params = {})
http.get "/organization/#{name}", params
end

end
25 changes: 8 additions & 17 deletions lib/circleci/build.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
module CircleCi

##
#
# Class for managing builds for a project

##
#
# Class for interacting with and managing builds
class Build

##
#
# Class for interacting with and managing builds

##
#
# Get artifacts for a specific build of a project
Expand All @@ -19,7 +12,7 @@ class Build
# @param build [String] - Build ID
# @return [CircleCi::Response] - Response object

def self.artifacts username, project, build
def self.artifacts(username, project, build)
CircleCi.http.get "/project/#{username}/#{project}/#{build}/artifacts"
end

Expand All @@ -32,7 +25,7 @@ def self.artifacts username, project, build
# @param build [String] - Build ID
# @return [CircleCi::Response] - Response object

def self.cancel username, project, build
def self.cancel(username, project, build)
CircleCi.http.post "/project/#{username}/#{project}/#{build}/cancel"
end

Expand All @@ -45,7 +38,7 @@ def self.cancel username, project, build
# @param build [String] - Build ID
# @return [CircleCi::Response] - Response object

def self.get username, project, build
def self.get(username, project, build)
CircleCi.http.get "/project/#{username}/#{project}/#{build}"
end

Expand All @@ -58,7 +51,7 @@ def self.get username, project, build
# @param build [String] - Build ID
# @return [CircleCi::Response] - Response object

def self.retry username, project, build
def self.retry(username, project, build)
CircleCi.http.post "/project/#{username}/#{project}/#{build}/retry"
end

Expand All @@ -71,10 +64,8 @@ def self.retry username, project, build
# @param build [String] - Build ID
# @return [CircleCi::Response] - Response object

def self.tests username, project, build
def self.tests(username, project, build)
CircleCi.http.get "/project/#{username}/#{project}/#{build}/tests"
end

end

end
11 changes: 4 additions & 7 deletions lib/circleci/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ module CircleCi
#
# Config class used internally.
# Configure API calls using AlPapi.configure

class Config

VERSION = 'v1'
DEFAULT_HOST = "https://circleci.com/api/#{VERSION}"
VERSION = 'v1'.freeze
DEFAULT_HOST = "https://circleci.com/api/#{VERSION}".freeze
DEFAULT_PORT = 80

attr_accessor :token, :host, :port
attr_accessor :token
attr_reader :host, :port

This comment has been minimized.

Copy link
@rjaquino

rjaquino Feb 27, 2016

This broke configurability of API host added in #10

This comment has been minimized.

Copy link
@mtchavez

mtchavez Feb 27, 2016

Author Owner

Cut a new release which should address this v0.2.2 and yanked a bad version which removed being able to configure the host v0.2.1. If you were on that version please update. Sorry for any issues.

This comment has been minimized.

Copy link
@rjaquino

rjaquino Feb 27, 2016

Thanks! I found that using 0.2.0 worked for me, but I'll upgrade to 0.2.2.


##
#
Expand All @@ -20,7 +19,5 @@ def initialize
@host = DEFAULT_HOST
@port = DEFAULT_PORT
end

end

end
39 changes: 23 additions & 16 deletions lib/circleci/http.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
# encoding: utf-8

module CircleCi

##
#
# Http class handles all HTTP requests
class Http # @private

attr_accessor :errors, :response, :success, :config, :over_limit, :suspended

def initialize(_config)
@config, @errors, @success, @over_limit, @suspended = _config, [], false, false, false
def initialize(config)
@config = config
@errors = []
@success = false
@over_limit = false
@suspended = false
end

def get(path, params = {})
Expand All @@ -31,18 +36,15 @@ def build_params(params = {})
end

def create_request_args(http_verb, url, body)
if http_verb == "post"
return [http_verb, url, body, headers]
end

return [http_verb, url, body, headers] if http_verb == 'post'
[http_verb, url, headers]
end

def request(http_verb, path, body = {})
url = "#{@config.host}#{path}"
args = create_request_args http_verb, url, body

RestClient.send(*args) do |res, req, raw_res|
RestClient.send(*args) do |res, _, raw_res|
body = res.body.to_s
body.force_encoding(Encoding::UTF_8)
code = raw_res.code.to_i
Expand All @@ -55,22 +57,27 @@ def request(http_verb, path, body = {})
end
end

def parsed_body(body)
JSON.parse(body)
rescue
nil
end

def handle_response(body, code, path)
parsed = JSON.parse(body) rescue nil
successful_code = (200..299).include?(code)
parsed = parsed_body(body)
successful_code = (200..299).cover?(code)
self.response = parsed if parsed
self.success = true

# Some responses are empty but are successful
if body == '""' && successful_code
self.response = ''
self.success = true
elsif parsed && successful_code
self.success = true
# Response is successful
else
self.errors << RequestError.new(body, code, path)
self.success = false
self.errors = [RequestError.new(body, code, path)]
end
end

end

end
43 changes: 19 additions & 24 deletions lib/circleci/project.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
module CircleCi

##
#
# Class for interacting with Projects

class Project

##
#
# Return all projects for your API key
Expand All @@ -24,7 +21,7 @@ def self.all
# @param project [String] - Name of project
# @return [CircleCi::Response] - Response object

def self.build username, project
def self.build(username, project)
CircleCi.http.post "/project/#{username}/#{project}"
end

Expand All @@ -38,9 +35,9 @@ def self.build username, project
# @param build_parameters [Hash] - Optional Build Parameters
# @return [CircleCi::Response] - Response object

def self.build_branch username, project, branch, build_parameters = {}
def self.build_branch(username, project, branch, build_parameters = {})
body = {}
body["build_parameters"] = build_parameters unless build_parameters.empty?
body['build_parameters'] = build_parameters unless build_parameters.empty?
CircleCi.http.post "/project/#{username}/#{project}/tree/#{branch}", {}, body
end

Expand All @@ -54,7 +51,7 @@ def self.build_branch username, project, branch, build_parameters = {}
# @param key [String] - The ssh private key
# @param hostname [String] - The hostname identified by the key
# @return [CircleCi::Response] - Response object
def self.build_ssh_key username, project, build, key, hostname
def self.build_ssh_key(username, project, build, key, hostname)
body = { hostname: hostname, private_key: key }
CircleCi.http.post "/project/#{username}/#{project}/#{build}/ssh-users", {}, body
end
Expand All @@ -67,7 +64,7 @@ def self.build_ssh_key username, project, build, key, hostname
# @param project [String] - Name of project
# @return [CircleCi::Response] - Response object

def self.clear_cache username, project
def self.clear_cache(username, project)
CircleCi.http.delete "/project/#{username}/#{project}/build-cache"
end

Expand All @@ -80,7 +77,7 @@ def self.clear_cache username, project
# @param fingerprint [String] - Fingerprint of a checkout key
# @return [CircleCi::Response] - Response object

def self.delete_checkout_key username, project, fingerprint
def self.delete_checkout_key(username, project, fingerprint)
CircleCi.http.delete "/project/#{username}/#{project}/checkout-key/#{fingerprint}"
end

Expand All @@ -93,7 +90,7 @@ def self.delete_checkout_key username, project, fingerprint
# @param project [String] - Name of project
# @return [CircleCi::Response] - Response object

def self.enable username, project
def self.enable(username, project)
CircleCi.http.post "/project/#{username}/#{project}/enable"
end

Expand All @@ -105,7 +102,7 @@ def self.enable username, project
# @param project [String] - Name of project
# @return [CircleCi::Response] - Response object

def self.envvars username, project
def self.envvars(username, project)
CircleCi.http.get "/project/#{username}/#{project}/envvar"
end

Expand All @@ -118,7 +115,7 @@ def self.envvars username, project
# @param envvar [Hash] - {name: 'foo', value: 'bar'}
# @return [CircleCi::Response] - Response object

def self.set_envvar username, project, envvar
def self.set_envvar(username, project, envvar)
body = envvar
CircleCi.http.post "/project/#{username}/#{project}/envvar", {}, body
end
Expand All @@ -131,7 +128,7 @@ def self.set_envvar username, project, envvar
# @param project [String] - Name of project
# @return [CircleCi::Response] - Response object

def self.follow username, project
def self.follow(username, project)
CircleCi.http.post "/project/#{username}/#{project}/follow"
end

Expand All @@ -144,7 +141,7 @@ def self.follow username, project
# @param fingerprint [String] - Fingerprint of a checkout key
# @return [CircleCi::Response] - Response object

def self.get_checkout_key username, project, fingerprint
def self.get_checkout_key(username, project, fingerprint)
CircleCi.http.get "/project/#{username}/#{project}/checkout-key/#{fingerprint}"
end

Expand All @@ -156,7 +153,7 @@ def self.get_checkout_key username, project, fingerprint
# @param project [String] - Name of project
# @return [CircleCi::Response] - Response object

def self.list_checkout_keys username, project
def self.list_checkout_keys(username, project)
CircleCi.http.get "/project/#{username}/#{project}/checkout-key"
end

Expand All @@ -169,7 +166,7 @@ def self.list_checkout_keys username, project
# @param type [String] - The type of key to create. Can be 'deploy-key' or 'github-user-key'.
# @return [CircleCi::Response] - Response object

def self.new_checkout_key username, project, type
def self.new_checkout_key(username, project, type)
body = { type: type }
CircleCi.http.post "/project/#{username}/#{project}/checkout-key", {}, body
end
Expand All @@ -182,7 +179,7 @@ def self.new_checkout_key username, project, type
# @param project [String] - Name of project
# @return [CircleCi::Response] - Response object

def self.recent_builds username, project
def self.recent_builds(username, project)
CircleCi.http.get "/project/#{username}/#{project}"
end

Expand All @@ -195,7 +192,7 @@ def self.recent_builds username, project
# @param branch [String] - Name of branch
# @return [CircleCi::Response] - Response object

def self.recent_builds_branch username, project, branch
def self.recent_builds_branch(username, project, branch)
CircleCi.http.get "/project/#{username}/#{project}/tree/#{branch}"
end

Expand All @@ -207,7 +204,7 @@ def self.recent_builds_branch username, project, branch
# @param project [String] - Name of project
# @return [CircleCi::Response] - Response object

def self.settings username, project
def self.settings(username, project)
CircleCi.http.get "/project/#{username}/#{project}/settings"
end

Expand All @@ -220,7 +217,8 @@ def self.settings username, project
# @param key [String] - The ssh private key
# @param hostname [String] - The hostname identified by the key
# @return [CircleCi::Response] - Response object
def self.ssh_key username, project, key, hostname

def self.ssh_key(username, project, key, hostname)
body = { hostname: hostname, private_key: key }
CircleCi.http.post "/project/#{username}/#{project}/ssh-key", {}, body
end
Expand All @@ -233,11 +231,8 @@ def self.ssh_key username, project, key, hostname
# @param project [String] - Name of project
# @return [CircleCi::Response] - Response object

def self.unfollow username, project
def self.unfollow(username, project)
CircleCi.http.post "/project/#{username}/#{project}/unfollow"
end

end

end

Loading

0 comments on commit 92cbb91

Please sign in to comment.