trend 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +11 -0
- data/CHANGELOG.md +5 -0
- data/README.md +23 -3
- data/lib/trend/client.rb +14 -4
- data/lib/trend/version.rb +1 -1
- data/lib/trend.rb +13 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a83763d819e0962a0c4c7ca0cb0f5af76852be87becd30bbd341b12246a7510b
|
4
|
+
data.tar.gz: ed4b4e58dc20df46dfdb7d252f4dcf680494bb38b901301c002a5a309f840344
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 042cd0604e706892b96009608827b6ec4cc14336d20d15779de0f6c0bb05bace81b16691d1c78168449041efe1a308c4fccc46ac9ef7b8ff98a44f4ecaa948b6
|
7
|
+
data.tar.gz: dd854dc3931e03260eb236fe7d40f7f3da2cb830676472e0d5a9d48c568274e0dae40b47b208cba31dba70e45e2bf503e353eb746283162eca6d3ebeaf566f6c
|
data/.travis.yml
ADDED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# Trend
|
2
2
|
|
3
|
-
Ruby client for [Trend](https://trendapi.org), the
|
3
|
+
Ruby client for [Trend](https://trendapi.org), the anomaly detection and forecasting API
|
4
|
+
|
5
|
+
[](https://travis-ci.org/ankane/trend)
|
4
6
|
|
5
7
|
## Getting Started
|
6
8
|
|
@@ -10,7 +12,7 @@ Add this line to your application’s Gemfile:
|
|
10
12
|
gem 'trend'
|
11
13
|
```
|
12
14
|
|
13
|
-
###
|
15
|
+
### Anomaly Detection
|
14
16
|
|
15
17
|
Detect anomalies in a time series
|
16
18
|
|
@@ -36,7 +38,7 @@ series = User.group_by_day(:created_at).count
|
|
36
38
|
Trend.anomalies(series)
|
37
39
|
```
|
38
40
|
|
39
|
-
###
|
41
|
+
### Forecasting
|
40
42
|
|
41
43
|
Get future predictions for a time series
|
42
44
|
|
@@ -64,6 +66,24 @@ Specify the number of predictions to return
|
|
64
66
|
Trend.forecast(series, count: 3)
|
65
67
|
```
|
66
68
|
|
69
|
+
### Correlation [experimental]
|
70
|
+
|
71
|
+
Get the correlation between two time series
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
Trend.correlation(series, series2)
|
75
|
+
```
|
76
|
+
|
77
|
+
## Authentication
|
78
|
+
|
79
|
+
An API key is needed for more than 1000 requests per day per IP. [Email us](mailto:hi@trendapi.org) to get one.
|
80
|
+
|
81
|
+
If you have one, set `ENV["TREND_API_KEY"]` or use:
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
Trend.api_key = "secret"
|
85
|
+
```
|
86
|
+
|
67
87
|
## History
|
68
88
|
|
69
89
|
View the [changelog](https://github.com/ankane/trend/blob/master/CHANGELOG.md)
|
data/lib/trend/client.rb
CHANGED
@@ -1,13 +1,16 @@
|
|
1
|
+
require "trend/version"
|
2
|
+
|
1
3
|
module Trend
|
2
4
|
class Client
|
3
5
|
HEADERS = {
|
4
6
|
"Content-Type" => "application/json",
|
5
|
-
"Accept" => "application/json"
|
7
|
+
"Accept" => "application/json",
|
8
|
+
"User-Agent" => "trend-ruby/#{Trend::VERSION}"
|
6
9
|
}
|
7
10
|
|
8
|
-
def initialize(url: nil)
|
9
|
-
|
10
|
-
@uri = URI.parse(url)
|
11
|
+
def initialize(url: nil, api_key: nil)
|
12
|
+
@api_key = api_key || Trend.api_key
|
13
|
+
@uri = URI.parse(url || Trend.url)
|
11
14
|
@http = Net::HTTP.new(@uri.host, @uri.port)
|
12
15
|
@http.use_ssl = true if @uri.scheme == "https"
|
13
16
|
@http.open_timeout = 3
|
@@ -24,6 +27,11 @@ module Trend
|
|
24
27
|
Hash[resp["forecast"].map { |k, v| [parse_time(k), v] }]
|
25
28
|
end
|
26
29
|
|
30
|
+
def correlation(series, series2, params = {})
|
31
|
+
resp = make_request("correlation", series, params.merge(series2: series2))
|
32
|
+
resp["correlation"]
|
33
|
+
end
|
34
|
+
|
27
35
|
private
|
28
36
|
|
29
37
|
def make_request(path, series, params)
|
@@ -31,6 +39,8 @@ module Trend
|
|
31
39
|
series: series
|
32
40
|
}.merge(params)
|
33
41
|
|
42
|
+
path = "#{path}?#{URI.encode_www_form(api_key: @api_key)}" if @api_key
|
43
|
+
|
34
44
|
begin
|
35
45
|
response = @http.post("/#{path}", post_data.to_json, HEADERS)
|
36
46
|
rescue Errno::ECONNREFUSED, Timeout::Error => e
|
data/lib/trend/version.rb
CHANGED
data/lib/trend.rb
CHANGED
@@ -17,6 +17,10 @@ module Trend
|
|
17
17
|
client.forecast(*args)
|
18
18
|
end
|
19
19
|
|
20
|
+
def self.correlation(*args)
|
21
|
+
client.correlation(*args)
|
22
|
+
end
|
23
|
+
|
20
24
|
def self.url
|
21
25
|
@url ||= ENV["TREND_URL"] || "https://trendapi.org"
|
22
26
|
end
|
@@ -26,6 +30,15 @@ module Trend
|
|
26
30
|
@client = nil
|
27
31
|
end
|
28
32
|
|
33
|
+
def self.api_key
|
34
|
+
@api_key ||= ENV["TREND_API_KEY"]
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.api_key=(api_key)
|
38
|
+
@api_key = api_key
|
39
|
+
@client = nil
|
40
|
+
end
|
41
|
+
|
29
42
|
# private
|
30
43
|
def self.client
|
31
44
|
@client ||= Client.new
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: trend
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-10-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: benchmark-ips
|
@@ -74,6 +74,7 @@ extensions: []
|
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
76
|
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
77
78
|
- CHANGELOG.md
|
78
79
|
- Gemfile
|
79
80
|
- LICENSE.txt
|
@@ -103,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
103
104
|
version: '0'
|
104
105
|
requirements: []
|
105
106
|
rubyforge_project:
|
106
|
-
rubygems_version: 2.7.
|
107
|
+
rubygems_version: 2.7.7
|
107
108
|
signing_key:
|
108
109
|
specification_version: 4
|
109
110
|
summary: Ruby client for Trend, the time series API
|