Class: Glare::Client

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

Constant Summary collapse

BASE_HOST =
'https://api.cloudflare.com'
BASE_PATH =
'/client/v4'

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



13
14
15
16
17
18
19
20
21
# File 'lib/glare/client.rb', line 13

def initialize
  @http = Faraday::Connection.new(BASE_HOST) do |builder|
    builder.request  :json
    builder.response :logger, Logger.new(STDERR) if ENV['CF_DEBUG']
    builder.response :json, content_type: /\bjson$/

    builder.adapter :net_http
  end
end

Instance Method Details

#delete(query, params = nil) ⇒ Object



50
51
52
# File 'lib/glare/client.rb', line 50

def delete(query, params=nil)
  ApiResponse.new(@http.delete(BASE_HOST + BASE_PATH + query, params, @headers)).valid!
end

#from_global_api_key(email, auth_key) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/glare/client.rb', line 23

def from_global_api_key(email, auth_key)
  @headers = {
    'X-Auth-Email' => email,
    'X-Auth-Key' => auth_key
  }
  self
end

#from_scoped_api_token(api_token) ⇒ Object



31
32
33
34
35
36
# File 'lib/glare/client.rb', line 31

def from_scoped_api_token(api_token)
  @headers = {
    'Authorization' => "Bearer #{api_token}"
  }
  self
end

#get(query, params) ⇒ Object



38
39
40
# File 'lib/glare/client.rb', line 38

def get(query, params)
  ApiResponse.new(@http.get(BASE_HOST + BASE_PATH + query, params, @headers)).valid!
end

#post(query, data) ⇒ Object



42
43
44
# File 'lib/glare/client.rb', line 42

def post(query, data)
  ApiResponse.new(@http.post(BASE_HOST + BASE_PATH + query, data, @headers)).valid!
end

#put(query, data) ⇒ Object



46
47
48
# File 'lib/glare/client.rb', line 46

def put(query, data)
  ApiResponse.new(@http.put(BASE_HOST + BASE_PATH + query, data, @headers)).valid!
end