prospector 0.1.0 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9a26aca5cebe6252d5c7669e2a4623159a621460
4
- data.tar.gz: ba58266396be640a87b4bc86a9b1847df075d879
3
+ metadata.gz: 0eb069f50cbc32b8783bbc6a11a63f819859a12f
4
+ data.tar.gz: f82b2042f9f51fcdef5d19d7ab5b260d915659ec
5
5
  SHA512:
6
- metadata.gz: 94dd515c0dcc1f5f9cab83eb674ce9bcc51145ed270751b3ec092efa114d40a04e0db7d271838355ccc794fc5c88ad9dde88be3d5c83b140a1d974067899c2b3
7
- data.tar.gz: b3f3f8ca2c91c3416aafd63dc3fb0b8f90d63bcc0c414fd055bfb4e41999ec77bc5d4114e1e247bf513f94bd1d80b25ecd1b29029bd8bda5d4c95707eb0e94e5
6
+ metadata.gz: 123b0d0306cfc3096b34214c928a8c4af0dfff97e23717fe4cdf0bcac5c0e8d0b9897772e673042db1375a2db58e241f31128ff0e5a5eea41a8538ff2ed1b6cf
7
+ data.tar.gz: 8d6c93f8f60c9bf4be6ce75ad0a4c1a379d442c6d0ad5519d6b2c3738cef37be5869495b83459b0b004d34eba73602a2396d8b65cc9b10481cf8ab744710779d
data/.travis.yml CHANGED
@@ -1,3 +1,4 @@
1
1
  language: ruby
2
+ script: bundle exec rspec spec
2
3
  rvm:
3
4
  - 2.2.2
data/README.md CHANGED
@@ -33,12 +33,32 @@ or:
33
33
  Prospector.configure do |config|
34
34
  config.secret_token = 'token from service'
35
35
  config.client_secret = 'secret from service'
36
+ config.background_adapter = :sidekiq
37
+
38
+ config.enabled = Rails.env.production?
36
39
  end
37
40
  ```
38
41
 
39
42
  ## Usage
40
43
 
41
- Currently the process of notifying the service is not automated. In the future this will be provided via a rake task as well as a Rails integration.
44
+ ### Rails
45
+
46
+ When the gem is included into a Rails project, you have the option of specifying a background adapter to run the notification service asynchronously on. The default adapter is `ActiveJob` if available in your Rails project, but we also support `Sidekiq` at this time.
47
+
48
+ You can choose to enable Prospector in the initializer file, or by the presence of an ENV variable "PROSPECTOR_ENABLED" as shown below.
49
+
50
+ ```ruby
51
+ # set the ENV variable
52
+ # ENV['PROSPECTOR_ENABLED'] = 'true'
53
+ # or
54
+ Prospector.configure do |config|
55
+ config.enabled = Rails.env.production?
56
+ end
57
+ ```
58
+
59
+ ### Without Rails
60
+
61
+ Without Rails, you need to manually call the method to notify the Prospector service yourself.
42
62
 
43
63
  ```ruby
44
64
  Prospector.notify!
data/lib/prospector.rb CHANGED
@@ -5,6 +5,9 @@ require "prospector/version"
5
5
  require "prospector/client"
6
6
  require "prospector/configuration"
7
7
  require "prospector/error"
8
+ require "prospector/background"
9
+
10
+ require "prospector/railtie" if defined?(Rails)
8
11
 
9
12
  begin
10
13
  require "pry"
@@ -16,17 +19,29 @@ module Prospector
16
19
  attr_writer :configuration
17
20
  end
18
21
 
19
- def self.configuration
20
- @configuration ||= Configuration.new
21
- end
22
+ module ClassMethods
23
+ def configuration
24
+ @configuration ||= Configuration.new
25
+ end
22
26
 
23
- def self.configure
24
- yield(configuration)
25
- end
27
+ def configure
28
+ yield(configuration)
29
+ end
30
+
31
+ def enabled?
32
+ configuration.enabled?
33
+ end
26
34
 
27
- def self.notify!
28
- specifications = Bundler.environment.specs
35
+ def notify!
36
+ raise NotEnabledError unless enabled?
29
37
 
30
- Client.deliver(specifications)
38
+ configuration.notify!
39
+
40
+ specifications = Bundler.environment.specs
41
+
42
+ Client.deliver(specifications)
43
+ end
31
44
  end
45
+
46
+ extend ClassMethods
32
47
  end
@@ -0,0 +1,13 @@
1
+ require "prospector/background/coordinator"
2
+ require "prospector/background/notify_job" if defined?(ActiveJob::Base)
3
+ require "prospector/background/notify_worker" if defined?(Sidekiq::Worker)
4
+
5
+ module Prospector
6
+ module Background
7
+ def enqueue
8
+ Coordinator.new.enqueue
9
+ end
10
+
11
+ module_function :enqueue
12
+ end
13
+ end
@@ -0,0 +1,37 @@
1
+ module Prospector; module Background
2
+ class Coordinator
3
+ def enqueue
4
+ return unless Prospector.enabled?
5
+
6
+ case adapter_name
7
+ when :active_job then enqueue_via_active_job
8
+ when :sidekiq then enqueue_via_sidekiq
9
+ when :none then perform_immediately
10
+ else
11
+ raise UnsupportedAdapterError.new(adapter_name)
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def adapter_name
18
+ @adapter_name ||= Prospector.configuration.background_adapter
19
+ end
20
+
21
+ def enqueue_via_active_job
22
+ raise UnsupportedAdapterError unless defined?(NotifyJob)
23
+
24
+ NotifyJob.perform_later
25
+ end
26
+
27
+ def enqueue_via_sidekiq
28
+ raise UnsupportedAdapterError unless defined?(NotifyWorker)
29
+
30
+ NotifyWorker.perform_async
31
+ end
32
+
33
+ def perform_immediately
34
+ Prospector.notify!
35
+ end
36
+ end
37
+ end; end
@@ -0,0 +1,7 @@
1
+ module Prospector; module Background
2
+ class NotifyJob < ActiveJob::Base
3
+ def perform
4
+ Prospector.notify!
5
+ end
6
+ end
7
+ end; end
@@ -0,0 +1,9 @@
1
+ module Prospector; module Background
2
+ class NotifyWorker
3
+ include Sidekiq::Worker
4
+
5
+ def perform
6
+ Prospector.notify!
7
+ end
8
+ end
9
+ end; end
@@ -9,6 +9,7 @@ module Prospector
9
9
 
10
10
  case response.code
11
11
  when "401" then raise AuthenticationError
12
+ when "402" then raise AccountSubscriptionStatusError, json[:error]
12
13
  when "200" then return true
13
14
  else
14
15
  raise UnknownError
@@ -29,6 +30,10 @@ module Prospector
29
30
  @endpoint ||= URI('http://api.gemprospector.com/v1/specifications.json')
30
31
  end
31
32
 
33
+ def json
34
+ @json ||= JSON.parse(response.body)
35
+ end
36
+
32
37
  def build_request
33
38
  request = Net::HTTP::Post.new(endpoint)
34
39
  request['X-Auth-Token'] = Prospector.configuration.secret_token
@@ -1,6 +1,34 @@
1
1
  module Prospector
2
2
  class Configuration
3
- attr_writer :secret_token, :client_secret
3
+ attr_writer :client_secret,
4
+ :enabled,
5
+ :secret_token
6
+
7
+ def initialize
8
+ @notified = false
9
+ end
10
+
11
+ def enabled?
12
+ return @enabled unless @enabled.nil?
13
+
14
+ @enabled = ENV['PROSPECTOR_ENABLED'] == 'true'
15
+ end
16
+
17
+ def notify!
18
+ @notified = true
19
+ end
20
+
21
+ def notified?
22
+ @notified == true
23
+ end
24
+
25
+ def background_adapter
26
+ @background_adapter ||= background_adapter_from_env || determine_default_background_adapater
27
+ end
28
+
29
+ def background_adapter=(value)
30
+ @background_adapter = value.to_sym
31
+ end
4
32
 
5
33
  def secret_token
6
34
  @secret_token || ENV['PROSPECTOR_SECRET_TOKEN'] || raise(InvalidCredentialsError)
@@ -9,5 +37,20 @@ module Prospector
9
37
  def client_secret
10
38
  @client_secret || ENV['PROSPECTOR_CLIENT_SECRET'] || raise(InvalidCredentialsError)
11
39
  end
40
+
41
+ private
42
+
43
+ def background_adapter_from_env
44
+ return if ENV['PROSPECTOR_BACKGROUND_ADAPTER'].nil?
45
+
46
+ ENV['PROSPECTOR_BACKGROUND_ADAPTER'].to_sym
47
+ end
48
+
49
+ def determine_default_background_adapater
50
+ return :active_job if defined?(ActiveJob)
51
+ return :sidekiq if defined?(Sidekiq)
52
+
53
+ :none
54
+ end
12
55
  end
13
56
  end
@@ -2,5 +2,8 @@ module Prospector
2
2
  class Error < StandardError; end
3
3
  class AuthenticationError < Error; end
4
4
  class InvalidCredentialsError < Error; end
5
+ class AccountSubscriptionStatusError < Error; end
6
+ class NotEnabledError < Error; end
7
+ class UnsupportedAdapterError < Error; end
5
8
  class UnknownError < Error; end
6
9
  end
@@ -0,0 +1,9 @@
1
+ module Prospector
2
+ class Railtie < Rails::Railtie
3
+ initializer 'prospector.railtie' do |app|
4
+ ActiveSupport.on_load :action_controller do |app|
5
+ Background.enqueue
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module Prospector
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/prospector.gemspec CHANGED
@@ -23,5 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.add_dependency "bundler", "~> 1.10"
24
24
 
25
25
  spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "webmock", "~> 1.21"
26
27
  spec.add_development_dependency "vcr", "~> 2.9"
28
+ spec.add_development_dependency "rspec", "~> 3.3"
27
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prospector
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
  - Matthew Millsaps-Brewer
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-06-18 00:00:00.000000000 Z
11
+ date: 2015-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.21'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.21'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: vcr
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -66,6 +80,20 @@ dependencies:
66
80
  - - "~>"
67
81
  - !ruby/object:Gem::Version
68
82
  version: '2.9'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.3'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.3'
69
97
  description: Gem Prospector allows your team to stay on top of important updates to
70
98
  gems in your Ruby project.
71
99
  email:
@@ -85,9 +113,14 @@ files:
85
113
  - bin/console
86
114
  - bin/setup
87
115
  - lib/prospector.rb
116
+ - lib/prospector/background.rb
117
+ - lib/prospector/background/coordinator.rb
118
+ - lib/prospector/background/notify_job.rb
119
+ - lib/prospector/background/notify_worker.rb
88
120
  - lib/prospector/client.rb
89
121
  - lib/prospector/configuration.rb
90
122
  - lib/prospector/error.rb
123
+ - lib/prospector/railtie.rb
91
124
  - lib/prospector/version.rb
92
125
  - prospector.gemspec
93
126
  homepage: http://www.gemprospector.com