Class: Lcoveralls::CoverallsRequest

Inherits:
Net::HTTP::Post
  • Object
show all
Defined in:
lib/lcoveralls/coveralls_request.rb

Overview

Encapsulates a Coveralls (coverall.io) HTTP POST request.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job, path = '/api/v1/jobs') ⇒ CoverallsRequest

Initializes a new CoverallsRequest instance.

Parameters:

  • job (Hash)

    The fields of a Coveralls API request. Can be any type that can be passed to JSON#generate.

  • path (defaults to: '/api/v1/jobs')

    Optional HTTP request path.



30
31
32
33
34
35
36
37
38
39
# File 'lib/lcoveralls/coveralls_request.rb', line 30

def initialize(job, path='/api/v1/jobs')
  super path
  @boundary = (1..70).map { self.class.boundary_chr(rand(62)) }.join
  set_content_type "multipart/form-data, boundary=#{@boundary}"
  @body =
    "--#{@boundary}\r\n" +
    "Content-Disposition: form-data; name=\"json_file\"; filename=\"json_file\"\r\n" +
    "Content-Type: application/json\r\n\r\n" +
    JSON::generate(job) + "\r\n--#{@boundary}--\r\n"
end

Class Method Details

.boundary_chr(index) ⇒ String

Returns one of the 74 valid MIME boundary characters.

Parameters:

  • index (Integer)

    Index of the boundary character to fetch. Must be between 0 and 73. Note, although indices between 0 and 73 are valid according to the MIME standard, only indices between 0 and 61 are valid for HTTP headers. If index is outside the range 0 to 73, this method will raise a RuntimeError.

Returns:

  • (String)

    a valid MIME boundary character.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lcoveralls/coveralls_request.rb', line 50

def self.boundary_chr(index)
  case index
  when 0..9
    index.to_s
  when 10..35
    ('a'.ord + index - 10).chr
  when 36..61
    ('A'.ord + index - 36).chr
  when 62..73
    "'()+_,-./:=?"[index - 62]
  else
    raise "Invalid boundary index #{index}"
  end
end