lib/trend/client.rb in trend-0.1.0 vs lib/trend/client.rb in trend-0.1.1

- old
+ new

@@ -1,15 +1,18 @@ +require "trend/version" + module Trend class Client HEADERS = { "Content-Type" => "application/json", - "Accept" => "application/json" + "Accept" => "application/json", + "User-Agent" => "trend-ruby/#{Trend::VERSION}" } - def initialize(url: nil) - url ||= Trend.url - @uri = URI.parse(url) + 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 @@ -22,15 +25,22 @@ 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