Class: OrbitMembers::HTTP

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

Class Method Summary collapse

Class Method Details

.delete(url:, user_agent:, api_key:, body: nil) ⇒ Object

Raises:

  • (ArgumentError)


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

def self.delete(url:, user_agent:, api_key:, body: nil)
  url = URI(url)

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true

  req = Net::HTTP::Delete.new(url)
  req["Authorization"] = "Bearer #{api_key}"
  req["User-Agent"] = user_agent

  req.body = body if body

  response = http.request(req)

  return "Deletion successful" if response.code == "204" || response.code == "200"

  raise ArgumentError, response.message if response.code != "204" || response.code != "200"
end

.get(url:, user_agent:, api_key:, filters: nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/orbit_members/http.rb', line 27

def self.get(url:, user_agent:, api_key:, filters: nil)
  url = URI(url)
  url.query = URI.encode_www_form(filters) if filters

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  req = Net::HTTP::Get.new(url)
  req["Accept"] = "application/json"
  req["Authorization"] = "Bearer #{api_key}"
  req["User-Agent"] = user_agent

  response = http.request(req)

  validate_payload(response.body)
end

.post(url:, user_agent:, api_key:, body:) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/orbit_members/http.rb', line 9

def self.post(url:, user_agent:, api_key:, body:)
  url = URI(url)

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  req = Net::HTTP::Post.new(url)
  req["Accept"] = "application/json"
  req["Content-Type"] = "application/json"
  req["Authorization"] = "Bearer #{api_key}"
  req["User-Agent"] = user_agent

  req.body = body

  response = http.request(req)

  validate_payload(response.body)
end

.put(url:, user_agent:, api_key:, body:) ⇒ Object

Raises:

  • (ArgumentError)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/orbit_members/http.rb', line 62

def self.put(url:, user_agent:, api_key:, body:)
  url = URI(url)

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  req = Net::HTTP::Put.new(url)
  req["Accept"] = "application/json"
  req["Content-Type"] = "application/json"
  req["Authorization"] = "Bearer #{api_key}"
  req["User-Agent"] = user_agent

  req.body = body

  response = http.request(req)

  return "Update successful" if response.code == "204" || response.code == "200"

  raise ArgumentError, response.message if response.code != "204" || response.code != "200"
end

.validate_payload(payload) ⇒ Object



82
83
84
# File 'lib/orbit_members/http.rb', line 82

def self.validate_payload(payload)
  JSON.parse(payload) if OrbitMembers::Utils.valid_json?(payload)
end