Class: CircleCi::Http

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

Overview

Http class handles all HTTP requests

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Http

Returns a new instance of Http.



10
11
12
13
14
15
16
# File 'lib/circleci/http.rb', line 10

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

Instance Attribute Details

#configObject



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

def config
  @config
end

#errorsObject



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

def errors
  @errors
end

#over_limitObject



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

def over_limit
  @over_limit
end

#responseObject



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

def response
  @response
end

#successObject



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

def success
  @success
end

#suspendedObject



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

def suspended
  @suspended
end

Instance Method Details

#build_params(params = {}) ⇒ Object



34
35
36
# File 'lib/circleci/http.rb', line 34

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

#create_request_args(http_verb, url, body) ⇒ Object



38
39
40
41
# File 'lib/circleci/http.rb', line 38

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

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



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

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

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



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

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

#handle_response(body, code, path) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/circleci/http.rb', line 66

def handle_response(body, code, path)
  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 = ''
  elsif parsed && successful_code
    # Response is successful
  else
    self.success = false
    self.errors = [RequestError.new(body, code, path)]
  end
end

#headersObject



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

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

#parsed_body(body) ⇒ Object



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

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

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



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

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

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



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

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

  RestClient.send(*args) do |res, _, 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