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.



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

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



71
72
73
74
75
76
77
78
79
# File 'lib/brick_ftp/http_client.rb', line 71

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



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/brick_ftp/http_client.rb', line 38

def get(path, params: {}, headers: {})
  query = params.map { |k, v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join('&')
  path = "#{path}?#{query}" unless query.empty?

  case res = request(:get, path, 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



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

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



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

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