F# Simple Twitter Update
Posted
by mroberts
on Geeks with Blogs
See other posts from Geeks with Blogs
or by mroberts
Published on Wed, 07 Apr 2010 07:15:17 GMT
Indexed on
2010/04/07
13:33 UTC
Read the original article
Hit count: 340
A short while ago I posted some code for a C# twitter update. I decided to move the same functionality / logic to F#. Here is what I came up with.
1: namespace Server.Actions
2:
3: open System
4: open System.IO
5: open System.Net
6: open System.Text
7:
8: type public TwitterUpdate() =
9:
10: //member variables
11: [<DefaultValue>] val mutable _body : string
12: [<DefaultValue>] val mutable _userName : string
13: [<DefaultValue>] val mutable _password : string
14:
15: //Properties
16: member this.Body with get() = this._body and set(value) = this._body <- value
17: member this.UserName with get() = this._userName and set(value) = this._userName <- value
18: member this.Password with get() = this._password and set(value) = this._password <- value
19:
20: //Methods
21: member this.Execute() =
22: let login = String.Format("{0}:{1}", this._userName, this._password)
23: let creds = Convert.ToBase64String(Encoding.ASCII.GetBytes(login))
24: let tweet = Encoding.ASCII.GetBytes(String.Format("status={0}", this._body))
25: let request = WebRequest.Create("http://twitter.com/statuses/update.xml") :?> HttpWebRequest
26:
27: request.Method <- "POST"
28: request.ServicePoint.Expect100Continue <- false
29: request.Headers.Add("Authorization", String.Format("Basic {0}", creds))
30: request.ContentType <- "application/x-www-form-urlencoded"
31: request.ContentLength <- int64 tweet.Length
32:
33: let reqStream = request.GetRequestStream()
34: reqStream.Write(tweet, 0, tweet.Length)
35: reqStream.Close()
36:
37: let response = request.GetResponse() :?> HttpWebResponse
38:
39: match response.StatusCode with
40: | HttpStatusCode.OK -> true
41: | _ -> false
While the above seems to work, it feels to me like it is not taking advantage of some functional concepts. Love to get some feedback as to how to make the above more “functional” in nature. For example, I don’t like the mutable properties.
© Geeks with Blogs or respective owner