使用Faraday上传图片到新浪微博

Faraday 是一个轻量灵活的 HTTP client.使用 Faraday 可以轻松的帮助我们上传含图片内容的信息到新浪微博。

新浪微博的 statuses/upload 接口要求请求必须是 multipart/form-data 编码格式,并且 pic 参数为 binary 类型。Faraday 几乎为我们实现了所有的要求,我们不需要自己来拼接这种请求报头和报文,下面是我的一个实例:

1
2
3
4
5
6
7
8
9
10
conn = Faraday.new(:url => 'https://upload.api.weibo.com') do |faraday|
  faraday.request :multipart
  faraday.adapter :net_http
end

conn.post "/2/statuses/upload.json", {
  :access_token => topic.user.weibo_token,
  :status => URI.encode(status),
  :pic => Faraday::UploadIO.new(pic_path, 'image/jpeg')
}
Comments

最近有点不开心

吐槽开始。

换了新工作3个多月了,虽然薪水涨了不少,但是心情却反而不如在之前的公司,仔细想了一下原因大概如下:

1.原来我一直喜欢的是很有GEEK氛围的团队,虽然之前我认为自己不够GEEK,但是到了新公司发现自己还算这里比较GEEK. 这边可能是属于传统的软件开发公司,比较刻板,项目驱动,每天都有人催,大家也就自然没有闲情逸致去发现新的技术或者是有意思或者新奇的东西。

2.团队交流太少。平常在工作的时候,可能是由于各自负责的项目过于分散,大家很少说话,即便开玩笑。 还有的细节比如吃饭,下班啊什么的,都是各走各的,也不怎么打招呼。我还记得我刚来的时候,甚至是在我的提议下,大家才各自做了一下自我介绍。

3.造成以上的状况的原因很可能是Leader情商不高,这个就不多抱怨啦,性格使然。

4.对写代码的追求。这边几乎没人关心代码风格,架构什么的,完成功能就行。这恰恰是我最痛苦的地方。每天看着一坨一坨又臭又长的代码…

5.需求过多,工程师从来都是被命令式的做项目,真正成了一个码农。很少参与需求讨论,不爽…

…….

Comments

Update MongoDB to Newest Stable Version

如果你是在Ubuntu下 sudo apt-get install mongodb, 很不幸,你很有可能会得到一堆错误,具体原因和解决办法如下:

1
2
3
  The Ubuntu repositories have a very old version of mongoDB - the current stable branch is 2.0 at the time of writing and yours is 1.6, 
  you could try fixing the error but it's not really worth it. 
  You should get an up to date version from the official 10gen repos as described here and uninstall the one you have now:

解决办法参考下面: http://www.mongodb.org/display/DOCS/Ubuntu+and+Debian+packages

General procedure would be:
1
2
3
  Remove Ubuntu version (apt-get remove mongodb)
  Add new repo line and gpg key as described above
  Install 10gen version (apt-get update; apt-get install mongodb-10gen)
Comments

解决 Redis-search 在 Passenger 下的 Reconnect 错误

redis-search 是一款基于 Redis 的高效搜索组件, 使用起来非常方便, 效果也非常不错, 但是偶尔搜索的时候有这个错误:

Tried to use a connection from a child process without reconnecting. You need to reconnect to Redis after forking.

这个错误是如何产生的呢? 我看了一下 redis-rb 的 unicorn example,找到了这个:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  require "redis"

  worker_processes 3

  # If you set the connection to Redis *before* forking,
  # you will cause forks to share a file descriptor.
  #
  # This causes a concurrency problem by which one fork
  # can read or write to the socket while others are
  # performing other operations.
  #
  # Most likely you'll be getting ProtocolError exceptions
  # mentioning a wrong initial byte in the reply.
  #
  # Thus we need to connect to Redis after forking the
  # worker processes.

  after_fork do |server, worker|
    Redis.current.quit
  end

然后我试着找资料写了一个 PhusionPassenger 的解决方案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  # config/initializers/redis_search.rb
  require "redis"
  require "redis-namespace"
  require "redis-search"
  # don't forget change namespace

  if defined?(PhusionPassenger)
    PhusionPassenger.on_event(:starting_worker_process) do |forked|
      if forked
        Redis.current.client.reconnect
      else
        Redis.current = Redis.new(:host => "127.0.0.1",:port => "6379")
      end
    end
  end

  redis = Redis.current
  redis.select(3)
  # We suggest you use a special db in Redis, when you need to clear all data, you can use flushdb command to clear them.
  # 
  # Give a special namespace as prefix for Redis key, when your have more than one project used redis-search, this config will make them work fine.
  redis = Redis::Namespace.new("wakmj:redis_search", :redis => redis)
  Redis::Search.configure do |config|
    config.redis = redis
    config.complete_max_length = 100
    config.pinyin_match = true
    # use rmmseg, true to disable it, it can save memroy
    config.disable_rmmseg = false
  end

好了,重新启动,现在没有如上的错误了。 参考链接:https://github.com/redis/redis-rb/blob/master/lib/redis/client.rb#L277

Comments

Hello World

之前博客在Heroku上面,经常被大墙墙掉不说,还慢的发指,还是迁移到 Github 上面来,稳定又免费,何乐而不为呢?

Comments