<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ryo Osawa on Medium]]></title>
        <description><![CDATA[Stories by Ryo Osawa on Medium]]></description>
        <link>https://medium.com/@rosawa?source=rss-20b6efc8c5b1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*WnQTZP-ljG_SoWOm.jpg</url>
            <title>Stories by Ryo Osawa on Medium</title>
            <link>https://medium.com/@rosawa?source=rss-20b6efc8c5b1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 11:06:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rosawa/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[BART Train Schedule Reminder]]></title>
            <link>https://medium.com/@rosawa/bart-train-schedule-reminder-d84849c5ce1b?source=rss-20b6efc8c5b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/d84849c5ce1b</guid>
            <dc:creator><![CDATA[Ryo Osawa]]></dc:creator>
            <pubDate>Fri, 17 Oct 2014 21:36:56 GMT</pubDate>
            <atom:updated>2014-10-18T01:49:09.840Z</atom:updated>
            <content:encoded><![CDATA[<p>If you’re a BART rider, don’t you think you’d want a little app that sends you a text message when your BART train is arriving at your nearest BART station? As part of MVP (Minium-Value-Product) project at Hack Reactor, I wrote a little Node.js app that does exactly that using Twilio and BART APIs.</p><p>If you know Javascript and want to know how to do that, please read on this blog post. Since this app is written in Node, you first need to install Node. If you don’t have Node.js installed in your computer, read <a href="http://nodejs.org/download/">this instruction</a> to install Node.js.</p><p>Once Node is installed in your computer, create a package.json to install all the dependent modules that your app needs. Create your own package.json and include ‘twilio’ and ‘xml2js’ modules.</p><p>Now all setups are done. Let’s first create a file to fetch real-time departure time using <a href="http://api.bart.gov/docs/overview/index.aspx">BART API</a>. BART provides a variety of APIs and the one we’re using is called <a href="http://api.bart.gov/docs/etd/etd.aspx">Real-time Information API</a>. Below is an example that fetches departure information at Powell station departing Northbound to Richmond station:</p><p><a href="http://api.bart.gov/api/etd.aspx?cmd=etd&amp;orig=RICH&amp;key=MW9S-E7SL-26DU-VV8V">http://api.bart.gov/api/etd.aspx?cmd=etd&amp;orig=RICH&amp;key=MW9S-E7SL-26DU-VV8V</a></p><p>And, here is the response to the above request. Notice that the response sent back from BART API is XML.</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt; <br>&lt;root&gt;<br> &lt;uri&gt;&lt;![CDATA[ http://api.bart.gov/api/etd.aspx?cmd=etd&amp;orig=RICH ]]&gt;&lt;/uri&gt;<br> &lt;date&gt;03/30/2011&lt;/date&gt; <br> &lt;time&gt;02:43:27 PM PDT&lt;/time&gt; <br> &lt;station&gt;<br> &lt;name&gt;Richmond&lt;/name&gt; <br> &lt;abbr&gt;RICH&lt;/abbr&gt; <br> &lt;etd&gt;<br> &lt;destination&gt;Fremont&lt;/destination&gt; <br> &lt;abbreviation&gt;FRMT&lt;/abbreviation&gt; <br> &lt;estimate&gt;<br> &lt;minutes&gt;5&lt;/minutes&gt; <br> &lt;platform&gt;2&lt;/platform&gt; <br> &lt;direction&gt;South&lt;/direction&gt; <br> &lt;length&gt;6&lt;/length&gt; <br> &lt;color&gt;ORANGE&lt;/color&gt; <br> &lt;hexcolor&gt;#ff9933&lt;/hexcolor&gt; <br> &lt;bikeflag&gt;1&lt;/bikeflag&gt; <br> &lt;/estimate&gt;<br> &lt;estimate&gt;<br> &lt;minutes&gt;20&lt;/minutes&gt; <br> &lt;platform&gt;2&lt;/platform&gt; <br> &lt;direction&gt;South&lt;/direction&gt; <br> &lt;length&gt;6&lt;/length&gt; <br> &lt;color&gt;ORANGE&lt;/color&gt; <br> &lt;hexcolor&gt;#ff9933&lt;/hexcolor&gt; <br> &lt;bikeflag&gt;1&lt;/bikeflag&gt; <br> &lt;/estimate&gt;<br> &lt;/etd&gt;<br> &lt;etd&gt;<br> &lt;destination&gt;Millbrae&lt;/destination&gt; <br> &lt;abbreviation&gt;MLBR&lt;/abbreviation&gt; <br> &lt;estimate&gt;<br> &lt;minutes&gt;Leaving&lt;/minutes&gt; <br> &lt;platform&gt;2&lt;/platform&gt; <br> &lt;direction&gt;South&lt;/direction&gt; <br> &lt;length&gt;10&lt;/length&gt; <br> &lt;color&gt;RED&lt;/color&gt; <br> &lt;hexcolor&gt;#ff0000&lt;/hexcolor&gt; <br> &lt;bikeflag&gt;1&lt;/bikeflag&gt; <br> &lt;/estimate&gt;<br> &lt;/etd&gt;<br> &lt;/station&gt;<br> &lt;message /&gt; <br>&lt;/root&gt;</pre><p>Now, here is a module called <strong>bart.js</strong> code that uses BART API and parses the response into JSON in Node.</p><pre>var qs = require(‘querystring’);<br>var parseString = require(‘xml2js’).parseString;<br>var request = require(‘request’);<br>var _ = require(‘underscore’);</pre><pre>var bartApiEndpoint = ‘<a href="http://api.bart.gov/api/etd.aspx?cmd=etd&amp;key=MW9S-E7SL-26DU-VV8V&amp;%27">http://api.bart.gov/api/etd.aspx?cmd=etd&amp;key=MW9S-E7SL-26DU-VV8V&amp;&#39;</a>;</pre><pre>var fetchDepartureInfo = function(orig, dir, dest, cb) {<br>  var querystring = qs.stringify({<br>  orig: orig,<br>  dir: dir<br>});</pre><pre>var url = bartApiEndpoint + querystring;<br>console.log(‘Sending a request to ‘, url);</pre><pre>request(url, function (err, resp, body) {<br> if (err) throw err;</pre><pre>parseString(body, function (err, json) {<br> var estimates = _.filter(json.root.station[0].etd, function (estimate) {<br>   if (estimate.abbreviation[0] === dest.toUpperCase()) {<br>     return estimate.estimate;<br>   }<br> });</pre><pre>if (estimates &amp;&amp; estimates[0] &amp;&amp; estimates[0].estimate) {<br>   estimates = estimates[0].estimate;<br>   var minutes = _.map(estimates, function (estimate) {<br>     return estimate.minutes[0];<br>   });</pre><pre>   cb(minutes);<br>}});<br>});<br>};</pre><pre>exports.fetchDepartureInfo = fetchDepartureInfo;</pre><p>Next, let’s create another module to send you a simple text message using Twilio SMS API. If you don’t have your Twilio account, you can create a trial account from <a href="https://www.twilio.com/">their site</a>. The overall signup process is very easy. After you verify your phone number, you’re good to go. To send a text message, you can simply make a POST request. Here is a module called twilio.js to send a text message.</p><pre>var _ = require(‘underscore’);<br>var accountSid = process.env.TWILIO_ACCOUNT_ID;<br>var authToken = process.env.AUTH_TOKEN;<br>var from = process.env.PHONE;</pre><pre>//require the Twilio module and create a REST client<br>var client = require(‘twilio’)(accountSid, authToken);</pre><pre>exports.sendText = function(to, dest, minutes, done) {</pre><pre>minutes = _.map(minutes, function(minute) {<br> return minute === ‘Leaving’ ? ‘now’ : minute;<br> });</pre><pre>var body = ‘Your train (‘ + dest + ‘) ‘ +<br> ‘is arriving at Powell station in [‘ +<br> minutes.join(‘, ‘) +<br> ‘] minutes.’;</pre><pre>var message = {<br> to: to,<br> from: from,<br> body: body<br>};</pre><pre>client.messages.create(message, function(err, message) {<br> if (err) {<br> console.error(err);<br> } else {<br> console.log(message.sid);<br> }<br> done();<br> });<br>};</pre><p>Using bart.js and twilio.js, you now can create a simple Node.js app to send you a quick reminder about your BART schedules. Here is the code that does this:</p><pre>var bart = require(‘./lib/bart’);<br>var twilio = require(‘./lib/twilio’);</pre><pre>bart.fetchDepartureInfo(job.station, job.direction, job.destination, function (estimates) {<br> twilio.sendText(job.phone, job.destination, estimates, done);<br>});</pre><p>There you have it. If you want to check out a fully working app, please check out <a href="https://github.com/ryojiosawa/bart-schedule-reminder">my repo</a> in GitHub. Thanks for reading my blog post.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d84849c5ce1b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Javascript Module Pattern]]></title>
            <link>https://medium.com/@rosawa/javascript-module-pattern-12ea1e917d0d?source=rss-20b6efc8c5b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/12ea1e917d0d</guid>
            <dc:creator><![CDATA[Ryo Osawa]]></dc:creator>
            <pubDate>Fri, 03 Oct 2014 02:28:39 GMT</pubDate>
            <atom:updated>2014-10-03T04:48:35.208Z</atom:updated>
            <content:encoded><![CDATA[<p><strong><em>Module Pattern</em></strong> is one of the common design patterns in Javascript and it’s used in many popular Javascript libraries. This design pattern can assist in encapsulating and organizing your code by hiding all the heavy lifting details and private states and only exposing the methods that other parts of your application use. I like the Module Pattern because it is very easy to implement and being able to define a class-like object that provides both private and public methods is a familiar programming concept that I’m very much used to.</p><p>Because Javascript does not offer access modifiers as part of its native language feature unlike some other programming languages like Java, C#, we need to use some techniques/features to create a module that works like a class in Javascript. Here are some key constructs that you need to use in order to define a module in Javascript:</p><ul><li>Immediately-invoked functional expression</li><li>Object literal as a return value</li><li>Closure</li></ul><h4>Define a Module</h4><p>To define a module, the first thing you need to is use an immediately-invoked functional expression like below.</p><pre>// myModule.js — define a namespace for myModule<br>var myModule = (function() {<br> // your code is defined here<br>}());</pre><p>The above code declares a simple skeleton module called <em>myModule</em>. Because Javascript supports the lexical scope, all variables and methods declared inside of this module will not be accessible from outside. Also, you can define a namespace by assigning the execution result of this expression to a variable so that you can reference it in your code — most likely in other Javascript files.</p><h4>Private Methods</h4><p>To expose your public methods or services, you need to define an object and use it as a return value. For example, if you want to expose two public methods in your module, you can do as follows:</p><pre>var myModule = (function() { </pre><pre> // your code is defined here</pre><pre> return {<br>   // <strong><em>Public methods are here</em></strong><br>   publicMethod1: function() { … },<br>   publicMethod2: function() }{ … }<br> };<br>}());</pre><p>Using the above example, if you want to invoke <em>publicMethod1</em> somewhere in your application code, you simply invoke like below.</p><pre>//app.js<br>myModule.publicMethod1();</pre><h4>Private Members and Methods</h4><p>For private members and methods, all you need to do is define them inside of your module function.</p><pre>var myModule = (function() {</pre><pre> // <strong><em>Private members</em></strong><br> privateMember1 = 0;<br> privateMember2 = [“bar”, “baz”];<br> <br> // <strong><em>Private methods</em></strong><br> privateMethod1 = function() { … };<br> privateMethod2 = function() { … }; </pre><pre> return {<br>   // Public methods are here<br>   publicMethod1: function() { … },<br>   publicMethod2: function() }{ … }<br> };<br>}());</pre><p>Because both public and private members/methods are declared in the same scope, public members can access to private members and methods. This way, you can invoke some internal private methods to carry out your public methods, which helps your code modularized and organized.</p><pre>var myModule = (function() {<br> // Private members<br> privateMember1 = 0;<br> privateMember2 = [“bar”, “baz”];<br> // Private methods<br> privateMethod1 = function() { … };<br> privateMethod2 = function() { … }; </pre><pre> return {<br>   // Public methods are here<br>   publicMethod1: function() {<br>   // <strong><em>Invoke private method</em></strong><br>     privateMember1++;<br>     return privateMetehod1(privateMember);<br>   },<br>   publicMethod2: function() }{ … }<br> };<br>}());</pre><p>One potential drawback of using the Module Pattern would be that you cannot write unit tests for your private methods as there is no way to access these methods outside of your module. However, it is often claimed that because these are private/internal methods, these public methods should not be unit-tested. Rather, you should focus on writing unit tests for your public methods.</p><h4>Advanced Techniques &amp; Module Pattern Variations</h4><p>Finally, because Module Pattern is very popular, there are some advanced techniques that you can use to utilize this design pattern. For example, if you want to import some other modules or reference Javascript libraries like jQuery or underscore inside your module, you can pass in those as an argument to your module like below.</p><pre>var myModule = (function() {<br> // Private members<br> privateMember1 = 0;<br> privateMember2 = [“bar”, “baz”]; </pre><pre> // Private methods<br> privateMethod1 = function() { <br>   var $p = $(‘p’); <strong><em>// Select all p nodes using jQuery</em></strong><br>   _.each($p, function(p) {<br>     <strong><em>// Iterate each p element using underscore’s each method</em></strong><br>   }); <br> };<br> privateMethod2 = function() { … }; </pre><pre> return {<br>   // Public methods are here<br>   publicMethod1: function() {<br>   // invoke private method<br>   privateMember1++;<br>     return privateMetehod1(privateMember);<br>   },<br>   publicMethod2: function() }{ … }<br> };<br>}(jQuery, _)); <strong><em>// Import jQuery and underscore libraries</em></strong></pre><p>Also, there is a variation of this pattern called <strong><em>Revealing Module Pattern</em></strong>, which essentially defines all public methods outside of your return object and include their references as a property. This way, the implementations of your public methods do not need to be there inside of your return object. Some people like this approach because it keeps your function code away from the return object.</p><pre>var myModule = (function() {<br> // Private members<br> privateMember1 = 0;<br> privateMember2 = [“bar”, “baz”]; <br> // Private methods<br> privateMethod1 = function() { … };<br> privateMethod2 = function() { … }; </pre><pre> // Public methods are defined here<br> publicMethod1: function() }{ … };<br> publicMethod2: function() }{ … };</pre><pre> return {<br> // Public methods are exposed here<br> publicMethod1: publicMethod1,<br> publicMethod2: publicMethod2<br> };<br>}());</pre><p>Hope this post helps you understand the Javascript Module Pattern and how to define it. If you’re interested in learning about advanced techniques using Module Pattern and other design patterns in Javascript, you should check out <a href="http://addyosmani.com/resources/essentialjsdesignpatterns/book/#modulepatternjavascript">this amazing site</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=12ea1e917d0d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[!! (Double-Not Operator)]]></title>
            <link>https://medium.com/@rosawa/double-not-operator-f1e9959c1a95?source=rss-20b6efc8c5b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/f1e9959c1a95</guid>
            <dc:creator><![CDATA[Ryo Osawa]]></dc:creator>
            <pubDate>Wed, 01 Oct 2014 00:27:50 GMT</pubDate>
            <atom:updated>2014-10-01T00:27:50.754Z</atom:updated>
            <content:encoded><![CDATA[<p>I’ve been learning some cool Javascript tricks lately. Using two <strong><em>“!”</em></strong> operators to convert a truthy/falsy value into an actual Boolean type is one of my favorites so far. For example, you can use this trick and reduce function to implement <strong><em>every</em></strong> function.</p><pre>var every = function(collection, predicate) {<br>  return collection.reduce(function(prev, item) {<br>   return !!prev || predicate(item);<br> });<br>};</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f1e9959c1a95" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>