<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>est</title>
    <link>https://ruby-china.org/est</link>
    <description></description>
    <language>en-us</language>
    <item>
      <title>十几行代码实现阿里云 OSS 上传</title>
      <description>&lt;p&gt;出于这个帖子的 &lt;a href="https://ruby-china.org/topics/25352" rel="nofollow" target="_blank"&gt;https://ruby-china.org/topics/25352&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;原始需求不太一样，我写这个函数目的是为了实现服务器给远程文件签名，客户端/浏览器直接上传，无需经过服务器中转。&lt;/p&gt;

&lt;p&gt;实现 代码有删改，没测试过，大概思路如下：&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def aliyun_oss file_name
  key, secret = 'YOUR_ALIYUN_KEY', 'YOUR_ALIYUN_SECRET'
  bucket_name = 'YOUR_OSS_BUCKET'
  date_str = Time.now.utc.strftime '%a, %d %b %Y %H:%M:%S GMT'

  path = "YOUR_WHATEVER_PREFIX/#{file_name}"
  signature = "PUT\n\n\n#{date_str}\n/#{bucket_name}/#{path}"
  token_b64 = Base64.encode64(
    Digest::HMAC.digest(signature, secret, Digest::SHA1)
  ).strip
 return {Authorization: 'OSS %s:%s' % [key, token_b64], Date: date_str}
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;然后用你喜欢的 http 工具 PUT 原始文件内容到  "http://#{bucket_name}.oss.aliyuncs.com/YOUR_WHATEVER_PREFIX/#{file_name}" 即可。其中 http 请求头需要加上 Authorization 这个字段。&lt;/p&gt;

&lt;p&gt;我在服务器端的的实现之一是直接 nohup 一个 curl。免除了 sidekiq 的麻烦。还能失败了自动重传。&lt;/p&gt;

&lt;p&gt;有人问：为什么不用 X 库的 Y 方法呢？我的回答是：懒得增加额外的依赖了。&lt;/p&gt;</description>
      <author>est</author>
      <pubDate>Wed, 29 Apr 2015 14:08:02 +0800</pubDate>
      <link>https://ruby-china.org/topics/25367</link>
      <guid>https://ruby-china.org/topics/25367</guid>
    </item>
    <item>
      <title>Python 也有 Ruby 的 string interpolation 了，无性能损失</title>
      <description>&lt;p&gt;&lt;a href="https://github.com/syrusakbary/interpy" rel="nofollow" target="_blank"&gt;https://github.com/syrusakbary/interpy&lt;/a&gt;&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# coding: interpy

package = "Interpy"
print "Enjoy #{package}!"
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;编译成 bytecode，性能无损失。。。。&lt;/p&gt;

&lt;p&gt;感觉这是 AST transform 以后又一个特别有用的黑魔法&lt;/p&gt;

&lt;p&gt;以后会不会出现 # coding: python3 这种无缝 2to3 转换呢。。。。哈哈哈哈。。。&lt;/p&gt;

&lt;p&gt;dropbox 搞这个 pyxl 也是脑洞大开！ 
&lt;a href="https://github.com/dropbox/pyxl" rel="nofollow" target="_blank"&gt;https://github.com/dropbox/pyxl&lt;/a&gt;&lt;/p&gt;</description>
      <author>est</author>
      <pubDate>Wed, 08 Apr 2015 22:09:44 +0800</pubDate>
      <link>https://ruby-china.org/topics/25057</link>
      <guid>https://ruby-china.org/topics/25057</guid>
    </item>
    <item>
      <title>ActiveRecord model 里你们是如何处理耗时远程属性的 cache 的？</title>
      <description>&lt;p&gt;比如&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ItemLog &amp;lt; ActiveRecord::Base
  def get_address
    http_get('http://x.com/get_address_by_item_type?id=' + self.type_id).to_json['address']
  end
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;那么比如遍历 500 个 ItemLog 输出 json，包括 get_address 字段，那么就得对应 500 次 http 请求。&lt;/p&gt;

&lt;p&gt;其实 type_id 是有限的几种，可以搞个缓存&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;address_cache = Hash[ItemLog.where(...).limit(500).pluck(:type_id).uniq.map {|type_id| [type_id, get_address(type_id)] }]
ItemLog.where(...).limit(500).each { |item| item.address = address_cache[item.type_id }
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;显示 cache 明显的缺点就是需要两次遍历数据，一次得到所有唯一的 type_id，一次去处理最终 json 输出。&lt;/p&gt;

&lt;p&gt;我的问题是，可否避免在代码上下文显示搞缓存，可否在 model 里直接搞个类似 scope cache 的东东？&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ItemLog &amp;lt; ActiveRecord::Base
  def address
    address_cache[self.type_id] || http_get('http://x.com/get_address_by_item_type?id=' + self.type_id).to_json['address']
  end
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;大家一般是怎么处理的呢？&lt;/p&gt;</description>
      <author>est</author>
      <pubDate>Sun, 04 Jan 2015 18:09:25 +0800</pubDate>
      <link>https://ruby-china.org/topics/23538</link>
      <guid>https://ruby-china.org/topics/23538</guid>
    </item>
    <item>
      <title>你们用 mina 是如何部署多环境 / 多服务器的？</title>
      <description>&lt;p&gt;难道只能用  $ mina deploy -f config/deploy_staging.rb 这种很 low 的形式吗？&lt;/p&gt;

&lt;p&gt;难道只能写多份.rb 文件吗？&lt;/p&gt;</description>
      <author>est</author>
      <pubDate>Mon, 01 Dec 2014 09:33:29 +0800</pubDate>
      <link>https://ruby-china.org/topics/22953</link>
      <guid>https://ruby-china.org/topics/22953</guid>
    </item>
    <item>
      <title>Mina 如何 使用 git pull 而不是 git clone?</title>
      <description>&lt;p&gt;每次都 git clone 多费事。。。。。还不如 git archive 快呢。&lt;/p&gt;</description>
      <author>est</author>
      <pubDate>Fri, 21 Nov 2014 13:16:28 +0800</pubDate>
      <link>https://ruby-china.org/topics/22796</link>
      <guid>https://ruby-china.org/topics/22796</guid>
    </item>
    <item>
      <title>http://guides.ruby-china.org/initialization.html 是不是还没翻译</title>
      <description>&lt;p&gt;&lt;a href="http://guides.ruby-china.org/initialization.html" rel="nofollow" target="_blank"&gt;http://guides.ruby-china.org/initialization.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;这篇是我打开姿势不丢还是缺翻译？如果没人翻译我想来试试。&lt;/p&gt;</description>
      <author>est</author>
      <pubDate>Wed, 15 Oct 2014 14:31:01 +0800</pubDate>
      <link>https://ruby-china.org/topics/22054</link>
      <guid>https://ruby-china.org/topics/22054</guid>
    </item>
    <item>
      <title>rescue 里可否获得各层栈里局部变量？</title>
      <description>&lt;p&gt;在做一个异常捕获工具，rescue 里面可否获得异常 call stack 每一层栈的局部变量和全局变量信息？&lt;/p&gt;</description>
      <author>est</author>
      <pubDate>Mon, 22 Sep 2014 14:55:04 +0800</pubDate>
      <link>https://ruby-china.org/topics/21660</link>
      <guid>https://ruby-china.org/topics/21660</guid>
    </item>
    <item>
      <title>可否知道其他 module 的__FILE__</title>
      <description>&lt;p&gt;已知一个 module 可否知道对应源码路径在哪里？&lt;/p&gt;

&lt;p&gt;比如 &lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require 'active_record'

ActiveRecord.__FILE__
NoMethodError: undefined method `__FILE__' for ActiveRecord:Module
    from (irb):9
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;这样是不行的。&lt;/p&gt;

&lt;p&gt;btw，python 可以。。。。比如&lt;code&gt;import urllib2; urllib2.__file__&lt;/code&gt;&lt;/p&gt;</description>
      <author>est</author>
      <pubDate>Mon, 22 Sep 2014 14:43:56 +0800</pubDate>
      <link>https://ruby-china.org/topics/21659</link>
      <guid>https://ruby-china.org/topics/21659</guid>
    </item>
    <item>
      <title>Rack 有没有异常或 5xx 错误自动发邮件的 middleware？</title>
      <description>&lt;ol&gt;
&lt;li&gt;遇到 500 错误自动发邮件给开发&lt;/li&gt;
&lt;li&gt;同一地方异常避免频发反复发送&lt;/li&gt;
&lt;li&gt;邮件包含 call stack 和局部变量摘要&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;有没有现成模块我直接拿过来用？&lt;/p&gt;

&lt;p&gt;服务器是 Goliath/Sinatra 混搭。。。&lt;/p&gt;</description>
      <author>est</author>
      <pubDate>Wed, 03 Sep 2014 10:44:57 +0800</pubDate>
      <link>https://ruby-china.org/topics/21359</link>
      <guid>https://ruby-china.org/topics/21359</guid>
    </item>
  </channel>
</rss>
