Skip to content

Commit 455f718

Browse files
authored
Merge pull request #16 from NiceMLT/newrspec2
Updates rspec and creates requester class
2 parents b54d937 + 945049a commit 455f718

File tree

10 files changed

+112
-41
lines changed

10 files changed

+112
-41
lines changed

Gemfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ gem 'sdoc', '~> 0.4.0', group: :doc
3434
gem 'httparty'
3535

3636
group :development, :test do
37-
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
38-
gem 'rspec'
37+
gem 'sinatra'
38+
gem 'rspec-rails', '~> 3.7'
3939
end
4040

4141
group :development do

Gemfile.lock

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ GEM
8585
minitest (5.11.3)
8686
multi_json (1.13.1)
8787
multi_xml (0.5.5)
88+
mustermann (1.0.2)
8889
nio4r (2.3.0)
8990
nokogiri (1.8.2)
9091
mini_portile2 (~> 2.3.0)
@@ -93,6 +94,8 @@ GEM
9394
pg (0.18.1)
9495
powerpack (0.1.1)
9596
rack (2.0.4)
97+
rack-protection (2.0.1)
98+
rack
9699
rack-test (0.6.3)
97100
rack (>= 1.0)
98101
rails (5.1.0)
@@ -124,10 +127,6 @@ GEM
124127
rb-inotify (0.9.10)
125128
ffi (>= 0.5.0, < 2)
126129
rdoc (4.3.0)
127-
rspec (3.7.0)
128-
rspec-core (~> 3.7.0)
129-
rspec-expectations (~> 3.7.0)
130-
rspec-mocks (~> 3.7.0)
131130
rspec-core (3.7.1)
132131
rspec-support (~> 3.7.0)
133132
rspec-expectations (3.7.0)
@@ -136,6 +135,14 @@ GEM
136135
rspec-mocks (3.7.0)
137136
diff-lcs (>= 1.2.0, < 2.0)
138137
rspec-support (~> 3.7.0)
138+
rspec-rails (3.7.2)
139+
actionpack (>= 3.0)
140+
activesupport (>= 3.0)
141+
railties (>= 3.0)
142+
rspec-core (~> 3.7.0)
143+
rspec-expectations (~> 3.7.0)
144+
rspec-mocks (~> 3.7.0)
145+
rspec-support (~> 3.7.0)
139146
rspec-support (3.7.1)
140147
rubocop (0.32.1)
141148
astrolabe (~> 1.3)
@@ -158,6 +165,11 @@ GEM
158165
sdoc (0.4.2)
159166
json (~> 1.7, >= 1.7.7)
160167
rdoc (~> 4.0)
168+
sinatra (2.0.1)
169+
mustermann (~> 1.0)
170+
rack (~> 2.0)
171+
rack-protection (= 2.0.1)
172+
tilt (~> 2.0)
161173
spring (2.0.2)
162174
activesupport (>= 4.2)
163175
sprockets (3.7.1)
@@ -196,10 +208,11 @@ DEPENDENCIES
196208
jquery-rails
197209
pg (~> 0.18)
198210
rails (= 5.1.0)
199-
rspec
211+
rspec-rails (~> 3.7)
200212
rubocop
201213
sass-rails (~> 5.0)
202214
sdoc (~> 0.4.0)
215+
sinatra
203216
spring
204217
turbolinks
205218
uglifier (>= 1.3.0)

app/controllers/subscriptions_controller.rb

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
11
class SubscriptionsController < ApiController
22

3+
before_action :create_session, only: %i[create]
4+
35
# GET /subscriptions/new
46
def new
57
@subscription = Subscription.new
68
end
79

810
# POST /subscriptions
911
def create
10-
if authed_payment_session
11-
new_sub_request = HTTParty.get('https://gist.github.com/freezepl/2a75c29c881982645156f5ccf8d1b139/validate')
12-
new_sub_response = JSON.parse(new_sub_request)
13-
14-
if new_sub_response == response_ok
15-
subscription = Subscription.new(subscription_params)
16-
return 'subscription created and payment successful'
17-
elsif new_sub_response == response_fail
18-
return 'subscription not created due to insufficient funds'
19-
elsif new_sub_response == response_timeout
20-
sleep 15
21-
# do that stuff again
22-
elsif new_sub_response == response_503
23-
return 'Service Unavailable'
24-
end
25-
else
26-
return 'Invalid session'
12+
requester = Requester.new
13+
if requester.paid?
14+
subscription = Subscription.new(subscription_params)
15+
return 'subscription created and payment successful'
16+
elsif requester.insufficient_funds?
17+
return 'subscription not created due to insufficient funds'
18+
elsif requester.error?
19+
sleep 15
20+
# retry
2721
end
2822
end
2923

@@ -45,6 +39,10 @@ def show_all_for_user
4539

4640
private
4741

42+
def create_session
43+
HTTParty.get('https://gist.github.com/freezepl/2a75c29c881982645156f5ccf8d1b139/')
44+
end
45+
4846
def subscription_params
4947
params[:subscription].permit(:user_id, :paid, :billing_date, :amount, :cc_number, :cc_expiration, :cc_code)
5048
end

app/models/subscription.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Subscription < ActiveRecord::Base
22

33
has_many :subscriptions
44

5-
validates :cc_number, format: { with: ^(?:4[0-9]{12}(?:[0-9]{3})?|[25][1-7][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$, on: :create }
5+
# validates :cc_number, format: { with: ^(?:4[0-9]{12}(?:[0-9]{3})?|[25][1-7][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$, on: :create }
66

77
def as_json
88
{

bin/fake-server

100644100755
File mode changed.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class ChangeIntegersToStrings < ActiveRecord::Migration[5.1]
2+
def change
3+
change_column :subscriptions, :cc_number, :string
4+
change_column :subscriptions, :cc_expiration, :string
5+
change_column :subscriptions, :cc_code, :string
6+
end
7+
end

db/schema.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 20180405124010) do
13+
ActiveRecord::Schema.define(version: 20180405164805) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
@@ -20,9 +20,9 @@
2020
t.string "paid"
2121
t.datetime "billing_date"
2222
t.integer "cost"
23-
t.integer "cc_number"
24-
t.integer "cc_expiration"
25-
t.integer "cc_code"
23+
t.string "cc_number"
24+
t.string "cc_expiration"
25+
t.string "cc_code"
2626
t.datetime "created_at"
2727
t.datetime "updated_at"
2828
t.string "payment_id"

spec/models/subscription_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1+
require 'rails_helper'
2+
13
RSpec.describe Subscription, :type => :model do
24

3-
before(:each) do
4-
@user = User.create(name: "Steve NoJobs", email: "steve@nojobs.com")
5-
@subscription = Subscription.create(user_id: user.id, paid: true, billing_date: "March 1, 2018", cc_number: 4011100110011001, cc_expiration: 0519, cc_code: 0123)
6-
end
5+
let(:user) { User.create!(name: "Steve NoJobs", email: "steve@nojobs.com") }
6+
let(:subscription) { Subscription.create!(user_id: user.id, paid: true, billing_date: "March 1, 2018", cc_number: "4011100110011001", cc_expiration: "0519", cc_code: "0123") }
77

88
it "should know it's billing_date" do
99
expect(subscription.billing_date).to eq("March 1, 2018")
1010
end
1111

1212
it "should know it's cc_number" do
13-
expect(subscription.cc_number).to eq(4011100110011001)
13+
expect(subscription.cc_number).to eq("4011100110011001")
1414
end
1515

1616
it "should know it's cc_expiration" do
17-
expect(subscription.cc_number).to eq(0519)
17+
expect(subscription.cc_expiration).to eq("0519")
1818
end
1919

2020
it "should know it's cc_code" do
21-
expect(subscription.cc_number).to eq(0123)
21+
expect(subscription.cc_code).to eq("0123")
2222
end
2323
end

spec/rails_helper.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# This file is copied to spec/ when you run 'rails generate rspec:install'
2+
require 'spec_helper'
3+
ENV['RAILS_ENV'] ||= 'test'
4+
require File.expand_path('../../config/environment', __FILE__)
5+
# Prevent database truncation if the environment is production
6+
abort("The Rails environment is running in production mode!") if Rails.env.production?
7+
require 'rspec/rails'
8+
# Add additional requires below this line. Rails is not loaded until this point!
9+
10+
# Requires supporting ruby files with custom matchers and macros, etc, in
11+
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
12+
# run as spec files by default. This means that files in spec/support that end
13+
# in _spec.rb will both be required and run as specs, causing the specs to be
14+
# run twice. It is recommended that you do not name files matching this glob to
15+
# end with _spec.rb. You can configure this pattern with the --pattern
16+
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
17+
#
18+
# The following line is provided for convenience purposes. It has the downside
19+
# of increasing the boot-up time by auto-requiring all files in the support
20+
# directory. Alternatively, in the individual `*_spec.rb` files, manually
21+
# require only the support files necessary.
22+
#
23+
# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
24+
25+
# Checks for pending migrations and applies them before tests are run.
26+
# If you are not using ActiveRecord, you can remove this line.
27+
ActiveRecord::Migration.maintain_test_schema!
28+
29+
RSpec.configure do |config|
30+
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
31+
config.fixture_path = "#{::Rails.root}/spec/fixtures"
32+
33+
# If you're not using ActiveRecord, or you'd prefer not to run each of your
34+
# examples within a transaction, remove the following line or assign false
35+
# instead of true.
36+
config.use_transactional_fixtures = true
37+
38+
# RSpec Rails can automatically mix in different behaviours to your tests
39+
# based on their file location, for example enabling you to call `get` and
40+
# `post` in specs under `spec/controllers`.
41+
#
42+
# You can disable this behaviour by removing the line below, and instead
43+
# explicitly tag your specs with their type, e.g.:
44+
#
45+
# RSpec.describe UsersController, :type => :controller do
46+
# # ...
47+
# end
48+
#
49+
# The different available types are documented in the features, such as in
50+
# https://relishapp.com/rspec/rspec-rails/docs
51+
config.infer_spec_type_from_file_location!
52+
53+
# Filter lines from Rails gems in backtraces.
54+
config.filter_rails_from_backtrace!
55+
# arbitrary gems may also be filtered via:
56+
# config.filter_gems_from_backtrace("gem name")
57+
end

spec/spec_helper.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# This file was generated by the `rspec --init` command. Conventionally, all
1+
# This file was generated by the `rails generate rspec:install` command. Conventionally, all
22
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
33
# The generated `.rspec` file contains `--require spec_helper` which will cause
44
# this file to always be loaded, without a need to explicitly require it in any
@@ -66,10 +66,6 @@
6666
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
6767
config.disable_monkey_patching!
6868
69-
# This setting enables warnings. It's recommended, but in some cases may
70-
# be too noisy due to issues in dependencies.
71-
config.warnings = true
72-
7369
# Many RSpec users commonly either run the entire suite or an individual
7470
# file, and it's useful to allow more verbose output when running an
7571
# individual spec file.

0 commit comments

Comments
 (0)