Class: BrickFTP::HTTPClient

Inherits:
Object
  • Object
show all
Defined in:
lib/brick_ftp/http_client.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

USER_AGENT =
'BrickFTP Client/0.1 (https://github.com/koshigoe/brick_ftp)'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(host = nil) ⇒ HTTPClient

Returns a new instance of HTTPClient.



27
28
29
30
31
# File 'lib/brick_ftp/http_client.rb', line 27

def initialize(host = nil)
  @host = host || BrickFTP.config.api_host
  @conn = Net::HTTP.new(@host, 443)
  @conn.use_ssl = true
end

Instance Method Details

#delete(path) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/brick_ftp/http_client.rb', line 63

def delete(path)
  case res = request(:delete, path)
  when Net::HTTPSuccess
    true
  else
    # TODO: redirect
    raise Error, res
  end
end

#get(path) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/brick_ftp/http_client.rb', line 33

def get(path)
  case res = request(:get, path)
  when Net::HTTPSuccess
    res.body.nil? || res.body.empty? ? {} : JSON.parse(res.body)
  else
    # TODO: redirect
    raise Error, res
  end
end

#post(path, params: {}) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/brick_ftp/http_client.rb', line 43

def post(path, params: {})
  case res = request(:post, path, params: params)
  when Net::HTTPSuccess, Net::HTTPCreated
    res.body.nil? || res.body.empty? ? {} : JSON.parse(res.body)
  else
    # TODO: redirect
    raise Error, res
  end
end

#put(path, params: {}) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/brick_ftp/http_client.rb', line 53

def put(path, params: {})
  case res = request(:put, path, params: params)
  when Net::HTTPSuccess
    res.body.nil? || res.body.empty? ? {} : JSON.parse(res.body)
  else
    # TODO: redirect
    raise Error, res
  end
end