Class: CircleCi::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/circleci/http.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_config) ⇒ Http

Returns a new instance of Http.



9
10
11
# File 'lib/circleci/http.rb', line 9

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

Instance Attribute Details

#configObject

Returns the value of attribute config.



7
8
9
# File 'lib/circleci/http.rb', line 7

def config
  @config
end

#errorsObject

Returns the value of attribute errors.



7
8
9
# File 'lib/circleci/http.rb', line 7

def errors
  @errors
end

#over_limitObject

Returns the value of attribute over_limit.



7
8
9
# File 'lib/circleci/http.rb', line 7

def over_limit
  @over_limit
end

#responseObject

Returns the value of attribute response.



7
8
9
# File 'lib/circleci/http.rb', line 7

def response
  @response
end

#successObject

Returns the value of attribute success.



7
8
9
# File 'lib/circleci/http.rb', line 7

def success
  @success
end

#suspendedObject

Returns the value of attribute suspended.



7
8
9
# File 'lib/circleci/http.rb', line 7

def suspended
  @suspended
end

Instance Method Details

#build_params(params = {}) ⇒ Object



29
30
31
# File 'lib/circleci/http.rb', line 29

def build_params(params = {})
  params.merge('circle-token' => @config.token)
end

#create_request_args(http_verb, url, body) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/circleci/http.rb', line 33

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

  [http_verb, url, headers]
end

#delete(path, params = {}) ⇒ Object



21
22
23
# File 'lib/circleci/http.rb', line 21

def delete(path, params = {})
  request 'delete', "#{path}?#{RestClient::Payload.generate(build_params(params))}"
end

#get(path, params = {}) ⇒ Object



13
14
15
# File 'lib/circleci/http.rb', line 13

def get(path, params = {})
  request 'get', "#{path}?#{RestClient::Payload.generate(build_params(params))}"
end

#handle_response(body, code, path) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/circleci/http.rb', line 58

def handle_response(body, code, path)
  parsed = JSON.parse(body) rescue nil
  if parsed && (200..299).include?(code)
    self.response = parsed
    self.success = true
  else
    self.errors << RequestError.new(body, code, path)
  end
end

#headersObject



25
26
27
# File 'lib/circleci/http.rb', line 25

def headers
  { 'accept' => 'application/json', 'content-type' => 'application/json' }
end

#post(path, params = {}, body = {}) ⇒ Object



17
18
19
# File 'lib/circleci/http.rb', line 17

def post(path, params = {}, body = {})
  request 'post', "#{path}?#{RestClient::Payload.generate(build_params(params))}", body
end

#request(http_verb, path, body = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/circleci/http.rb', line 41

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|
    body = res.body.to_s
    body.force_encoding(Encoding::UTF_8)
    code = raw_res.code.to_i

    self.response = body
    self.errors   = []

    handle_response(body, code, path)
    Response.new(self, code, path)
  end
end