Skip to content
This repository has been archived by the owner on Mar 21, 2020. It is now read-only.

Commit

Permalink
Get google pubsub working I think I can't test locally cause google e…
Browse files Browse the repository at this point in the history
…mulator won't play nicely with local dev
  • Loading branch information
coreyja committed Sep 8, 2018
1 parent b71032f commit 7d0c14e
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ bower.json

# Ignore pow environment settings
.powenv
.envrc

# Ignore Byebug command history file.
.byebug_history
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ gem 'simple_form'
gem 'omniauth'
gem 'omniauth-google-oauth2'

gem 'google-cloud-pubsub'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platform: :mri
Expand Down
47 changes: 47 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ GEM
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
addressable (2.5.2)
public_suffix (>= 2.0.2, < 4.0)
arel (8.0.0)
ast (2.4.0)
bcrypt (3.1.11)
Expand Down Expand Up @@ -73,6 +75,41 @@ GEM
geocoder (1.5.0)
globalid (0.4.1)
activesupport (>= 4.2.0)
google-cloud-core (1.2.3)
google-cloud-env (~> 1.0)
google-cloud-env (1.0.2)
faraday (~> 0.11)
google-cloud-pubsub (0.32.0)
concurrent-ruby (~> 1.0)
google-cloud-core (~> 1.2)
google-gax (~> 1.3)
grpc-google-iam-v1 (~> 0.6.9)
google-gax (1.3.0)
google-protobuf (~> 3.2)
googleapis-common-protos (>= 1.3.5, < 2.0)
googleauth (~> 0.6.2)
grpc (>= 1.7.2, < 2.0)
rly (~> 0.2.3)
google-protobuf (3.6.1)
googleapis-common-protos (1.3.7)
google-protobuf (~> 3.0)
googleapis-common-protos-types (~> 1.0)
grpc (~> 1.0)
googleapis-common-protos-types (1.0.2)
google-protobuf (~> 3.0)
googleauth (0.6.6)
faraday (~> 0.12)
jwt (>= 1.4, < 3.0)
memoist (~> 0.12)
multi_json (~> 1.11)
os (>= 0.9, < 2.0)
signet (~> 0.7)
grpc (1.14.2)
google-protobuf (~> 3.1)
googleapis-common-protos-types (~> 1.0.0)
grpc-google-iam-v1 (0.6.9)
googleapis-common-protos (>= 1.3.1, < 2.0)
grpc (~> 1.0)
hashie (3.5.7)
http-cookie (1.0.3)
domain_name (~> 0.5)
Expand All @@ -96,6 +133,7 @@ GEM
nokogiri (>= 1.5.9)
mail (2.7.0)
mini_mime (>= 0.1.1)
memoist (0.16.0)
method_source (0.8.2)
mime-types (3.1)
mime-types-data (~> 3.2015)
Expand Down Expand Up @@ -126,6 +164,7 @@ GEM
omniauth-oauth2 (1.5.0)
oauth2 (~> 1.1)
omniauth (~> 1.2)
os (1.0.0)
parallel (1.12.1)
parser (2.5.1.2)
ast (~> 2.4.0)
Expand All @@ -140,6 +179,7 @@ GEM
pry-remote (0.1.8)
pry (~> 0.9)
slop (~> 3.0)
public_suffix (3.0.3)
puma (3.12.0)
rack (2.0.5)
rack-test (1.1.0)
Expand Down Expand Up @@ -176,6 +216,7 @@ GEM
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
rly (0.2.3)
rubocop (0.58.2)
jaro_winkler (~> 1.5.1)
parallel (~> 1.10)
Expand All @@ -202,6 +243,11 @@ GEM
sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3)
signet (0.9.1)
addressable (~> 2.3)
faraday (~> 0.9)
jwt (>= 1.5, < 3.0)
multi_json (~> 1.10)
simple_form (3.5.0)
actionpack (> 4, < 5.2)
activemodel (> 4, < 5.2)
Expand Down Expand Up @@ -250,6 +296,7 @@ DEPENDENCIES
coffee-rails (~> 4.2)
easypost
geocoder
google-cloud-pubsub
jbuilder (~> 2.5)
jquery-rails
listen (~> 3.1.5)
Expand Down
23 changes: 23 additions & 0 deletions app/controllers/api/pubsub_messages_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module Api
class PubsubMessagesController < ApplicationController
skip_before_action :verify_authenticity_token, only: :create

def create
Rails.logger.info create_params
Rails.logger.info decoded_message
head :no_content
end

private

def decoded_message
Base64.decode64(create_params[:message][:data])
end

def create_params
params.permit(:subscription, message: %i[message_id data])
end
end
end
2 changes: 2 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

ENV['PUBSUB_SUB_NAME'] ||= 'package-tracker-pubsub-subscription'

module PackageTracker
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
Expand Down
2 changes: 2 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

ENV['APPLICATION_BASE_URL'] ||= 'https://package-tracker.dev'

Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.

Expand Down
2 changes: 2 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

ENV['APPLICATION_BASE_URL'] ||= 'https://tracker.coreyja.com'

Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.

Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace :api do
resources :easypost_events, only: %i(create)
resources :pubsub_messages, only: %i(create)
end

namespace :my do
Expand Down
22 changes: 22 additions & 0 deletions lib/tasks/pubsub.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

namespace :pubsub do
desc 'Create the Pub/Sub Subscription if it does not already exist'
task createSub: :environment do
require 'google/cloud/pubsub'

pubsub = Google::Cloud::Pubsub.new
topic = pubsub.topic ENV.fetch('PUBSUB_TOPIC')
endpoint = Rails.application.routes.url_helpers.api_pubsub_messages_url(host: ENV.fetch('APPLICATION_BASE_URL'))

subscription_name = ENV.fetch('PUBSUB_SUB_NAME')

sub = topic.subscription subscription_name

if sub.present?
sub.endpoint = endpoint
else
topic.subscribe subscription_name, endpoint: endpoint
end
end
end

0 comments on commit 7d0c14e

Please sign in to comment.