Class: DiffTest::ApiClient

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/diff_test/api_client.rb

Class Method Summary collapse

Class Method Details

.delete(path, options = {}, &block) ⇒ Object



27
28
29
30
# File 'lib/diff_test/api_client.rb', line 27

def self.delete(path, options = {}, &block)
  process_options(options)
  super
end

.get(path, options = {}, &block) ⇒ Object



7
8
9
10
# File 'lib/diff_test/api_client.rb', line 7

def self.get(path, options = {}, &block)
  process_options(options)
  super
end

.gzip_body(options) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/diff_test/api_client.rb', line 45

def self.gzip_body(options)
  return unless options[:body]

  buffer = StringIO.new
  gz = Zlib::GzipWriter.new(buffer)
  gz.write(options[:body])
  gz.close
  options[:body] = buffer.string
  options[:headers] ||= {}
  options[:headers]['Content-Encoding'] = 'gzip'
end

.jsonify_body(options) ⇒ Object



38
39
40
41
42
43
# File 'lib/diff_test/api_client.rb', line 38

def self.jsonify_body(options)
  return unless options[:body] && options[:body].is_a?(Hash)
  options[:body] = options[:body].to_json
  options[:headers] ||= {}
  options[:headers]['Content-Type'] = 'application/json'
end

.patch(path, options = {}, &block) ⇒ Object



22
23
24
25
# File 'lib/diff_test/api_client.rb', line 22

def self.patch(path, options = {}, &block)
  process_options(options)
  super
end

.post(path, options = {}, &block) ⇒ Object



12
13
14
15
# File 'lib/diff_test/api_client.rb', line 12

def self.post(path, options = {}, &block)
  process_options(options)
  super
end

.process_options(options) ⇒ Object



32
33
34
35
36
# File 'lib/diff_test/api_client.rb', line 32

def self.process_options(options)
  jsonify_body(options)
  gzip_body(options)
  set_api_key_in_headers(options)
end

.put(path, options = {}, &block) ⇒ Object



17
18
19
20
# File 'lib/diff_test/api_client.rb', line 17

def self.put(path, options = {}, &block)
  process_options(options)
  super
end

.set_api_key_in_headers(options) ⇒ Object



57
58
59
60
61
# File 'lib/diff_test/api_client.rb', line 57

def self.set_api_key_in_headers(options)
  base_uri DiffTest.configuration.base_api_url
  options[:headers] ||= {}
  options[:headers]['X-Diff-Test-Api-Key'] = DiffTest.configuration.api_key
end