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.



29
30
31
32
33
34
35
# File 'lib/brick_ftp/http_client.rb', line 29

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

Instance Method Details

#delete(path, params: {}, headers: {}) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/brick_ftp/http_client.rb', line 67

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

#get(path, params: {}, headers: {}) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/brick_ftp/http_client.rb', line 37

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

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



47
48
49
50
51
52
53
54
55
# File 'lib/brick_ftp/http_client.rb', line 47

def post(path, params: {}, headers: {})
  case res = request(:post, path, params: params, headers: headers)
  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: {}, headers: {}) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/brick_ftp/http_client.rb', line 57

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