Skip to content

Commit d2dbc21

Browse files
committed
Adds amount to params and moves auth
1 parent 5bd344c commit d2dbc21

File tree

4 files changed

+38
-16
lines changed

4 files changed

+38
-16
lines changed

app/controllers/api_controller.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
class ApiController < ApplicationController
2-
# #Auth goes here
2+
3+
def authed_session
4+
HTTParty.get('https://gist.github.com/freezepl/2a75c29c881982645156f5ccf8d1b139/')
5+
end
36
end

app/controllers/subscriptions_controller.rb

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,52 @@
11
class SubscriptionsController < ApiController
2+
3+
# GET /subscriptions/new
4+
def new
5+
@subscription = Subscription.new
6+
end
7+
8+
# POST /subscriptions
29
def create
3-
authed_session = HTTParty.get('https://gist.github.com/freezepl/2a75c29c881982645156f5ccf8d1b139')
4-
new_sub_request = HTTParty.get('https://gist.github.com/freezepl/2a75c29c881982645156f5ccf8d1b139/validate')
5-
new_sub_response = JSON.parse(new_sub_request)
6-
7-
if new_sub_response == response_ok
8-
subscription = Subscription.new(params)
9-
return 'subscription created and payment successful'
10-
elsif new_sub_response == response_fail
11-
return 'subscription not created due to insufficient funds'
12-
elsif new_sub_response == response_timeout
13-
sleep 15
14-
# do that stuff again
15-
elsif new_sub_response == response_503
16-
return 'Service Unavailable'
10+
if authed_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'
1727
end
1828
end
1929

30+
#GET /subscriptions
2031
def index
2132
subscriptions = Subscription.all
2233
render json: { status: 'SUCCESS', message: 'Loaded all subscriptions', data: subscriptions }, status: :ok
2334
end
2435

36+
# GET /subscriptions/1
2537
def show
2638
render json: { status: 'SUCCESS', message: 'Got your sub, bro', data: @subscription }, status: :ok
2739
end
2840

41+
# GET /subscriptions/user_id?{@user}
2942
def show_all_for_user
3043
render json: { status: 'SUCCESS', message: 'All subs for user', data: @user_subscriptions }, status: :ok
3144
end
3245

3346
private
3447

3548
def subscription_params
36-
params[:subscription].permit(:user_id, :paid, :billing_date, :cc_number, :cc_expiration, :cc_code)
49+
params[:subscription].permit(:user_id, :paid, :billing_date, :amount, :cc_number, :cc_expiration, :cc_code)
3750
end
3851

3952
def find_subscription

app/models/subscription.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def as_json
1010
user_id: user_id,
1111
paid: paid,
1212
billing_date: billing_date,
13+
amount: amount,
1314
cc_number: cc_number,
1415
cc_expiration: cc_expiration,
1516
cc_code: cc_code
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddAmountToSubscription < ActiveRecord::Migration[5.1]
2+
def change
3+
add_column :subscriptions, :amount, :integer
4+
end
5+
end

0 commit comments

Comments
 (0)