The Ring programming language version 1.9 book - Part 54 of 210
1. Ring Documentation, Release 1.9
52.28 StyleFunctions Class
This class contains methods for adding CSS to the generated web page.
Like ScriptFunctions Class, The StyleFunctions class methods are merged to the Page class, so we can use the next
methods with page objects directly.
Method Parameters Description
StyleFloatLeft None Return float: left ;
StyleFloatRight None Return float: right ;
StyleSizeFull None Return width: 100% ; height: 100% ;
Stylecolor x Return ” color: ” + x + ” ; “
Stylebackcolor x Return ” background-color: ” + x + ” ;”
StyleTextCenter None Return “text-align: center ;”
StyleTextRight None Return “text-align: right ;”
StyleTextLeft None Return “text-align: left ;”
StyleSize x,y Return ” width: ” + x + ” ; height: ” + y + ” ;”
StyleWidth x Return ” width: ” + x + ” ;”
StyleHeight x Return ” height: ” + x + ” ;”
StyleTop x Return ” top: ” + x + ” ;”
StyleLeft x Return ” Left: ” + x + ” ;”
StylePos x,y Return ” top: ” + x + ” ;” + ” Left: ” + y + ” ;”
StyleHorizontalCenter None Return ” margin-right:auto ; margin-left:auto; “
StyleMarginTop x Return ” margin-top: ” + x + ” ;”
StyleMarginRight x Return ” margin-right: ” + x + ” ;”
StyleMarginLeft x Return ” margin-left: ” + x + ” ;”
StyleDivCenter nWidth,nHeight Create Div in the center of the page
StyleAbsolute None Return ” position:absolute ;”
StyleFixed None Return ” position:fixed ;”
StyleZIndex x Return ” z-index: ” + x + ” ;”
StyleFontSize x Return ” font-size: ” + x + ” ;”
StyleGradient x Generate Gradient (x values from 1 to 60)
StyleTable None Set table properties
StyleTableRows id Set different color to even and odd rows in the table
StyleTableNoBorder None Return ” border-style: none;”
52.29 WebPage Class
We use braces to access the active WebPage object attributes
Each one of these attribute will return a new object to access again using braces.
52.28. StyleFunctions Class 499
2. Ring Documentation, Release 1.9
Attribute Description
H1 Wraps HTML H1.
H2 Wraps HTML H2.
H3 Wraps HTML H3.
H4 Wraps HTML H4.
H5 Wraps HTML H5.
H6 Wraps HTML H6.
P Wraps HTML P.
Link Wraps HTML link.
NewLine Wraps HTML NewLine.
Div Wraps HTML Div.
Form Wraps HTML Form.
Input Wraps HTML Input.
TextArea Wraps HTML TextArea.
Select Wraps HTML Select.
Option Wraps HTML Option.
Image Wraps HTML Image.
UL Wraps HTML UL.
LI Wraps HTML LI.
Table Wraps HTML Table.
TR Wraps HTML TR.
TD Wraps HTML TD.
TH Wraps HTML TH.
Audio Wraps HTML Audio.
Video Wraps HTML Video.
Nav Wraps HTML Nav.
Span Wraps HTML Span.
Button Wraps HTML Button.
52.30 HtmlPage Class
The same as the WebPage class with the next changes
1. No output to the stdout
2. Provide the Output Method to get the output
Syntax:
output() ---> The output as string
52.30. HtmlPage Class 500
3. CHAPTER
FIFTYTHREE
DEPLOYING WEB APPLICATIONS IN THE CLOUD
In this chapter we will learn about deploying Ring Web Applications in the Cloud using Heroku
53.1 Introduction
We created a new project and tutorial to explain how to deploy Ring web applications in the Cloud using Heroku
Demo : http://testring.herokuapp.com/
Project : https://github.com/ring-lang/RingWebAppOnHeroku
Heroku Website : https://www.heroku.com/
53.2 Usage
To use this project and deploy it on Heroku
501
4. Ring Documentation, Release 1.9
1. Create Heroku account
2. Open your Heroku account and create new application
Example : testring
Note (You have to select a unique name for your application)
3. Open the command prompt, Create new folder : MyApp
md MyApp
4. Open the application folder
cd MyApp
5. Clone this projet using Git (Don’t forget the dot in the end to clone in the current directory)
git clone https://github.com/ring-lang/RingWebAppOnHeroku .
6. Login to Heroku (Enter your Email and Password)
heroku login
7. Add heroku (remote) to your Git project
change testring to your application name
heroku git:remote -a testring
8. Set the buildpacks (So Heroku can know how to support your project)
heroku buildpacks:add --index 1 https://github.com/ring-lang/heroku-buildpack-apt
heroku buildpacks:add --index 2 https://github.com/ring-lang/heroku-buildpack-ring
9. Now build your project and deploy it
git push heroku master
10. Test your project (In the browser)
heroku open
53.3 Ring source code files and permissions
To be able to run your new Ring scripts, Set the permission of the file to be executable using Git
For example, if you created a file : myscript.ring
git update-index --chmod=+x myscript.ring
git commit -m "Update file permission"
If you are using TortoiseGit, From windows explorer, select the file
Right click —> Properties —> Git —> Executable (+x)
Then commit and deploy!
53.3. Ring source code files and permissions 502
5. Ring Documentation, Release 1.9
53.4 Hello World program
file : ringapp/helloworld.ring
To run it : http://testring.herokuapp.com/ringapp/helloworld.ring
#!/app/runring.sh -cgi
see "content-type: text/html" +nl+nl
see "Hello, World!" + nl
file : ringapp/helloworld2.ring
To run it : http://testring.herokuapp.com/ringapp/helloworld2.ring
#!/app/runring.sh -cgi
load "weblib.ring"
import System.Web
new page {
text("Hello, World!")
}
53.5 Application Database
When you depoly the application, Everything will works directly!
No change is required, but in practice, You will need to update the next files to use your database
There are two scripts to interact with the database (We are using PostgreSQL in the cloud)
You will need to update the connection string in these files if you will use another database
• file: ringapp/database/newdb.ring (We run it using the browser for one time to create the tables)
• file: ringapp/datalib.ring (Class: Database)
In your practical projects, You can write better code (To be able to change the database)
Also you can create configuration file (To write the connection string in one place)
Database service : https://www.heroku.com/postgres
53.6 Deploying after updates
Just use Git and commit then push to heroku
file: build.bat contains the next commands for quick tests
git add .
git commit -m "Update RingWebAppOnHeroku"
git push heroku master
heroku open
53.4. Hello World program 503
6. Ring Documentation, Release 1.9
53.7 Local Tests
Local tests using Ring Notepad on Windows (Using local Apache Web Server)
Replace the first line in the file : ringapp/index.ring with
#!ring -cgi
Then run it from Ring Notepad (Ctrl+F6)
53.7. Local Tests 504
7. CHAPTER
FIFTYFOUR
USING RINGLIBCURL
In this chapter we will learn about using RingLibCurl
54.1 Get Request
Example:
load "libcurl.ring"
curl = curl_easy_init()
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1)
curl_easy_setopt(curl, CURLOPT_URL, "http://ring-lang.sf.net")
curl_easy_perform(curl)
curl_easy_cleanup(curl)
54.2 Post Request
Example:
load "libcurl.ring"
curl = curl_easy_init()
cPostThis = "page=4&Number1=4&Number2=5"
curl_easy_setopt(curl, CURLOPT_URL, "http://localhost/ringapp/index.ring?page=3")
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, cPostThis)
curl_easy_perform(curl)
curl_easy_cleanup(curl)
54.3 Facebook Login
Example:
505
8. Ring Documentation, Release 1.9
load "libcurl.ring"
see "Enter Email : " give $login_email
See "Enter Password : " give $login_pass
curl = curl_easy_init()
curl_easy_setopt(curl, CURLOPT_URL, 'https://www.facebook.com/login.php')
curl_easy_setopt(curl, CURLOPT_POSTFIELDS,'charset_test=j u s t a t e s t'+
' &email='+urlencode($login_email)+'&pass='+
urlencode($login_pass)+'&login=Login')
curl_easy_setopt(curl, CURLOPT_POST, 1)
curl_easy_setopt(curl, CURLOPT_HEADER, 0)
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1)
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookies.txt")
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookies.txt")
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U;"+
" Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3")
curl_easy_setopt(curl, CURLOPT_REFERER, "http://www.facebook.com")
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE)
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2)
mylist = curl_slist_append(NULL,'Accept-Charset: utf-8')
curl_slist_append(mylist,'Accept-Language: en-us,en;q=0.7,bn-bd;q=0.3')
curl_slist_append(mylist,'Accept: text/xml,application/xml,'+
'application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5')
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, mylist)
curl_easy_setopt(curl, CURLOPT_COOKIESESSION, false)
curl_easy_perform(curl)
curl_easy_cleanup(curl)
Func URLEncode cStr
cOut = ""
for x in cStr
if isalnum(x)
cOut += x
but x = " "
cOut += "+"
else
cOut += "%"+str2hex(x)
ok
next
return cOut
54.4 Save Output to String
Example:
load "libcurl.ring"
curl = curl_easy_init()
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1)
54.4. Save Output to String 506
9. Ring Documentation, Release 1.9
curl_easy_setopt(curl, CURLOPT_URL, "http://ring-lang.sf.net")
cOutput = curl_easy_perform_silent(curl)
See "Output:" + nl
see cOutput
curl_easy_cleanup(curl)
54.5 Get Stock Data From Yahoo
Example:
Load "libcurl.ring"
### Part 1 --- Get Crumb and Cookie -----------------------------------------
See "Start curl_easy_init(): "+ nl
curl = curl_easy_init() ### >>> HANDLE >>> 01006BD0 CURL 0
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1)
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookies.txt")
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookies.txt")
curl_easy_setopt(curl, CURLOPT_URL, "https://finance.yahoo.com/quote/AMZN/history")
### HTML Data >>> STDOUT Window, Use curl_easy_perform_silent >>> String
cOutput = curl_easy_perform_silent(curl) ### GO Get Data >>> String
### Extract Crumb from data
### "CrumbStore":{"crumb":"abcdefghijk"},
if cOutput != NULL
newStr1 = substr(cOutput, substr(cOutput, '"CrumbStore":{"crumb":"' ), 48 )
nPosS = substr(newStr1, ':"' ) ; ### Start of crumb -2
nPosE = substr(newStr1, '"}' ) ; ### End of crumb
nCount = nPosE - nPosS -2 ### size of crumb
myCrumb = substr(newStr1, nPosS +2, nCount)
See "myCrumb.: |"+ myCrumb +"|" +nl
### UniCode "u002F" replace it with "/"
if substr( myCrumb, "u002F")
myCrumb = substr( myCrumb, "u002F", "/")
See "myCrumb2: |"+ myCrumb +"|"+ nl
ok
else
See "No Connectivity to Yahoo. Looking for Cookie and Crumb." +nl +nl
ok
### Part 2 --- Send URL with Crumb, and Cookie -----------------------------------------
54.5. Get Stock Data From Yahoo 507
10. Ring Documentation, Release 1.9
### Send URL+Crumb to Yahoo to fetch 1st stock history data,
$url = "https://query1.finance.yahoo.com/v7/finance/download/AMZN"+
"?period1=1277856000&period2=1498777545&interval=1wk" +
"&events=history&crumb=" + myCrumb
curl_easy_setopt(curl, CURLOPT_URL, $url);
cStr = curl_easy_perform_silent(curl)
See cStr
curl_easy_cleanup(curl) ### REMEMBER to CLOSE CURL
Output:
myCrumb.: |sEEeW97mxvN|
Date,Open,High,Low,Close,Adj Close,Volume
2010-07-05,110.650002,117.480003,109.000000,117.260002,117.260002,21000400
2010-07-12,117.809998,124.879997,117.320000,118.489998,118.489998,29407300
2010-07-19,118.379997,121.250000,105.800003,118.870003,118.870003,74252100
54.5. Get Stock Data From Yahoo 508