Class: Effective::Http

Inherits:
Object
  • Object
show all
Defined in:
app/models/effective/http.rb

Class Method Summary collapse

Class Method Details

.delete(endpoint, params: nil, headers: nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/effective/http.rb', line 71

def self.delete(endpoint, params: nil, headers: nil)
  headers = { 'Content-Type': 'application/json' }.merge(headers || {})
  query = ('?' + params.compact.map { |k, v| "$#{k}=#{v}" }.join('&')) if params.present?

  uri = URI.parse(endpoint + query.to_s)

  http = Net::HTTP.new(uri.host, uri.port)
  http.read_timeout = 10
  http.use_ssl = true if endpoint.start_with?('https')

  response = with_retries do
    puts "[DELETE] #{uri}" if Rails.env.development?
    http.delete(uri, headers)
  end

  unless ['200', '204'].include?(response.code.to_s)
    puts("Response code: #{response.code} #{response.body}")
    return false
  end

  JSON.parse(response.body)
end

.get(endpoint, params: nil, headers: nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/models/effective/http.rb', line 4

def self.get(endpoint, params: nil, headers: nil)
  headers = { 'Content-Type': 'application/json' }.merge(headers || {})
  query = ('?' + params.compact.map { |k, v| "$#{k}=#{v}" }.join('&')) if params.present?

  uri = URI.parse(endpoint + query.to_s)

  http = Net::HTTP.new(uri.host, uri.port)
  http.read_timeout = 10
  http.use_ssl = true if endpoint.start_with?('https')

  response = with_retries do
    puts "[GET] #{uri}" if Rails.env.development?
    http.get(uri, headers)
  end

  unless ['200', '204'].include?(response.code.to_s)
    puts("Response code: #{response.code} #{response.body}")
    return false
  end

  JSON.parse(response.body)
end

.patch(endpoint, params:, headers: nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/effective/http.rb', line 49

def self.patch(endpoint, params:, headers: nil)
  headers = { 'Content-Type': 'application/json' }.merge(headers || {})

  uri = URI.parse(endpoint)

  http = Net::HTTP.new(uri.host, uri.port)
  http.read_timeout = 10
  http.use_ssl = true if endpoint.start_with?('https')

  response = with_retries do
    puts "[PATCH] #{uri} #{params}" if Rails.env.development?
    http.post(uri.path, params.to_json, headers)
  end

  unless ['200', '204'].include?(response.code.to_s)
    puts("Response code: #{response.code} #{response.body}")
    return false
  end

  JSON.parse(response.body)
end

.post(endpoint, params:, headers: nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/models/effective/http.rb', line 27

def self.post(endpoint, params:, headers: nil)
  headers = { 'Content-Type': 'application/json' }.merge(headers || {})

  uri = URI.parse(endpoint)

  http = Net::HTTP.new(uri.host, uri.port)
  http.read_timeout = 10
  http.use_ssl = true if endpoint.start_with?('https')

  response = with_retries do
    puts "[POST] #{uri} #{params}" if Rails.env.development?
    http.post(uri.path, params.to_json, headers)
  end

  unless ['200', '204'].include?(response.code.to_s)
    puts("Response code: #{response.code} #{response.body}")
    return false
  end

  JSON.parse(response.body)
end