Module: Sheetsu::Util

Defined in:
lib/sheetsu/util.rb

Constant Summary collapse

SHEETSU_API_URL_BEGINNING =
"https://sheetsu.com/apis/v1.0/"

Class Method Summary collapse

Class Method Details

.append_query_string_to_url(url, options) ⇒ Object



23
24
25
# File 'lib/sheetsu/util.rb', line 23

def self.append_query_string_to_url(url, options)
  url + "?#{query_string(options)}"
end

.default_headersObject



6
7
8
9
10
11
12
13
# File 'lib/sheetsu/util.rb', line 6

def self.default_headers
  {
    'Accept-Encoding' => 'gzip, deflate',
    'Accept' => 'application/vnd.sheetsu.3+json',
    'Content-Type' => 'application/json',
    'User-Agent' => "Sheetsu-Ruby/#{Sheetsu::VERSION}"
  }
end

.encoded_column(options) ⇒ Object



27
28
29
# File 'lib/sheetsu/util.rb', line 27

def self.encoded_column(options)
  ['/', CGI::escape(options[:column].to_s), '/', CGI::escape(options[:value].to_s)].join('')
end

.parse_api_url(url) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/sheetsu/util.rb', line 15

def self.parse_api_url(url)
  if url.start_with?(SHEETSU_API_URL_BEGINNING)
    url
  else
    [SHEETSU_API_URL_BEGINNING, url].join('')
  end
end

.parse_response(response) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sheetsu/util.rb', line 31

def self.parse_response(response)
  case response.code.to_i
  when 200 then JSON.parse(response.body)
  when 201 then JSON.parse(response.body)
  when 204 then :ok
  when 401 then raise Sheetsu::UnauthorizedError
  when 403 then raise Sheetsu::ForbiddenError
  when 404 then raise Sheetsu::NotFoundError
  when 429 then raise Sheetsu::LimitExceedError
  else
    raise Sheetsu::SheetsuError.new(nil, response.code, response.body)
  end
end