Sha256: 982bf83d12ed02977253228ba90d51c6005670ebed8f946349f71494abda30a9
Contents?: true
Size: 1.66 KB
Versions: 1
Compression:
Stored size: 1.66 KB
Contents
require "trend/version" module Trend class Client HEADERS = { "Content-Type" => "application/json", "Accept" => "application/json", "User-Agent" => "trend-ruby/#{Trend::VERSION}" } def initialize(url: nil, api_key: nil) @api_key = api_key || Trend.api_key @uri = URI.parse(url || Trend.url) @http = Net::HTTP.new(@uri.host, @uri.port) @http.use_ssl = true if @uri.scheme == "https" @http.open_timeout = 3 @http.read_timeout = 5 end def anomalies(series, params = {}) resp = make_request("anomalies", series, params) resp["anomalies"].map { |v| parse_time(v) } end def forecast(series, params = {}) resp = make_request("forecast", series, params) Hash[resp["forecast"].map { |k, v| [parse_time(k), v] }] end def correlation(series, series2, params = {}) resp = make_request("correlation", series, params.merge(series2: series2)) resp["correlation"] end private def make_request(path, series, params) post_data = { series: series }.merge(params) path = "#{path}?#{URI.encode_www_form(api_key: @api_key)}" if @api_key begin response = @http.post("/#{path}", post_data.to_json, HEADERS) rescue Errno::ECONNREFUSED, Timeout::Error => e raise Trend::Error, e.message end parsed_body = JSON.parse(response.body) rescue {} if !response.is_a?(Net::HTTPSuccess) raise Trend::Error, parsed_body["error"] || "Server returned #{response.code} response" end parsed_body end def parse_time(v) v.size == 10 ? Date.parse(v) : Time.parse(v) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
trend-0.1.1 | lib/trend/client.rb |