handiv 0.2.0 → 0.3.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: f58678c08db6603f91251ce8a0645ce01d8a0bd4
4
- data.tar.gz: ad14cb8eec09f79933f6f161dd30951c37ded766
3
+ metadata.gz: c7221625e188b1e35f787f6c6def31a3cd17e7a1
4
+ data.tar.gz: d218d25ac66a50ca574bae30769b325b5b0e358e
5
5
  SHA512:
6
- metadata.gz: 43622cb6cf8dda6bf4b30838f2759ba02ed7f941b3fa60e081c740a9441ea70c92cab1ee24e003743515e39665fbf319e97a38d387189f5b668eaaa5b9ce8344
7
- data.tar.gz: a599f185c6c39937db8c371aff97672ba36767d242e3c0c80e3a7a4381a79d59107cdc14948ecb7e2ca64375dabb3b127385b22b28637087359b2723855b53ab
6
+ metadata.gz: 17c8de51b76af37f6bd90c59269c805ba4c9dd4a216034da3aa30c369c4ab7f8e882631d4206413a7647b5a218afbc7d863699106aa26d016d0b5410a942c8d0
7
+ data.tar.gz: d7605d1d5e0b33045e85596e5826ff5c631ce550e01953bb38946c1d4a781a626657ef35edde76b0c7062d3b2a4a86e3d99bb1f716e6048223d704d5566e4f93
data/lib/handiv.rb CHANGED
@@ -1,27 +1,22 @@
1
1
  require 'handiv/version'
2
- require 'handiv/configuration'
3
- require 'handiv/logger'
4
2
  require 'handiv/connection'
5
-
6
- require 'handiv/tasks/reporter'
3
+ require 'handiv/client'
7
4
 
8
5
  module Handiv
9
6
  class << self
10
- def logger
11
- @logger ||= Logger.new
7
+ def client
8
+ @client ||= Handiv::Client.new
12
9
  end
13
10
 
14
- def configuration
15
- @configuration ||= Configuration.new
16
- end
11
+ private
17
12
 
18
- def configure
19
- yield(configuration) if block_given?
20
- true
13
+ def method_missing(method_name, *args, &block)
14
+ return super unless client.respond_to?(method_name)
15
+ client.send(method_name, *args, &block)
21
16
  end
22
17
 
23
- def report_task(task_id, options = {})
24
- Tasks::Reporter.new(task_id, options).report
18
+ def respond_to_missing?(method_name, include_private = false)
19
+ client.respond_to?(method_name, include_private)
25
20
  end
26
21
  end
27
22
  end
@@ -0,0 +1,48 @@
1
+ require 'handiv/configuration'
2
+ require 'handiv/logger'
3
+
4
+ require 'handiv/tasks/reporter'
5
+
6
+ module Handiv
7
+ class Client
8
+ attr_reader :options
9
+
10
+ def initialize(options = {})
11
+ @options = options
12
+
13
+ set_config_from_options
14
+ end
15
+
16
+ def logger
17
+ @logger ||= Logger.new(self)
18
+ end
19
+
20
+ def configuration
21
+ @configuration ||= Configuration.new
22
+ end
23
+ alias config configuration
24
+
25
+ def connection
26
+ Connection.new(self)
27
+ end
28
+
29
+ def configure
30
+ yield(configuration) if block_given?
31
+ true
32
+ end
33
+
34
+ def report_task(task_id, options = {})
35
+ Tasks::Reporter.new(self, task_id, options).report
36
+ end
37
+
38
+ private
39
+
40
+ def set_config_from_options
41
+ options.each do |key, value|
42
+ next unless configuration.respond_to?("#{key}=")
43
+
44
+ configuration.send("#{key}=", value)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -6,9 +6,10 @@ module Handiv
6
6
  class Connection
7
7
  USER_AGENT = "handiv-ruby/#{Handiv::VERSION}".freeze
8
8
 
9
- attr_reader :connection
9
+ attr_reader :client, :connection
10
10
 
11
- def initialize
11
+ def initialize(client)
12
+ @client = client
12
13
  @connection = set_connection
13
14
  end
14
15
 
@@ -18,7 +19,7 @@ module Handiv
18
19
  response = post_data(path, data)
19
20
  response.status
20
21
  rescue => e
21
- Handiv.logger.error "Failed to send a message (#{e.class} - #{e.message})"
22
+ client.logger.error "Failed to send a message (#{e.class} - #{e.message})"
22
23
  500 # Return status
23
24
  end
24
25
 
@@ -56,7 +57,7 @@ module Handiv
56
57
  end
57
58
 
58
59
  def config
59
- Handiv.configuration
60
+ client.config
60
61
  end
61
62
  end
62
63
  end
data/lib/handiv/logger.rb CHANGED
@@ -2,11 +2,13 @@ require 'logger'
2
2
 
3
3
  module Handiv
4
4
  class Logger
5
+ attr_reader :client
6
+
5
7
  LOG_PREFIX = '** [Handiv] '.freeze
6
8
  LOG_LEVELS = [:fatal, :error, :warn, :info, :debug].freeze
7
9
 
8
10
  LOG_LEVELS.each do |level|
9
- define_method(level)do |*args, &block|
11
+ define_method(level) do |*args, &block|
10
12
  msg = args[0] # Block-level default args is a 1.9 feature
11
13
  msg ||= block.call if block
12
14
 
@@ -14,6 +16,10 @@ module Handiv
14
16
  end
15
17
  end
16
18
 
19
+ def initialize(client)
20
+ @client = client
21
+ end
22
+
17
23
  def logger
18
24
  custom_logger || default_logger
19
25
  end
@@ -25,7 +31,7 @@ module Handiv
25
31
  end
26
32
 
27
33
  def custom_logger
28
- Handiv.configuration.logger
34
+ client.configuration.logger
29
35
  end
30
36
  end
31
37
  end
@@ -1,40 +1,36 @@
1
1
  module Handiv
2
2
  module Tasks
3
3
  class Reporter
4
- attr_reader :task_id, :options
4
+ attr_reader :client, :task_id, :options
5
5
 
6
- def initialize(task_id, options = {})
6
+ def initialize(client, task_id, options = {})
7
+ @client = client
7
8
  @task_id = task_id
8
9
  @options = options.is_a?(String) ? { message: options } : options
9
10
  end
10
11
 
11
12
  def report
12
- Handiv.logger.debug "Sending event about task #{task_id}"
13
+ client.logger.debug "Sending event about task #{task_id}"
13
14
 
14
- status = connection.send_data("tasks/#{task_id}", options)
15
+ status = client.connection.send_data("tasks/#{task_id}", options)
15
16
  log_status(status)
16
17
 
17
- status == 200 # Return success
18
+ status >= 200 && status < 300 # Return success
18
19
  end
19
20
 
20
21
  private
21
22
 
22
23
  def log_status(status)
23
- case status
24
- when 200
25
- Handiv.logger.debug "Successfully sent event about task #{task_id}"
26
- when 404
27
- Handiv.logger.error(
28
- "Failed to send event for #{task_id} task."
24
+ if status >= 200 && status < 300 # Return success
25
+ client.logger.debug "Successfully sent event about task #{task_id}"
26
+ elsif status == 404
27
+ client.logger.error(
28
+ "Task #{task_id} was not found with these credentials."
29
29
  )
30
30
  else
31
- Handiv.logger.error "Failed to send event about task #{task_id}"
31
+ client.logger.error "Failed to send event about task #{task_id}"
32
32
  end
33
33
  end
34
-
35
- def connection
36
- @connection ||= Connection.new
37
- end
38
34
  end
39
35
  end
40
36
  end
@@ -1,3 +1,3 @@
1
1
  module Handiv
2
- VERSION = '0.2.0'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: handiv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justas Palumickas
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-17 00:00:00.000000000 Z
11
+ date: 2016-11-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -100,6 +100,7 @@ files:
100
100
  - bin/setup
101
101
  - handiv.gemspec
102
102
  - lib/handiv.rb
103
+ - lib/handiv/client.rb
103
104
  - lib/handiv/configuration.rb
104
105
  - lib/handiv/connection.rb
105
106
  - lib/handiv/logger.rb
@@ -125,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
126
  version: '0'
126
127
  requirements: []
127
128
  rubyforge_project:
128
- rubygems_version: 2.5.1
129
+ rubygems_version: 2.5.2
129
130
  signing_key:
130
131
  specification_version: 4
131
132
  summary: A gem that provides a client interface for Handiv