Created
March 13, 2018 15:33
-
-
Save tiagomatos/a9b17387ca40a8643d86a32fbda72bfe to your computer and use it in GitHub Desktop.
Create Javascript App in a Jumpseller App
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# after the App is Authorized, the /callback route is requested. | |
# on the App itself, the /callback controller will: | |
# 1. confirm the App is authorized | |
# 2. save the Authorization tokens on the Database | |
# 3. create the Javascript App on the Store. | |
app.get '/callback' do | |
halt 403, 'Authorization denied' if authorization_denied? // App Authorization | |
store = find_or_create_app( | |
klass: model_name, | |
store_id: session[:store_id], | |
application_id: load_app_credentials(settings.name)['id'] | |
) | |
new_token = get_token_from(code: params[:code]) | |
# save tokens | |
save_extras( | |
store: store, | |
extras: { | |
access_token: new_token.token, | |
refresh_token: new_token.refresh_token, | |
expires_at: new_token.expires_at | |
} | |
) | |
# create the Javascript App | |
response = HTTParty.post("#{settings.api_host}/v1/jsapps.json", body: { | |
app: { | |
url: uri("/script/#{store.uuid}/jsapp.js"), | |
element: settings.app_element, | |
template: settings.app_template | |
} | |
}.to_json, headers: { | |
'Content-Type' => 'application/json', | |
'Accept' => 'application/json', | |
'Authorization' => "Bearer #{new_token.token}" | |
}) | |
halt 500 unless response.ok? | |
redirect uri('/') | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment