treat 2.0.4 → 2.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,393 @@
1
+ <!doctype html>
2
+
3
+ <!--[if lt IE 7 ]> <html lang="en" class="ie6"> <![endif]-->
4
+ <!--[if IE 7 ]> <html lang="en" class="ie7"> <![endif]-->
5
+ <!--[if IE 8 ]> <html lang="en" class="ie8"> <![endif]-->
6
+ <!--[if IE 9 ]> <html lang="en" class="ie9"> <![endif]-->
7
+ <!--[if (gt IE 9)|!(IE)]><!--> <html lang="en" class="no-js"> <!--<![endif]-->
8
+
9
+ <head>
10
+ <meta charset="utf-8">
11
+ <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
12
+ <title>Net::HTTP Cheat Sheet</title>
13
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
14
+ <link rel="icon" href="/favicon.png" type="image/png">
15
+ <link rel="shortcut icon" href="/favicon.png" type="image/png">
16
+ <link rel="alternate" type="application/rss+xml" title="Ruby Inside" href="http://www.rubyinside.com/feed/" />
17
+ <link rel="stylesheet" href="http://www.rubyinside.com/wp-content/themes/ri2011/css/ri.css">
18
+ <!-- script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script -->
19
+ <script type="text/javascript" src="http://use.typekit.com/dmj7czx.js"></script>
20
+ <script type="text/javascript">try{Typekit.load();}catch(e){}</script>
21
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
22
+
23
+ <!--[if lt IE 9]>
24
+ <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
25
+ <![endif]-->
26
+ </head>
27
+
28
+ <body class="single single-post postid-2940 single-format-standard">
29
+ <div id="superheader">Want to stay on top? <a href="http://rubyweekly.com/?m">Ruby Weekly</a> is a once-weekly e-mail newsletter covering the latest Ruby and Rails news.</div>
30
+ <div id="container">
31
+ <div class="outerheader top">
32
+ <div class="right"><form method="get" id="searchform" action="/"><input type="text" value="" name="s" id="s" size="24" /><input type="submit" id="searchsubmit" value="Search" /></form>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="http://www.rubyinside.com/feed/"><img src="http://feeds2.feedburner.com/~fc/RubyInside?bg=99CCFF&fg=333333&anim=0" align="top" alt="Feed Icon" /></a></div>
33
+ <a href="/"><img src="http://www.rubyinside.com/wp-content/themes/ri2011/css/logo.png" id="logo" alt="Ruby Inside - A Ruby Blog" /></a>
34
+ </div>
35
+
36
+ <div class="outerheader masthead">
37
+ <div class="menu">
38
+ <ul>
39
+ <li><a href="/" class="home">Home</a></li> <li><a href="/about/">About</a></li>
40
+ <li><a href="/archives/">Archives</a></li>
41
+ <!-- li><a href="/advertise/">Advertising</a></li -->
42
+ <li><a href="http://jobs.rubyinside.com/">Ruby and Rails Jobs</a></li>
43
+ <li class="right"><a href="/feed/">RSS</a> <!-- span class="count">(24146)</span --></li>
44
+ <li class="right">
45
+ </li>
46
+ <li class="right"><a href="http://twitter.com/rubyinside">Follow us on Twitter</a> <!-- span class="count">(5548)</span --></li>
47
+ </ul>
48
+ </div>
49
+
50
+
51
+ </div>
52
+
53
+ <div id="innercontainer"><div id="page">
54
+
55
+
56
+ <div id="sidebar">
57
+ <div style="margin-top: 12px"><a href="http://twitter.com/RubyInside" class="twitter-follow-button">Follow @RubyInside</a>
58
+ <script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script></div>
59
+ <div id="execphp-3" class="widget-container section widget_execphp">
60
+ <div class="execphpwidget"> <h3><a href="http://jobs.rubyinside.com/">Ruby and Rails Jobs</a></h3>
61
+ <div class="inner">
62
+ <ul>
63
+ <!-- ? readfile('http://www.rubyinside.com/jobs.html'); ? -->
64
+ <li><div class='link'><a href="http://jobs.rubyinside.com/a/jbb/job-details/847083">SOFTWARE DEVELOPER - RUBY</a></div><div class='company'>Openbay</div><div class="location">Cambridge, Massachusetts</div></li>
65
+ <li><div class='link'><a href="http://jobs.rubyinside.com/a/jbb/job-details/825538">Ruby Developer</a></div><div class='company'>HouseTrip</div><div class="location">London, United Kingdom</div></li>
66
+ <li><div class='link'><a href="http://jobs.rubyinside.com/a/jbb/job-details/831595">Software Engineer</a></div><div class='company'>Goodreads</div><div class="location">San Francisco, California</div></li>
67
+ <li><div class='link'><a href="http://jobs.rubyinside.com/a/jbb/job-details/831800">Software Engineer</a></div><div class='company'>Samasource</div><div class="location">San Francisco, California</div></li>
68
+ <li><div class='link'><a href="http://jobs.rubyinside.com/a/jbb/job-details/845628">Ruby on Rails Developer</a></div><div class='company'>emuze</div><div class="location">Boston, Massachusetts</div></li>
69
+ <li><div class='link'><a href="http://jobs.rubyinside.com/a/jbb/job-details/837571">Ruby on Rails Senior Developer</a></div><div class='company'>Berkeley Software Systems</div><div class="location">Sunnyvale, California</div></li>
70
+ <li><div class='link'><a href="http://jobs.rubyinside.com/a/jbb/job-details/831198">Software Engineer</a></div><div class='company'>Nextpoint</div><div class="location">Madison, Wisconsin</div></li>
71
+ <li><div class='link'><a href="http://jobs.rubyinside.com/a/jbb/job-details/840691">Ruby on Rails Web Application Developer - 100% telecommute</a></div><div class='company'>DRH Internet Inc.</div><div class="location">Schneider, Indiana</div></li>
72
+ <div class="minor"><a href="http://ruby.jobamatic.com/">More jobs &raquo;</a> : <a href="/post-a-job">Post a Job</a></div>
73
+ </div>
74
+ </div>
75
+ </div><div id="text-3" class="widget-container section widget_text"> <div class="textwidget"><!-- a href="http://owningrails.com/?ref=5905208113"><img src="http://owningrails.com/images/ad-with.png" /></a --><!-- a href="http://rubyweekly.com/"><img src="http://rubyinside.com/wp-content/themes/ri20102/images/rw-ri-box.gif" /></a --><!-- a href="https://cooperpress.com/19walkthrough"><img src="/images/ruby19wt2.gif" /></a --><!-- a href="https://cooperpress.com/rubyreloaded"><img src="http://rubyinside.com/images/reloaded-riad.png" /></a --></div>
76
+ </div>
77
+
78
+ <!-- if (is_front_page() && !is_paged()) { -->
79
+ </div>
80
+
81
+ <div id="main">
82
+
83
+
84
+
85
+ <div class="post-2940 post type-post status-publish format-standard hentry category-reference" id="post-2940">
86
+
87
+ <div class="content">
88
+
89
+ <div class="title"><h2>Net::HTTP Cheat Sheet</h2></div>
90
+
91
+ <p class="author">By <a href="http://www.rubyinside.com/author/admin" title="View all posts by Peter Cooper">Peter Cooper</a> <span class="date">/ January 16, 2010</span></p>
92
+ <div id="thecontent"><p><img src="http://www.rubyinside.com/wp-content/uploads/2010/01/http-is-a-hamster-on-rollerblades-says-marc-andre-cournoyer.gif" width="120" height="120" alt="http-is-a-hamster-on-rollerblades-says-marc-andre-cournoyer.gif" style="float:left; margin-right:12px; margin-bottom:12px; border:1px #000000 solid;" />Norwegian Rubyist <a href="http://august.lilleaas.net/">August Lilleaas</a> has been busy putting together <a href="http://github.com/augustl/net-http-cheat-sheet">a ton of examples</a> of using the <a href="http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html">Net::HTTP</a> Ruby library that comes with most Ruby distributions. I asked him if it'd be okay to put some of them directly on Ruby Inside for reference purposes and he said "No problem!"</p>
93
+ <p>It's worth noting that Net::HTTP has been superseded in many areas by libraries like John Nunemaker's <a href="http://github.com/jnunemaker/httparty">HTTParty</a> and Paul DIx's high performance <a href="http://github.com/pauldix/typhoeus">Typhoeus</a>, but as part of the standard library, Net::HTTP is still a popular option though it doesn't have the easiest API to remember.</p>
94
+ <p>Here's a selection of August's examples for some of the most common operations. Want to see <i>all</i> of the examples and follow any updates made to them? Check out August's <a href="http://github.com/augustl/net-http-cheat-sheet">net-http-cheat-sheet GitHub repo</a>.</p>
95
+ <h3>Standard HTTP Request</h3>
96
+ <pre><span class="ident">require</span> <span class="punct">"</span><span class="string">net/http</span><span class="punct">"</span>
97
+ <span class="ident">require</span> <span class="punct">"</span><span class="string">uri</span><span class="punct">"</span>
98
+
99
+ <span class="ident">uri</span> <span class="punct">=</span> <span class="constant">URI</span><span class="punct">.</span><span class="ident">parse</span><span class="punct">("</span><span class="string">http://google.com/</span><span class="punct">")</span>
100
+
101
+ <span class="comment"># Shortcut</span>
102
+ <span class="ident">response</span> <span class="punct">=</span> <span class="constant">Net</span><span class="punct">::</span><span class="constant">HTTP</span><span class="punct">.</span><span class="ident">get_response</span><span class="punct">(</span><span class="ident">uri</span><span class="punct">)</span>
103
+
104
+ <span class="comment"># Will print response.body</span>
105
+ <span class="constant">Net</span><span class="punct">::</span><span class="constant">HTTP</span><span class="punct">.</span><span class="ident">get_print</span><span class="punct">(</span><span class="ident">uri</span><span class="punct">)</span>
106
+
107
+ <span class="comment"># Full</span>
108
+ <span class="ident">http</span> <span class="punct">=</span> <span class="constant">Net</span><span class="punct">::</span><span class="constant">HTTP</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">uri</span><span class="punct">.</span><span class="ident">host</span><span class="punct">,</span> <span class="ident">uri</span><span class="punct">.</span><span class="ident">port</span><span class="punct">)</span>
109
+ <span class="ident">response</span> <span class="punct">=</span> <span class="ident">http</span><span class="punct">.</span><span class="ident">request</span><span class="punct">(</span><span class="constant">Net</span><span class="punct">::</span><span class="constant">HTTP</span><span class="punct">::</span><span class="constant">Get</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">uri</span><span class="punct">.</span><span class="ident">request_uri</span><span class="punct">))</span></pre>
110
+ <h3>Basic Auth</h3>
111
+ <pre><span class="ident">require</span> <span class="punct">"</span><span class="string">net/http</span><span class="punct">"</span>
112
+ <span class="ident">require</span> <span class="punct">"</span><span class="string">uri</span><span class="punct">"</span>
113
+
114
+ <span class="ident">uri</span> <span class="punct">=</span> <span class="constant">URI</span><span class="punct">.</span><span class="ident">parse</span><span class="punct">("</span><span class="string">http://google.com/</span><span class="punct">")</span>
115
+
116
+ <span class="ident">http</span> <span class="punct">=</span> <span class="constant">Net</span><span class="punct">::</span><span class="constant">HTTP</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">uri</span><span class="punct">.</span><span class="ident">host</span><span class="punct">,</span> <span class="ident">uri</span><span class="punct">.</span><span class="ident">port</span><span class="punct">)</span>
117
+ <span class="ident">request</span> <span class="punct">=</span> <span class="constant">Net</span><span class="punct">::</span><span class="constant">HTTP</span><span class="punct">::</span><span class="constant">Get</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">uri</span><span class="punct">.</span><span class="ident">request_uri</span><span class="punct">)</span>
118
+ <span class="ident">request</span><span class="punct">.</span><span class="ident">basic_auth</span><span class="punct">("</span><span class="string">username</span><span class="punct">",</span> <span class="punct">"</span><span class="string">password</span><span class="punct">")</span>
119
+ <span class="ident">response</span> <span class="punct">=</span> <span class="ident">http</span><span class="punct">.</span><span class="ident">request</span><span class="punct">(</span><span class="ident">request</span><span class="punct">)</span></pre>
120
+ <h3>Dealing with response objects</h3>
121
+ <pre><span class="ident">require</span> <span class="punct">"</span><span class="string">net/http</span><span class="punct">"</span>
122
+ <span class="ident">require</span> <span class="punct">"</span><span class="string">uri</span><span class="punct">"</span>
123
+
124
+ <span class="ident">uri</span> <span class="punct">=</span> <span class="constant">URI</span><span class="punct">.</span><span class="ident">parse</span><span class="punct">("</span><span class="string">http://google.com/</span><span class="punct">")</span>
125
+
126
+ <span class="ident">http</span> <span class="punct">=</span> <span class="constant">Net</span><span class="punct">::</span><span class="constant">HTTP</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">uri</span><span class="punct">.</span><span class="ident">host</span><span class="punct">,</span> <span class="ident">uri</span><span class="punct">.</span><span class="ident">port</span><span class="punct">)</span>
127
+ <span class="ident">request</span> <span class="punct">=</span> <span class="constant">Net</span><span class="punct">::</span><span class="constant">HTTP</span><span class="punct">::</span><span class="constant">Get</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">uri</span><span class="punct">.</span><span class="ident">request_uri</span><span class="punct">)</span>
128
+
129
+ <span class="ident">response</span> <span class="punct">=</span> <span class="ident">http</span><span class="punct">.</span><span class="ident">request</span><span class="punct">(</span><span class="ident">request</span><span class="punct">)</span>
130
+
131
+ <span class="ident">response</span><span class="punct">.</span><span class="ident">code</span> <span class="comment"># => 301</span>
132
+ <span class="ident">response</span><span class="punct">.</span><span class="ident">body</span> <span class="comment"># => The body (HTML, XML, blob, whatever)</span>
133
+ <span class="comment"># Headers are lowercased</span>
134
+ <span class="ident">response</span><span class="punct">["</span><span class="string">cache-control</span><span class="punct">"]</span> <span class="comment"># => public, max-age=2592000</span></pre>
135
+ <h3>POST form request</h3>
136
+ <pre><span class="ident">require</span> <span class="punct">"</span><span class="string">net/http</span><span class="punct">"</span>
137
+ <span class="ident">require</span> <span class="punct">"</span><span class="string">uri</span><span class="punct">"</span>
138
+
139
+ <span class="ident">uri</span> <span class="punct">=</span> <span class="constant">URI</span><span class="punct">.</span><span class="ident">parse</span><span class="punct">("</span><span class="string">http://example.com/search</span><span class="punct">")</span>
140
+
141
+ <span class="comment"># Shortcut</span>
142
+ <span class="ident">response</span> <span class="punct">=</span> <span class="constant">Net</span><span class="punct">::</span><span class="constant">HTTP</span><span class="punct">.</span><span class="ident">post_form</span><span class="punct">(</span><span class="ident">uri</span><span class="punct">,</span> <span class="punct">{"</span><span class="string">q</span><span class="punct">"</span> <span class="punct">=></span> <span class="punct">"</span><span class="string">My query</span><span class="punct">",</span> <span class="punct">"</span><span class="string">per_page</span><span class="punct">"</span> <span class="punct">=></span> <span class="punct">"</span><span class="string">50</span><span class="punct">"})</span>
143
+
144
+ <span class="comment"># Full control</span>
145
+ <span class="ident">http</span> <span class="punct">=</span> <span class="constant">Net</span><span class="punct">::</span><span class="constant">HTTP</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">uri</span><span class="punct">.</span><span class="ident">host</span><span class="punct">,</span> <span class="ident">uri</span><span class="punct">.</span><span class="ident">port</span><span class="punct">)</span>
146
+
147
+ <span class="ident">request</span> <span class="punct">=</span> <span class="constant">Net</span><span class="punct">::</span><span class="constant">HTTP</span><span class="punct">::</span><span class="constant">Post</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">uri</span><span class="punct">.</span><span class="ident">request_uri</span><span class="punct">)</span>
148
+ <span class="ident">request</span><span class="punct">.</span><span class="ident">set_form_data</span><span class="punct">({"</span><span class="string">q</span><span class="punct">"</span> <span class="punct">=></span> <span class="punct">"</span><span class="string">My query</span><span class="punct">",</span> <span class="punct">"</span><span class="string">per_page</span><span class="punct">"</span> <span class="punct">=></span> <span class="punct">"</span><span class="string">50</span><span class="punct">"})</span>
149
+
150
+ <span class="ident">response</span> <span class="punct">=</span> <span class="ident">http</span><span class="punct">.</span><span class="ident">request</span><span class="punct">(</span><span class="ident">request</span><span class="punct">)</span></pre>
151
+ <h3>File upload - input type="file" style</h3>
152
+ <pre><span class="ident">require</span> <span class="punct">"</span><span class="string">net/http</span><span class="punct">"</span>
153
+ <span class="ident">require</span> <span class="punct">"</span><span class="string">uri</span><span class="punct">"</span>
154
+
155
+ <span class="comment"># Token used to terminate the file in the post body. Make sure it is not</span>
156
+ <span class="comment"># present in the file you're uploading.</span>
157
+ <span class="constant">BOUNDARY</span> <span class="punct">=</span> <span class="punct">"</span><span class="string">AaB03x</span><span class="punct">"</span>
158
+
159
+ <span class="ident">uri</span> <span class="punct">=</span> <span class="constant">URI</span><span class="punct">.</span><span class="ident">parse</span><span class="punct">("</span><span class="string">http://something.com/uploads</span><span class="punct">")</span>
160
+ <span class="ident">file</span> <span class="punct">=</span> <span class="punct">"</span><span class="string">/path/to/your/testfile.txt</span><span class="punct">"</span>
161
+
162
+ <span class="ident">post_body</span> <span class="punct">=</span> <span class="punct">[]</span>
163
+ <span class="ident">post_body</span> <span class="punct">< <</span> <span class="punct">"</span><span class="string">--<span class="expr">#{BOUNDARY}</span><span class="escape">rn</span></span><span class="punct">"</span>
164
+ <span class="ident">post_body</span> <span class="punct">< <</span> <span class="punct">"</span><span class="string">Content-Disposition: form-data; name=<span class="escape">"</span>datafile<span class="escape">"</span>; filename=<span class="escape">"</span><span class="expr">#{File.basename(file)}</span><span class="escape">"rn</span></span><span class="punct">"</span>
165
+ <span class="ident">post_body</span> <span class="punct">< <</span> <span class="punct">"</span><span class="string">Content-Type: text/plain<span class="escape">rn</span></span><span class="punct">"</span>
166
+ <span class="ident">post_body</span> <span class="punct">< <</span> <span class="punct">"</span><span class="string"><span class="escape">rn</span></span><span class="punct">"</span>
167
+ <span class="ident">post_body</span> <span class="punct">< <</span> <span class="constant">File</span><span class="punct">.</span><span class="ident">read</span><span class="punct">(</span><span class="ident">file</span><span class="punct">)</span>
168
+ <span class="ident">post_body</span> <span class="punct">< <</span> <span class="punct">"</span><span class="string"><span class="escape">rn</span>--<span class="expr">#{BOUNDARY}</span>--<span class="escape">rn</span></span><span class="punct">"</span>
169
+
170
+ <span class="ident">http</span> <span class="punct">=</span> <span class="constant">Net</span><span class="punct">::</span><span class="constant">HTTP</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">uri</span><span class="punct">.</span><span class="ident">host</span><span class="punct">,</span> <span class="ident">uri</span><span class="punct">.</span><span class="ident">port</span><span class="punct">)</span>
171
+ <span class="ident">request</span> <span class="punct">=</span> <span class="constant">Net</span><span class="punct">::</span><span class="constant">HTTP</span><span class="punct">::</span><span class="constant">Post</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">uri</span><span class="punct">.</span><span class="ident">request_uri</span><span class="punct">)</span>
172
+ <span class="ident">request</span><span class="punct">.</span><span class="ident">body</span> <span class="punct">=</span> <span class="ident">post_body</span><span class="punct">.</span><span class="ident">join</span>
173
+ <span class="ident">request</span><span class="punct">["</span><span class="string">Content-Type</span><span class="punct">"]</span> <span class="punct">=</span> <span class="punct">"</span><span class="string">multipart/form-data, boundary=<span class="expr">#{BOUNDARY}</span></span><span class="punct">"</span>
174
+
175
+ <span class="ident">http</span><span class="punct">.</span><span class="ident">request</span><span class="punct">(</span><span class="ident">request</span><span class="punct">)</span></span></span></span></span></span></span></pre>
176
+ <h3>SSL/HTTPS request</h3>
177
+ <p><strong>Update: There are some good reasons why this code example is bad. It introduces a potential security vulnerability if it's essential you use the server certificate to verify the identity of the server you're connecting to. There's <a href="http://www.rubyinside.com/how-to-cure-nethttps-risky-default-https-behavior-4010.html">a fix for the issue though!</a></strong></p>
178
+ <pre><span class="ident">require</span> <span class="punct">"</span><span class="string">net/https</span><span class="punct">"</span>
179
+ <span class="ident">require</span> <span class="punct">"</span><span class="string">uri</span><span class="punct">"</span>
180
+
181
+ <span class="ident">uri</span> <span class="punct">=</span> <span class="constant">URI</span><span class="punct">.</span><span class="ident">parse</span><span class="punct">("</span><span class="string">https://secure.com/</span><span class="punct">")</span>
182
+ <span class="ident">http</span> <span class="punct">=</span> <span class="constant">Net</span><span class="punct">::</span><span class="constant">HTTP</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">uri</span><span class="punct">.</span><span class="ident">host</span><span class="punct">,</span> <span class="ident">uri</span><span class="punct">.</span><span class="ident">port</span><span class="punct">)</span>
183
+ <span class="ident">http</span><span class="punct">.</span><span class="ident">use_ssl</span> <span class="punct">=</span> <span class="constant">true</span>
184
+ <span class="ident">http</span><span class="punct">.</span><span class="ident">verify_mode</span> <span class="punct">=</span> <span class="constant">OpenSSL</span><span class="punct">::</span><span class="constant">SSL</span><span class="punct">::</span><span class="constant">VERIFY_NONE</span>
185
+
186
+ <span class="ident">request</span> <span class="punct">=</span> <span class="constant">Net</span><span class="punct">::</span><span class="constant">HTTP</span><span class="punct">::</span><span class="constant">Get</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">uri</span><span class="punct">.</span><span class="ident">request_uri</span><span class="punct">)</span>
187
+
188
+ <span class="ident">response</span> <span class="punct">=</span> <span class="ident">http</span><span class="punct">.</span><span class="ident">request</span><span class="punct">(</span><span class="ident">request</span><span class="punct">)</span>
189
+ <span class="ident">response</span><span class="punct">.</span><span class="ident">body</span>
190
+ <span class="ident">response</span><span class="punct">.</span><span class="ident">status</span>
191
+ <span class="ident">response</span><span class="punct">["</span><span class="string">header-here</span><span class="punct">"]</span> <span class="comment"># All headers are lowercase</span></pre>
192
+ <h3>SSL/HTTPS request with PEM certificate</h3>
193
+ <pre><span class="ident">require</span> <span class="punct">"</span><span class="string">net/https</span><span class="punct">"</span>
194
+ <span class="ident">require</span> <span class="punct">"</span><span class="string">uri</span><span class="punct">"</span>
195
+
196
+ <span class="ident">uri</span> <span class="punct">=</span> <span class="constant">URI</span><span class="punct">.</span><span class="ident">parse</span><span class="punct">("</span><span class="string">https://secure.com/</span><span class="punct">")</span>
197
+ <span class="ident">pem</span> <span class="punct">=</span> <span class="constant">File</span><span class="punct">.</span><span class="ident">read</span><span class="punct">("</span><span class="string">/path/to/my.pem</span><span class="punct">")</span>
198
+ <span class="ident">http</span> <span class="punct">=</span> <span class="constant">Net</span><span class="punct">::</span><span class="constant">HTTP</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">uri</span><span class="punct">.</span><span class="ident">host</span><span class="punct">,</span> <span class="ident">uri</span><span class="punct">.</span><span class="ident">port</span><span class="punct">)</span>
199
+ <span class="ident">http</span><span class="punct">.</span><span class="ident">use_ssl</span> <span class="punct">=</span> <span class="constant">true</span>
200
+ <span class="ident">http</span><span class="punct">.</span><span class="ident">cert</span> <span class="punct">=</span> <span class="constant">OpenSSL</span><span class="punct">::</span><span class="constant">X509</span><span class="punct">::</span><span class="constant">Certificate</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">pem</span><span class="punct">)</span>
201
+ <span class="ident">http</span><span class="punct">.</span><span class="ident">key</span> <span class="punct">=</span> <span class="constant">OpenSSL</span><span class="punct">::</span><span class="constant">PKey</span><span class="punct">::</span><span class="constant">RSA</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">pem</span><span class="punct">)</span>
202
+ <span class="ident">http</span><span class="punct">.</span><span class="ident">verify_mode</span> <span class="punct">=</span> <span class="constant">OpenSSL</span><span class="punct">::</span><span class="constant">SSL</span><span class="punct">::</span><span class="constant">VERIFY_PEER</span>
203
+
204
+ <span class="ident">request</span> <span class="punct">=</span> <span class="constant">Net</span><span class="punct">::</span><span class="constant">HTTP</span><span class="punct">::</span><span class="constant">Get</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">uri</span><span class="punct">.</span><span class="ident">request_uri</span><span class="punct">)</span></pre>
205
+ <h3>REST methods</h3>
206
+ <pre><span class="comment"># Basic REST.</span>
207
+ <span class="comment"># Most REST APIs will set semantic values in response.body and response.code.</span>
208
+ <span class="ident">require</span> <span class="punct">"</span><span class="string">net/http</span><span class="punct">"</span>
209
+
210
+ <span class="ident">http</span> <span class="punct">=</span> <span class="constant">Net</span><span class="punct">::</span><span class="constant">HTTP</span><span class="punct">.</span><span class="ident">new</span><span class="punct">("</span><span class="string">api.restsite.com</span><span class="punct">")</span>
211
+
212
+ <span class="ident">request</span> <span class="punct">=</span> <span class="constant">Net</span><span class="punct">::</span><span class="constant">HTTP</span><span class="punct">::</span><span class="constant">Post</span><span class="punct">.</span><span class="ident">new</span><span class="punct">("</span><span class="string">/users</span><span class="punct">")</span>
213
+ <span class="ident">request</span><span class="punct">.</span><span class="ident">set_form_data</span><span class="punct">({"</span><span class="string">users[login]</span><span class="punct">"</span> <span class="punct">=></span> <span class="punct">"</span><span class="string">quentin</span><span class="punct">"})</span>
214
+ <span class="ident">response</span> <span class="punct">=</span> <span class="ident">http</span><span class="punct">.</span><span class="ident">request</span><span class="punct">(</span><span class="ident">request</span><span class="punct">)</span>
215
+ <span class="comment"># Use nokogiri, hpricot, etc to parse response.body.</span>
216
+
217
+ <span class="ident">request</span> <span class="punct">=</span> <span class="constant">Net</span><span class="punct">::</span><span class="constant">HTTP</span><span class="punct">::</span><span class="constant">Get</span><span class="punct">.</span><span class="ident">new</span><span class="punct">("</span><span class="string">/users/1</span><span class="punct">")</span>
218
+ <span class="ident">response</span> <span class="punct">=</span> <span class="ident">http</span><span class="punct">.</span><span class="ident">request</span><span class="punct">(</span><span class="ident">request</span><span class="punct">)</span>
219
+ <span class="comment"># As with POST, the data is in response.body.</span>
220
+
221
+ <span class="ident">request</span> <span class="punct">=</span> <span class="constant">Net</span><span class="punct">::</span><span class="constant">HTTP</span><span class="punct">::</span><span class="constant">Put</span><span class="punct">.</span><span class="ident">new</span><span class="punct">("</span><span class="string">/users/1</span><span class="punct">")</span>
222
+ <span class="ident">request</span><span class="punct">.</span><span class="ident">set_form_data</span><span class="punct">({"</span><span class="string">users[login]</span><span class="punct">"</span> <span class="punct">=></span> <span class="punct">"</span><span class="string">changed</span><span class="punct">"})</span>
223
+ <span class="ident">response</span> <span class="punct">=</span> <span class="ident">http</span><span class="punct">.</span><span class="ident">request</span><span class="punct">(</span><span class="ident">request</span><span class="punct">)</span>
224
+
225
+ <span class="ident">request</span> <span class="punct">=</span> <span class="constant">Net</span><span class="punct">::</span><span class="constant">HTTP</span><span class="punct">::</span><span class="constant">Delete</span><span class="punct">.</span><span class="ident">new</span><span class="punct">("</span><span class="string">/users/1</span><span class="punct">")</span>
226
+ <span class="ident">response</span> <span class="punct">=</span> <span class="ident">http</span><span class="punct">.</span><span class="ident">request</span><span class="punct">(</span><span class="ident">request</span><span class="punct">)</span></pre>
227
+ <p>There are more in August's repo if you want to keep browsing..</p>
228
+ <p style="background-color: #ffc; font-weight: bold; font-size: 13px; color: #000;">Job: New Relic is <a href="http://ruby.jobamatic.com/a/jbb/job-details/165476">looking for a Ruby on Rails developer in Portland, Oregon.</a></p>
229
+ </div>
230
+
231
+
232
+
233
+
234
+ <!-- div style="margin-bottom: 8px; background-color: #ffc; text-align: center; padding: 6px"><a href="http://zfer.us/EKm97" style="text-decoration: none; margin: 0; padding: 0" rel="nofollow"><img src="http://www.rubyinside.com/images/railstutorial-box.gif" /></a></div -->
235
+ <!-- <a href="http://www.rubyinside.com/19walkthrough/"><img src="http://www.rubyinside.com/images/19pro.gif" /></a> -->
236
+
237
+
238
+
239
+
240
+
241
+ </div>
242
+ </div>
243
+
244
+ <div id="related"><h3>Related Posts</h3><ul><li><a href='http://www.rubyinside.com/cheat-sheet-for-rest-on-rails-261.html' rel='bookmark' title='Cheat Sheet for REST on Rails'>Cheat Sheet for REST on Rails</a></li>
245
+ <li><a href='http://www.rubyinside.com/quick-ruby-reference-cheat-sheet-47.html' rel='bookmark' title='Quick Ruby Reference / Cheat Sheet'>Quick Ruby Reference / Cheat Sheet</a></li>
246
+ <li><a href='http://www.rubyinside.com/ruby-on-rails-testing-cheat-sheet-206.html' rel='bookmark' title='Ruby on Rails Testing Cheat Sheet'>Ruby on Rails Testing Cheat Sheet</a></li>
247
+ </ul></div>
248
+ <div id="commentzone">
249
+
250
+
251
+
252
+ <h3 id="comments-title">Comments</h3>
253
+
254
+ <ol class="commentlist">
255
+ <li class="comment even thread-even depth-1" id="comment-40542">
256
+ <img alt='' src='http://0.gravatar.com/avatar/6268c7528d855f1cef5696a00d159909?s=64&amp;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D64&amp;r=G' class='avatar avatar-64 photo' height='64' width='64' /> <cite><a href='http://twitter.com/peterc' rel='external nofollow' class='url'>Peter Cooper</a> says:</cite><br />
257
+
258
+ <small class="commentmetadata">January 16, 2010 at 1:02 am</small>
259
+
260
+
261
+ <p>The idea for the hamster on rollerskates issue can be discovered by checking out its filename.. :-)</p>
262
+
263
+
264
+ </li> <li class="comment odd alt thread-odd thread-alt depth-1" id="comment-40546">
265
+ <img alt='' src='http://0.gravatar.com/avatar/aa31b79adedc3f60547769f1a8971ba6?s=64&amp;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D64&amp;r=G' class='avatar avatar-64 photo' height='64' width='64' /> <cite><a href='http://emmanueloga@gmail.com' rel='external nofollow' class='url'>Emmanuel</a> says:</cite><br />
266
+
267
+ <small class="commentmetadata">January 16, 2010 at 3:50 pm</small>
268
+
269
+
270
+ <p>Anybody knows if the HTTP::Net issues related with Timeout is still present in all (or any) ruby versions?</p>
271
+ <p><a href="http://blog.headius.com/2008/02/rubys-threadraise-threadkill-timeoutrb.html" rel="nofollow">http://blog.headius.com/2008/02/rubys-threadraise-threadkill-timeoutrb.html</a></p>
272
+
273
+
274
+ </li> <li class="comment even thread-even depth-1" id="comment-40550">
275
+ <img alt='' src='http://0.gravatar.com/avatar/2bfc6436d28fc4a224e3ff1702a046d0?s=64&amp;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D64&amp;r=G' class='avatar avatar-64 photo' height='64' width='64' /> <cite>ratbeard says:</cite><br />
276
+
277
+ <small class="commentmetadata">January 17, 2010 at 4:23 pm</small>
278
+
279
+
280
+ <p>Another library that abstracts over Net::HTTP is Adam Wiggin's 'rest-client' library. It seems more targeted toward single requests (i.e. Restclient.get 'google.com') than mixing in and building a request class like HTTParty, though both libraries do variants of each style. It has a great interactive shell and request logging.</p>
281
+ <p><a href="http://github.com/archiloque/rest-client/" rel="nofollow">http://github.com/archiloque/rest-client/</a></p>
282
+ <p>I **highly** recommend another library authored by Mr. Wiggins that abstracts over the messy file system api, 'rush'. I'm really surprised that more libraries and apps that have a non-trivial amount of file system code don't use it.</p>
283
+ <p><a href="http://rush.heroku.com/" rel="nofollow">http://rush.heroku.com/</a></p>
284
+ <p>The code is very clean in both, I tip my hat to you Mr. Wiggins.</p>
285
+
286
+
287
+ </li> <li class="comment odd alt thread-odd thread-alt depth-1" id="comment-40556">
288
+ <img alt='' src='http://0.gravatar.com/avatar/e61f142f400df8299d37c2bce09e3478?s=64&amp;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D64&amp;r=G' class='avatar avatar-64 photo' height='64' width='64' /> <cite><a href='http://august.lilleaas.net/' rel='external nofollow' class='url'>August Lilleaas</a> says:</cite><br />
289
+
290
+ <small class="commentmetadata">January 19, 2010 at 10:19 am</small>
291
+
292
+
293
+ <p>I always use one of these libraries when I can (rest-client, httparty, ...). In some cases you'd be better off without dependencies, though, such as in small shell scripts, etc.</p>
294
+
295
+
296
+ </li> </ol>
297
+
298
+
299
+
300
+ </div>
301
+
302
+ <h3>Other Posts to Enjoy</h3>
303
+ <div class="widget_featured-posts noborder four">
304
+ <ul class="clearfix"><li>
305
+
306
+ <a href="http://www.rubyinside.com/the-split-is-not-enough-whitespace-shenigans-for-rubyists-5980.html"><img src="http://www.rubyinside.com/wp-content/plugins/wp-featured-post-with-thumbnail/scripts/timthumb.php?src=http://www.rubyinside.com/wp-content/uploads/2012/11/spaced.png&amp;h=73&amp;w=73&amp;zc=1" class="alignleft" alt="The Split is Not Enough: Unicode Whitespace Shenigans for Rubyists" /></a>
307
+ <h4 class="featured-title"><a href="http://www.rubyinside.com/the-split-is-not-enough-whitespace-shenigans-for-rubyists-5980.html">The Split is Not Enough: Unicode Whitespace Shenigans for Rubyists</a></h4>
308
+ </li>
309
+ <li>
310
+
311
+ <a href="http://www.rubyinside.com/mega-february-2012-ruby-news-5815.html"><img src="http://www.rubyinside.com/wp-content/plugins/wp-featured-post-with-thumbnail/scripts/timthumb.php?src=http://www.rubyinside.com/wp-content/uploads/2012/03/mega.png&amp;h=73&amp;w=73&amp;zc=1" class="alignleft" alt="The Mega Ruby News and Release Roundup for February 2012" /></a>
312
+ <h4 class="featured-title"><a href="http://www.rubyinside.com/mega-february-2012-ruby-news-5815.html">The Mega Ruby News and Release Roundup for February 2012</a></h4>
313
+ </li>
314
+ <li>
315
+
316
+ <a href="http://www.rubyinside.com/sinatra-book-review-5704.html"><img src="http://www.rubyinside.com/wp-content/plugins/wp-featured-post-with-thumbnail/scripts/timthumb.php?src=http://www.rubyinside.com/wp-content/uploads/2011/12/sinatra-up.jpeg&amp;h=73&amp;w=73&amp;zc=1" class="alignleft" alt="A Lagom Review of O&#8217;Reilly&#8217;s &#8216;Sinatra Up and Running&#8217;" /></a>
317
+ <h4 class="featured-title"><a href="http://www.rubyinside.com/sinatra-book-review-5704.html">A Lagom Review of O&#8217;Reilly&#8217;s &#8216;Sinatra Up and Running&#8217;</a></h4>
318
+ </li>
319
+ <li>
320
+
321
+ <a href="http://www.rubyinside.com/rubys-unary-operators-and-how-to-redefine-their-functionality-5610.html"><img src="http://www.rubyinside.com/wp-content/plugins/wp-featured-post-with-thumbnail/scripts/timthumb.php?src=http://www.rubyinside.com/wp-content/uploads/2011/11/unary.gif&amp;h=73&amp;w=73&amp;zc=1" class="alignleft" alt="Ruby&#8217;s Unary Operators and How to Define Their Functionality" /></a>
322
+ <h4 class="featured-title"><a href="http://www.rubyinside.com/rubys-unary-operators-and-how-to-redefine-their-functionality-5610.html">Ruby&#8217;s Unary Operators and How to Define Their Functionality</a></h4>
323
+ </li>
324
+ </ul> </div>
325
+
326
+
327
+
328
+
329
+
330
+ <h3>Twitter Mentions</h3>
331
+ <div id="boastful"></div>
332
+
333
+ <div class="previousnext">
334
+ <div class="next"><a href="http://www.rubyinside.com/this-weeks-ruby-news-rspec-2-8-0-rc1-minitest-2-8-0-and-whats-new-in-bundler-1-1-5637.html" rel="next">Next Post &raquo;</a></div>
335
+ <div class="previous"><a href="http://www.rubyinside.com/the-ruby-standard-library-to-be-converted-to-gems-for-ruby-2-0-5586.html" rel="prev">&laquo; Previous Post</a></div>
336
+ </div>
337
+
338
+ <!-- <h3>Want to get up to speed with Ruby 1.9?</h3>
339
+
340
+ <p><a href="http://www.rubyinside.com/19walkthrough/"><img src="http://www.rubyinside.com/images/19pro.gif" /></a> </p>
341
+ -->
342
+
343
+
344
+ </div>
345
+
346
+ </div>
347
+
348
+ </div> <!-- inner -->
349
+
350
+
351
+
352
+
353
+ </div> <!-- container -->
354
+
355
+
356
+ <div id="footer">
357
+ <div class="inner">
358
+ <p>Copyright &copy; 2006&ndash;2012 <a href="http://twitter.com/peterc">Peter Cooper</a></p>
359
+ </div>
360
+ </div>
361
+
362
+ <script type="text/javascript" src="http://engine.rubyrow.net/z/1313/adzerk1_4_16_19,adzerk2_4_16_19,adzerk3_4_16_19,adzerk4_4_16_19,adzerk5_4_16_19,adzerk6_4_16_19,adzerk7_4_16_19,adzerk8_4_16_19"></script>
363
+ <script type="text/javascript" src="http://www.rubyinside.com/wp-content/themes/ri2011/jquery.boastful.js"></script>
364
+
365
+ <script type="text/javascript">
366
+ $(document).ready(function() {
367
+ $('#boastful').boastful();
368
+ });
369
+ </script>
370
+
371
+ <script type="text/javascript">
372
+ var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
373
+ document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
374
+ </script>
375
+ <script type="text/javascript">
376
+ var pageTracker = _gat._getTracker("UA-2237791-3");
377
+ pageTracker._initData();
378
+ pageTracker._trackPageview();
379
+ </script>
380
+
381
+
382
+ </body>
383
+ </html>
384
+
385
+ <!-- div style="float:right"><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.rubyinside.com/rubys-unary-operators-and-how-to-redefine-their-functionality-5610.html" data-counturl="http://www.rubyinside.com/rubys-unary-operators-and-how-to-redefine-their-functionality-5610.html" data-text="Ruby&#8217;s Unary Operators and How to Define Their Functionality" data-count="horizontal">Tweet</a></div -->
386
+ <!-- div style="margin-bottom: -12px; margin-top: -10px"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.rubyinside.com%2Frubys-unary-operators-and-how-to-redefine-their-functionality-5610.html&amp;layout=standard&amp;show_faces=false&amp;width=420&amp;action=like&amp;colorscheme=light&amp;height=26" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:420px; height:26px;" allowTransparency="true"></iframe></div -->
387
+ <!-- div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:like href="http://x.com/" show_faces="false" width="450"></fb:like></div -->
388
+
389
+
390
+
391
+ <!-- Dynamic page generated in 0.176 seconds. -->
392
+ <!-- Cached page generated by WP-Super-Cache on 2013-06-03 01:48:10 -->
393
+ <!-- super cache -->
@@ -4,7 +4,7 @@
4
4
  'abw', 'doc', 'yaml', 'uea',
5
5
  'lda', 'pdf', 'ptb', 'dot',
6
6
  'ai', 'id3', 'svo', 'mlp',
7
- 'svm', 'srx'],
7
+ 'svm', 'srx', 'nlp'],
8
8
 
9
9
  encodings:
10
10
  {language_to_code: {
@@ -33,7 +33,7 @@
33
33
  processors: {
34
34
  parsers: [:stanford],
35
35
  segmenters: [:scalpel, :srx, :tactful, :punkt, :stanford],
36
- tokenizers: [:ptb, :stanford, :punkt]
36
+ tokenizers: [:ptb, :stanford, :punkt, :open_nlp]
37
37
  }
38
38
  },
39
39
  stop_words:
@@ -1,3 +1,5 @@
1
+ #encoding: UTF-8
2
+
1
3
  {
2
4
  dependencies: [
3
5
  'punkt-segmenter',
@@ -8,5 +8,9 @@
8
8
  stanford: {
9
9
  jar_path: nil,
10
10
  model_path: nil
11
+ },
12
+ open_nlp: {
13
+ jar_path: nil,
14
+ model_path: nil
11
15
  }
12
16
  }
@@ -100,8 +100,7 @@ module Treat::Entities::Entity::Buildable
100
100
  def from_array(array)
101
101
  obj = self.new
102
102
  array.each do |el|
103
- el = el.to_entity unless el.
104
- is_a?(Treat::Entities::Entity)
103
+ el = el.to_entity unless el.is_a?(Treat::Entities::Entity)
105
104
  obj << el
106
105
  end
107
106
  obj
@@ -206,10 +205,9 @@ module Treat::Entities::Entity::Buildable
206
205
  # Build a document from a raw or serialized file.
207
206
  def from_file(file,def_fmt=nil)
208
207
 
209
- if file.index('yml') ||
210
- file.index('yaml') ||
211
- file.index('xml') ||
212
- file.index('mongo')
208
+ if file.index('.yml') ||
209
+ file.index('.yaml') ||
210
+ file.index('.xml')
213
211
  from_serialized_file(file)
214
212
  else
215
213
  fmt = Treat::Workers::Formatters::