updown 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e2dbae809fc3ca16f30af62279401f4310eda7db
4
- data.tar.gz: 2f582ea04640723eac849cb8d5647c2d1d27a4e8
3
+ metadata.gz: cd938c29ae89b14996a0c327baa66cf3bdfdade3
4
+ data.tar.gz: ebdefb7d0cefab8592785555cafce000d956d1ad
5
5
  SHA512:
6
- metadata.gz: 9712a39c483a6dd62d10889519b436e3013218e5a57427ac2e80224113dbf3c75aea9083ff2be3b25fdcaf67d8f214daba49fc6e2bd09703b13272b6e5eb7cd0
7
- data.tar.gz: 7167145967a7e8b56104fd312badd76ed0f72ed91bca1685d05e2b0827817398f7a9262f33e07c57bf42e0cfea5bb10ad6ad4dfda326507cf2c9ba746bc1b812
6
+ metadata.gz: c07ae3f8c77b886a00b0a29d7a8eb0403d7a8f229de9af85b5ad5edbd52128796bf355016ac8a8418dfe90540405250c75237f532c57aa77e496135abf049933
7
+ data.tar.gz: eafc6788479953c7043341daccd8b368f220f5705c706d2bdc4010c200a226d2749a6c42042c2286ef7d7829a82a3f6baf5cc4f6fc3a3eaea99ebcfec056912a
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg
5
+ tmp
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Updown
2
2
 
3
- A wrapper for the Updown.io API
3
+ A Ruby wrapper and CLI for the [updown.io](https://updown.io) API
4
4
 
5
5
  ## Installation
6
6
 
@@ -10,15 +10,10 @@ Add this line to your application's Gemfile:
10
10
  gem 'updown'
11
11
  ```
12
12
 
13
- And then execute:
14
-
15
- $ bundle
16
-
17
13
  Or install it yourself as:
18
14
 
19
15
  $ gem install updown
20
16
 
21
-
22
17
  ## Configuration
23
18
 
24
19
  Set your API key
@@ -34,75 +29,85 @@ Updown.configuration.api_key = 'your_api_key'
34
29
 
35
30
  Or set the `UPDOWN_API_KEY` environment variable
36
31
 
32
+ Find your API key in your [settings page](https://updown.io/settings/edit).
33
+
37
34
  ## Usage
38
35
 
39
36
  List all your checks:
40
37
 
41
38
  ```ruby
42
39
  Updown::Check.all
40
+ # => [<Updown::Check>, <Updown::Check>, … ]
43
41
  ```
44
42
 
45
- See downtimes for a specific check:
43
+ List downtimes for a specific check (paginated, 100 per call):
46
44
 
47
45
  ```ruby
48
46
  check.downtimes
47
+ # => [<Updown::Downtime>, <Updown::Downtime>, … ]
48
+
49
+ check.downtimes page: 2
50
+ # => [<Updown::Downtime>]
49
51
  ```
50
52
 
51
53
  Create a new check:
52
54
 
53
55
  ```ruby
54
56
  Updown::Check.create 'https://google.com'
57
+ # => <Updown::Check>
55
58
  ```
56
59
 
57
- You can also set `period` and `published`:
60
+ You can also set any parameters allowed by the API, like `enabled`, `published`, `period` or `apdex_t`:
58
61
 
59
62
  ```ruby
60
63
  Updown::Check.create 'https://google.com', period: 30, published: true
64
+ # => <Updown::Check>
65
+ ```
66
+
67
+ In case of validation errors, an `Updown::Error` will be raised:
68
+
69
+ ```ruby
70
+ Updown::Check.create 'https://google.com', period: 45
71
+ # => Updown::Error: URL is already registered (given: "https://google.com"), Period is not included in the list (given: 45)
61
72
  ```
62
73
 
63
74
  Update a specific check:
64
75
 
65
76
  ```ruby
66
77
  check.update period: 30
78
+ # => <Updown::Check>
67
79
  ```
68
80
 
69
- See more details about the options here: https://updown.io/api
70
-
71
81
  Delete a specific check:
72
82
 
73
83
  ```ruby
74
84
  check.destroy
85
+ # => true
75
86
  ```
76
87
 
77
- ## Command Line
88
+ Learn more about the API here: https://updown.io/api
78
89
 
79
- This gem also comes with a few commands
90
+ ## Command Line
80
91
 
92
+ This gem also comes with an `updown` shell command.
81
93
  First, configure your API key:
82
94
 
83
- ```
84
- $ updown configure YOUR_API_KEY
85
- ```
95
+ $ updown configure YOUR_API_KEY
86
96
 
87
97
  See the status of your checks:
88
98
 
89
- ```
90
- $ updown status
91
- [up] https://google.com
92
- [up] https://bing.com
93
- ```
99
+ $ updown status
100
+ [up] rjis https://google.com
101
+ [DOWN] bn94 — https://bing.com
102
+ [up] qwer — http://stackoverflow.com
94
103
 
95
104
  Add a new check:
96
105
 
97
- ```
98
- $ updown add https://google.com
99
- ```
100
-
106
+ $ updown add https://google.com
101
107
 
102
108
  ## Todo
103
109
 
104
110
  - Write tests!
105
- - Error handling
106
111
 
107
112
  ## Contributing
108
113
 
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
- require 'bundler/gem_tasks'
1
+ require 'bundler/gem_tasks'
2
+
3
+ desc "Open an irb session preloaded with updown gem"
4
+ task :console do
5
+ sh "irb -rubygems -I lib -r updown.rb"
6
+ end
data/lib/updown/call.rb CHANGED
@@ -1,28 +1,39 @@
1
+ require 'json'
2
+
1
3
  module Updown
2
- class Call
4
+ Error = Class.new StandardError
5
+
6
+ module Call
3
7
 
4
8
  def self.resource
5
9
  RestClient::Resource.new 'https://updown.io/api/', headers: { 'X-API-KEY' => Updown.configuration.api_key }
6
10
  end
7
11
 
8
12
  def self.checks
9
- JSON.parse Call.resource['checks'].get
13
+ process { Call.resource['checks'].get }
10
14
  end
11
15
 
12
- def self.downtimes(token)
13
- JSON.parse Call.resource["checks/#{token}/downtimes"].get
16
+ def self.downtimes(token, filters={})
17
+ process { Call.resource["checks/#{token}/downtimes"].get(params: filters) }
14
18
  end
15
19
 
16
20
  def self.create_check(attributes={})
17
- JSON.parse Call.resource['checks'].post(attributes)
21
+ process { Call.resource['checks'].post(attributes) }
18
22
  end
19
23
 
20
24
  def self.update_check(token, attributes={})
21
- JSON.parse Call.resource["checks/#{token}"].put(attributes)
25
+ process { Call.resource["checks/#{token}"].put(attributes) }
22
26
  end
23
27
 
24
28
  def self.destroy_check(token)
25
- JSON.parse Call.resource["checks/#{token}"].delete
29
+ process { Call.resource["checks/#{token}"].delete }
30
+ end
31
+
32
+ def self.process
33
+ JSON.parse yield
34
+ rescue RestClient::BadRequest, RestClient::Unauthorized, RestClient::ResourceNotFound => e
35
+ result = (JSON.parse(e.response) rescue {})
36
+ raise Updown::Error.new(result['error'] || e.reponse)
26
37
  end
27
38
 
28
39
  end
data/lib/updown/check.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Updown
2
2
  class Check
3
- attr_accessor :token, :enabled, :url, :period, :apdex_t, :published, :uptime, :down, :error, :down_since, :last_check_at, :next_check_at
3
+ attr_accessor :token, :url, :last_status, :uptime, :down, :down_since, :error, :period, :apdex_t, :enabled, :published, :last_check_at, :next_check_at, :ssl_tested_at, :ssl_valid, :ssl_error
4
4
 
5
5
  def self.all
6
6
  Updown::Call.checks.map do |check|
@@ -8,36 +8,41 @@ module Updown
8
8
  end
9
9
  end
10
10
 
11
- def self.create(url, period: 60, published: false)
12
- Check.new Updown::Call.create_check(url: url, period: period, published: published)
11
+ def self.create(url, attributes = {})
12
+ Check.new Updown::Call.create_check(attributes.merge(url: url))
13
13
  end
14
14
 
15
15
  def initialize(json)
16
16
  @token = json['token']
17
- @enabled = json['enabled']
18
17
  @url = json['url']
18
+ @last_status = json['last_status']
19
+ @enabled = json['enabled']
19
20
  @period = json['period']
20
21
  @apdex_t = json['apdex_t']
21
22
  @published = json['published']
22
23
  @uptime = json['uptime']
23
24
  @down = json['down']
24
25
  @error = json['error']
25
- @down_since = json['down_since']
26
- @last_check_at = json['last_check_at']
27
- @next_check_at = json['next_check_at']
26
+ @down_since = Time.parse(json['down_since']) if json['down_since']
27
+ @last_check_at = Time.parse(json['last_check_at']) if json['last_check_at']
28
+ @next_check_at = Time.parse(json['next_check_at']) if json['next_check_at']
29
+ if ssl = json['ssl']
30
+ @ssl_tested_at = Time.parse(ssl['tested_at']) if ssl['tested_at']
31
+ @ssl_valid = ssl['valid']
32
+ @ssl_error = ssl['error']
33
+ end
28
34
  end
29
35
 
30
- def downtimes
31
- Downtime.find(@token)
36
+ def downtimes page: 1
37
+ Downtime.find(@token, page: page)
32
38
  end
33
39
 
34
40
  def update(attributes={})
35
- Check.new Updown::Call.update_check(@token, url: url, period: period, published: published)
41
+ Check.new Updown::Call.update_check(@token, attributes)
36
42
  end
37
43
 
38
44
  def destroy
39
- Updown::Call.destroy_check(@token)
40
- nil
45
+ Updown::Call.destroy_check(@token)['deleted']
41
46
  end
42
47
  end
43
48
  end
data/lib/updown/cli.rb CHANGED
@@ -1,3 +1,7 @@
1
+ require 'colorize'
2
+
3
+ String.disable_colorization(true) if not STDOUT.isatty
4
+
1
5
  module Updown
2
6
  class CLI < Thor
3
7
 
@@ -6,9 +10,24 @@ module Updown
6
10
  configure_api_key
7
11
 
8
12
  Updown::Check.all.each do |check|
9
- status = check.down ? 'DOWN' : 'up'
10
- puts "[#{status}] #{check.url}"
13
+ status = if !check.enabled
14
+ ' ---- '.colorize(:light_black)
15
+ elsif check.down
16
+ '[DOWN]'.colorize(:light_red)
17
+ else
18
+ ' [up] '.colorize(:light_green)
19
+ end
20
+ url = if check.ssl_valid == true
21
+ check.url.sub('https', 'https'.colorize(:light_green))
22
+ elsif check.ssl_valid == false
23
+ check.url.sub('https', 'https'.colorize(:light_red))
24
+ else
25
+ check.url
26
+ end
27
+ puts "#{status} #{check.token.colorize(:light_magenta)} #{"—".colorize(:light_black)} #{url}"
11
28
  end
29
+ rescue Updown::Error => e
30
+ puts "Error: #{e}"
12
31
  end
13
32
 
14
33
  desc 'add URL [PERIOD]', 'add a new check'
@@ -17,6 +36,8 @@ module Updown
17
36
 
18
37
  check = Updown::Check.create url, period: period
19
38
  system "open https://updown.io/#{check.token}"
39
+ rescue Updown::Error => e
40
+ puts "Error: #{e}"
20
41
  end
21
42
 
22
43
  desc 'configure API_KEY', 'set your updown.io api key'
@@ -2,16 +2,16 @@ module Updown
2
2
  class Downtime
3
3
  attr_accessor :error, :started_at, :ended_at, :duration
4
4
 
5
- def self.find(token)
6
- Updown::Call.downtimes(token).map do |downtime|
5
+ def self.find(token, page: 1)
6
+ Updown::Call.downtimes(token, page: page).map do |downtime|
7
7
  Downtime.new downtime
8
8
  end
9
9
  end
10
10
 
11
11
  def initialize(json)
12
12
  @error = json['error']
13
- @started_at = json['started_at']
14
- @ended_at = json['ended_at']
13
+ @started_at = Time.parse(json['started_at']) if json['started_at']
14
+ @ended_at = Time.parse(json['ended_at']) if json['ended_at']
15
15
  @duration = json['duration']
16
16
  end
17
17
 
@@ -1,3 +1,3 @@
1
1
  module Updown
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/updown.rb CHANGED
@@ -11,5 +11,5 @@ module Updown
11
11
  with_configuration do
12
12
  has :api_key, classes: String, default: ENV['UPDOWN_API_KEY'].to_s
13
13
  end
14
-
14
+
15
15
  end
data/updown.gemspec CHANGED
@@ -6,10 +6,10 @@ require 'updown/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "updown"
8
8
  spec.version = Updown::VERSION
9
- spec.authors = ["Aske Hansen"]
10
- spec.email = ["aske@deeco.dk"]
11
- spec.summary = %q{Updown.io wrapper}
12
- spec.description = %q{A wrapper for the Updown.io API}
9
+ spec.authors = ["Aske Hansen", "Adrien Jarthon"]
10
+ spec.email = ["aske@deeco.dk", "me@adrienjarthon.com"]
11
+ spec.summary = %q{updown.io API wrapper}
12
+ spec.description = %q{A wrapper for the updown.io API}
13
13
  spec.homepage = "https://github.com/askehansen/updown-ruby"
14
14
  spec.license = "MIT"
15
15
 
@@ -23,6 +23,6 @@ Gem::Specification.new do |spec|
23
23
 
24
24
  spec.add_dependency 'rest-client'
25
25
  spec.add_dependency 'thor'
26
-
27
- spec.add_runtime_dependency 'gem_config'
26
+ spec.add_dependency 'gem_config'
27
+ spec.add_dependency 'colorize'
28
28
  end
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: updown
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aske Hansen
8
+ - Adrien Jarthon
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2015-03-27 00:00:00.000000000 Z
12
+ date: 2015-05-14 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: bundler
@@ -80,16 +81,31 @@ dependencies:
80
81
  - - ">="
81
82
  - !ruby/object:Gem::Version
82
83
  version: '0'
83
- description: A wrapper for the Updown.io API
84
+ - !ruby/object:Gem::Dependency
85
+ name: colorize
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :runtime
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ description: A wrapper for the updown.io API
84
99
  email:
85
100
  - aske@deeco.dk
101
+ - me@adrienjarthon.com
86
102
  executables:
87
103
  - updown
88
104
  extensions: []
89
105
  extra_rdoc_files: []
90
106
  files:
107
+ - ".gitignore"
91
108
  - Gemfile
92
- - Gemfile.lock
93
109
  - LICENSE.txt
94
110
  - README.md
95
111
  - Rakefile
@@ -121,8 +137,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
137
  version: '0'
122
138
  requirements: []
123
139
  rubyforge_project:
124
- rubygems_version: 2.4.5
140
+ rubygems_version: 2.2.2
125
141
  signing_key:
126
142
  specification_version: 4
127
- summary: Updown.io wrapper
143
+ summary: updown.io API wrapper
128
144
  test_files: []
145
+ has_rdoc:
data/Gemfile.lock DELETED
@@ -1,27 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- updown (0.1.0)
5
- gem_config
6
- rest-client
7
- thor
8
-
9
- GEM
10
- remote: https://rubygems.org/
11
- specs:
12
- gem_config (0.3.1)
13
- mime-types (2.4.3)
14
- netrc (0.10.3)
15
- rake (10.4.2)
16
- rest-client (1.7.3)
17
- mime-types (>= 1.16, < 3.0)
18
- netrc (~> 0.7)
19
- thor (0.19.1)
20
-
21
- PLATFORMS
22
- ruby
23
-
24
- DEPENDENCIES
25
- bundler (~> 1.7)
26
- rake (~> 10.0)
27
- updown!