Class: SoracomSummary::HttpClient
- Inherits:
-
Object
- Object
- SoracomSummary::HttpClient
- Defined in:
- lib/soracom_summary/http_client.rb
Class Method Summary collapse
Instance Method Summary collapse
- #get(path, headers) ⇒ Object
-
#initialize(base_url:, interval_second: 1, retry_limit: 5) ⇒ HttpClient
constructor
A new instance of HttpClient.
- #post(path, headers, request_body) ⇒ Object
Constructor Details
#initialize(base_url:, interval_second: 1, retry_limit: 5) ⇒ HttpClient
Returns a new instance of HttpClient.
24 25 26 27 28 29 30 |
# File 'lib/soracom_summary/http_client.rb', line 24 def initialize(base_url: , interval_second: 1, retry_limit: 5) @logger = Logger.new(STDERR) @logger.level = ENV['SORACOM_SUMMARY_DEBUG'] ? Logger::DEBUG : Logger::WARN @base_url = base_url @interval_second = interval_second @retry_limit = retry_limit end |
Class Method Details
.download(url) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/soracom_summary/http_client.rb', line 9 def download(url) uri = URI.parse(url) request = Net::HTTP::Get.new(uri) response = Net::HTTP.start(uri.hostname, uri.port, { use_ssl: true }) do |http| http.request(request) end if (response.code.start_with?('2')) return response.body.force_encoding('UTF-8') else Logger.new(STDERR).warn("Cannot download csv") raise end end |
Instance Method Details
#get(path, headers) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/soracom_summary/http_client.rb', line 32 def get(path, headers) uri = URI.join(@base_url, path) request = Net::HTTP::Get.new(uri) if headers.class == Hash headers.each do |key, value| request[key] = value end end access(uri, request) end |
#post(path, headers, request_body) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/soracom_summary/http_client.rb', line 45 def post(path, headers, request_body) uri = URI.join(@base_url, path) request = Net::HTTP::Post.new(uri) request.content_type = 'application/json' if headers.class == Hash headers.each do |key, value| request[key] = value end end if !request_body.nil? request.body = JSON.generate(request_body) end access(uri, request) end |