Class: Spage::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/spage/client.rb

Overview

HTTP client for making requests to statuspage.io

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



12
13
14
15
# File 'lib/spage/client.rb', line 12

def initialize
  @api_key = Spage.config.api_key
  @api_version = Spage.config.api_version
end

Instance Method Details

#delete(resource, id) ⇒ Object



29
30
31
# File 'lib/spage/client.rb', line 29

def delete(resource, id)
  make_request(Net::HTTP::Delete, resource, id)
end

#get(resource, id = nil, query: nil) ⇒ Object



17
18
19
# File 'lib/spage/client.rb', line 17

def get(resource, id = nil, query: nil)
  make_request(Net::HTTP::Get, resource, id, query: query)
end

#make_request(http_method, resource, id, body = nil, query: nil) {|res| ... } ⇒ Object

rubocop: disable Metrics/MethodLength, Metrics/AbcSize

Yields:

  • (res)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/spage/client.rb', line 34

def make_request(http_method, resource, id, body = nil, query: nil)
  path = [@api_version, resource, id].compact.join('/')
  uri = URI::HTTP.build(host: BASE_URL, path: "/#{path}", query: query)

  request = http_method.new(uri)
  request.add_field('Authorization', "OAuth #{@api_key}")
  request.add_field('Content-Type', 'application/json')

  request.body = body if request.request_body_permitted?

  res = Net::HTTP.start(uri.hostname, use_ssl: true) do |http|
    response = http.request(request)

    response.body = JSON.parse(response.body) if response.body
    response
  end

  yield(res) if block_given?
  res
end

#post(resource, body) ⇒ Object



25
26
27
# File 'lib/spage/client.rb', line 25

def post(resource, body)
  make_request(Net::HTTP::Post, resource, nil, body)
end

#put(resource, id, body) ⇒ Object



21
22
23
# File 'lib/spage/client.rb', line 21

def put(resource, id, body)
  make_request(Net::HTTP::Put, resource, id, body)
end