klunk 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6414ef36d0b3146def7a1217ceae1134f0e02fbe
4
- data.tar.gz: 08c4f0c806ebbb3be0ef62bd30966f8b86fa6883
3
+ metadata.gz: 226a8c47401b226f655466a652c34cc9e22e7ca7
4
+ data.tar.gz: 850354d3be3af7dc57091ba6bc99c653a9d139ab
5
5
  SHA512:
6
- metadata.gz: 225e0c1d2fc3233f3a63fe31b4f5f4cd2ac0cf44725b0f15914eb416dec171e993297ee65daf01061c8c75c932a40b0f6628cbde6cf06327c8a634cd624fd763
7
- data.tar.gz: 2bec47db4358322c5139f826a1b9f439b1179e4c61d0cd30eba0654779aff2c42f39d7a90054927733048032bc2a4041b233132ef1e39f0b74f262b7f8da80ee
6
+ metadata.gz: 6e658745adc6d1ba083ef635b8d73630f1be77522460b583a1baaed4aa01588d64105927fd4d677e9b007aef4a3433f6f2d53ab33993db86807bd5c6fffc8c0f
7
+ data.tar.gz: b41ee1389d2f29eff22ce56b8a77b832b794c1b571cc05ddd8f5787c83a925904e8252211b94e36f8ac5660e18772fc5c0e98985f48ecb7012a65fa37c286052
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ script: bundle exec rspec spec
5
+ addons:
6
+ code_climate:
7
+ repo_token:
8
+ secure: ch4POH+GHn+wcMb3p78X0vaBiJzsHICq6zDVnnmS7FNSw6TQ4tCC+x1zr+PyNTinrj3yyP6v9tpfoWLpCkgCkV9hoKF+IwQVNmO1WLmH95msCGdZcQGqJdLYn725urQjpCinMW1UbrWNkijNLAZlu9cqGM6vUz6T+BJkwrOFAkn5Pzuo3weuNZupS9m86cAgQL1J6arsdP6TCRZJGyeb5wN2ZJ3XqMnRoL3dPeXFabILg82TaVOTOt5aC13tnmpcSJvompaxGsF3ywb0pK5X79hw4IidiQaXeRc7oSr9IE+IIBrknwawPsOWEKb/Ct78M8qpQIzRKL8AOOJB7wutTg7obRmpjKPvCSvNGPW2d+Y6UaiCWoY6MMYLHLqxQZLa1q8y3VykhDIXUJoeEI9wOaalmf2KVNKuL0Cn6b8V4j4z0ygm9b05dWEha19Nh1Xz+qrtH+9kNKYM7N3eSCdYEvNWMFfys9ce5rbEyv+QPS3+DYKb6Siwl/b6O7omQRoywTT0i1EbRl0xiZEk2RriZCkv6OM6J14rvTR7o8hqFpXvpU5w7ic+InOK94W2epwNTJZChqJRpmhIMDtlA7lGLV2w7T83NFgnru/jv3E+6GUPvRRwo87lf46Rt6xYqCUrkm/ThNUrsaafubXr/eoVFEPIlfSlXHAhM51zKtZJUUk=
data/Gemfile CHANGED
@@ -6,3 +6,7 @@ gemspec
6
6
  group :development, :test do
7
7
  gem 'rubocop', require: nil
8
8
  end
9
+
10
+ group :test do
11
+ gem 'codeclimate-test-reporter', require: nil
12
+ end
data/README.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Klunk
2
2
 
3
+ [![Build Status](https://travis-ci.org/kayaman/klunk.svg?branch=master)](https://travis-ci.org/kayaman/klunk)
4
+ [![Code Climate](https://codeclimate.com/github/kayaman/klunk/badges/gpa.svg)](https://codeclimate.com/github/kayaman/klunk)
5
+ [![Test Coverage](https://codeclimate.com/github/kayaman/klunk/badges/coverage.svg)](https://codeclimate.com/github/kayaman/klunk/coverage)
6
+
7
+
3
8
  ## Goal
4
9
 
5
10
  Create and setup __publishers__ (topics) and __subscribers__ (queues) for Rails applications.
data/klunk.gemspec CHANGED
@@ -20,10 +20,10 @@ Gem::Specification.new do |spec|
20
20
  spec.require_paths = ['lib']
21
21
 
22
22
  spec.add_runtime_dependency 'aws-sdk', '~> 2'
23
- spec.add_runtime_dependency 'safe_yaml'
24
- spec.add_runtime_dependency 'json'
23
+ spec.add_runtime_dependency 'safe_yaml', '~> 1.0.4'
24
+ spec.add_runtime_dependency 'json', '~> 2.0.2'
25
25
 
26
- spec.add_development_dependency 'bundler', '~> 1.13'
26
+ spec.add_development_dependency 'bundler', '~> 1.13.6'
27
27
  spec.add_development_dependency 'rake', '~> 10.0'
28
28
  spec.add_development_dependency 'rspec', '~> 3.0'
29
29
  end
data/lib/klunk/queue.rb CHANGED
@@ -20,13 +20,12 @@ module Klunk
20
20
  queue = client.create_queue(
21
21
  queue_name: name_for(queue_name),
22
22
  attributes: attributes
23
- )
23
+ )
24
24
  rescue Aws::SQS::Errors::QueueAlreadyExists
25
25
  puts "#{queue_name} already exists.".green
26
26
  queue = client.create_queue(queue_name: name_for(queue_name))
27
27
  end
28
28
  subscriptions.to_a.each do |subscription|
29
- queue_arn = attributes['QueueArn']
30
29
  topic_name = Topic.name_for(subscription[:name], subscription)
31
30
  topic = Topic.create(topic_name)
32
31
  ap Topic.subscribe(queue.queue_url, topic.topic_arn)
@@ -1,17 +1,49 @@
1
1
  namespace :klunk do
2
+
3
+ desc 'Bootstrap whole structure'
4
+ task bootstrap: :environment do
5
+ puts 'Bootstraping topics:'
6
+ Rake::Task['klunk:sns:create_topics'].invoke
7
+ puts 'Bootstraping queues:'
8
+ Rake::Task['klunk:sqs:create_queues_if_needed'].invoke
9
+ puts 'Current structure:'
10
+ Rake::Task['klunk:describe'].invoke
11
+ end
12
+
13
+ desc 'Describe current structure'
14
+ task describe: :environment do
15
+ Klunk::Topic::TOPICS.each do |topic|
16
+ ap Klunk::Topic.describe(topic[:name])
17
+ end
18
+ Klunk::Queue::QUEUES.each do |queue|
19
+ queue[:subscribes].to_a.each do |topic_options|
20
+ topic_name = topic_options.delete(:name)
21
+ ap Klunk::Topic.describe(topic_name, topic_options)
22
+ end
23
+ end
24
+ end
25
+
2
26
  namespace :sns do
3
27
  desc 'Create SNS topics'
4
28
  task create_topics: :environment do
5
29
  Klunk::Topic::TOPICS.each do |topic|
6
- puts Klunk::Topic.create(topic[:name])
30
+ Klunk::Topic.create(topic[:name])
7
31
  end
8
32
  end
9
33
  end
10
34
 
11
35
  namespace :sqs do
12
36
  desc 'Create SQS queues if needed'
13
- task create_sqs_queues_if_needed: :environment do
14
- # TODO: help me
37
+ task create_queues_if_needed: :environment do
38
+ existing_queues = Klunk::Queue.client.list_queues(queue_name_prefix: Klunk::Queue.name_for('')).queue_urls.collect{|queue| queue.split('/').last }
39
+ needed_queues = Klunk::Queue::QUEUES.map do |queue|
40
+ [Klunk::Queue.name_for(queue[:name]), Klunk::Queue.name_for(queue[:name], true)]
41
+ end.flatten
42
+ unless (needed_queues - existing_queues).empty?
43
+ Rake::Task["klunk:sqs:create_queues"].invoke
44
+ else
45
+ puts "\tQueues were set up. Skipping..."
46
+ end
15
47
  end
16
48
 
17
49
  desc 'Create SQS queues'
data/lib/klunk/topic.rb CHANGED
@@ -10,31 +10,44 @@ module Klunk
10
10
  end
11
11
 
12
12
  def create(topic_name)
13
- topic = client.create_topic(name: topic_name)
14
- puts "Topic: #{topic.topic_arn}".cyan
13
+ topic = client.create_topic(name: name_for(topic_name))
14
+ puts "Topic created: #{topic.topic_arn}".cyan
15
15
  topic
16
16
  end
17
17
 
18
+ def publish(topic_name, message)
19
+ topic_arn = topic_arn(topic_name)
20
+ puts "Publishing to #{topic_arn}: #{message}"
21
+ client.publish(topic_arn: topic_arn(topic_name), message: message)
22
+ end
23
+
24
+ def topic_arn(topic_name, options = {})
25
+ "arn:aws:sns:#{ENV['AWS_REGION']}:#{ENV['AWS_ACCOUNT_ID']}:#{name_for(topic_name, options)}"
26
+ end
27
+
18
28
  def subscribe(queue_url, topic_arn, previous_policy = nil)
19
29
  queue_attributes = Klunk::Queue.get_attributes(queue_url)
20
30
  queue_arn = queue_attributes['QueueArn']
21
31
  subscription = client.subscribe(
22
- topic_arn: topic_arn,
23
- protocol: 'sqs',
24
- endpoint: queue_arn)
32
+ topic_arn: topic_arn, protocol: 'sqs', endpoint: queue_arn
33
+ )
25
34
  client.set_subscription_attributes(
26
35
  subscription_arn: subscription.subscription_arn,
27
- attribute_name: 'RawMessageDelivery',
28
- attribute_value: 'true')
36
+ attribute_name: 'RawMessageDelivery', attribute_value: 'true'
37
+ )
29
38
  if queue_attributes.key?('Policy')
30
39
  previous_policy = JSON.parse(queue_attributes['Policy'])
31
40
  end
32
41
  add_policy(queue_url, topic_arn, previous_policy)
33
- topic_subscriptions = {
34
- topic: "#{topic_arn}",
42
+ end
43
+
44
+ def describe(topic_name, options = {})
45
+ puts topic_arn(topic_name, options)
46
+ {
47
+ topic: topic_arn(topic_name, options),
35
48
  subscriptions: client.list_subscriptions_by_topic(
36
- topic_arn: topic_arn
37
- ).subscriptions.map {|topic| topic[:endpoint] }
49
+ topic_arn: topic_arn(topic_name, options)
50
+ ).subscriptions.map { |topic| topic[:endpoint] }
38
51
  }
39
52
  end
40
53
 
@@ -44,8 +57,7 @@ module Klunk
44
57
  queue_url: queue_url,
45
58
  attributes: {
46
59
  Policy: previous_policy.tap do |p|
47
- p['Statement'] ||= []
48
- p['Statement'] << build_statement(queue_url, topic_arn)
60
+ (p['Statement'] ||= []) << build_statement(queue_url, topic_arn)
49
61
  p['Statement'].uniq!
50
62
  end.to_json
51
63
  }
@@ -55,7 +67,6 @@ module Klunk
55
67
  def build_statement(queue_url, topic_arn)
56
68
  queue_arn = Klunk::Queue.get_attributes(queue_url)['QueueArn']
57
69
  queue_name = queue_arn.split(':').last
58
- topic = client.get_topic_attributes(topic_arn: topic_arn)
59
70
  topic_name = topic_arn.split(':').last
60
71
  {
61
72
  'Sid': "#{queue_name.camelize}_Send_#{topic_name.camelize}",
data/lib/klunk/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Klunk
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.1.1'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: klunk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marco Antonio Gonzalez Junior
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-12-18 00:00:00.000000000 Z
12
+ date: 2016-12-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: aws-sdk
@@ -29,44 +29,44 @@ dependencies:
29
29
  name: safe_yaml
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - ">="
32
+ - - "~>"
33
33
  - !ruby/object:Gem::Version
34
- version: '0'
34
+ version: 1.0.4
35
35
  type: :runtime
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - ">="
39
+ - - "~>"
40
40
  - !ruby/object:Gem::Version
41
- version: '0'
41
+ version: 1.0.4
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: json
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
- - - ">="
46
+ - - "~>"
47
47
  - !ruby/object:Gem::Version
48
- version: '0'
48
+ version: 2.0.2
49
49
  type: :runtime
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
- - - ">="
53
+ - - "~>"
54
54
  - !ruby/object:Gem::Version
55
- version: '0'
55
+ version: 2.0.2
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: bundler
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
60
  - - "~>"
61
61
  - !ruby/object:Gem::Version
62
- version: '1.13'
62
+ version: 1.13.6
63
63
  type: :development
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
67
  - - "~>"
68
68
  - !ruby/object:Gem::Version
69
- version: '1.13'
69
+ version: 1.13.6
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: rake
72
72
  requirement: !ruby/object:Gem::Requirement
@@ -106,6 +106,7 @@ files:
106
106
  - ".gitignore"
107
107
  - ".rspec"
108
108
  - ".rubocop.yml"
109
+ - ".travis.yml"
109
110
  - Gemfile
110
111
  - README.md
111
112
  - Rakefile