How i organize my grape files in rails app

My file structure looks like this:

app/
  - api
    |- application_api.rb
    |- entities.rb
    |- helpers.rb
    |- root_api.rb
    |- users_api.rb
    |- pages_api.rb
    |- comments_api.rb
  - assets
  - controllers
...

Each api is inherited from application_api, just like application_controller.
eg:

class UsersAPI < ApplicationAPI
 
  get '/users' do
    ......
  end

end

Let’s take a look at application_api

require 'entities'
require 'helpers'

class ApplicationAPI < Grape::API
 
  def self.inherited(child)
    super
    child.format :json
    child.prefix "api"
    child.version 'v1', :using => :path
  end

end

If u wanna only one mount into your routes.rb, i mount all i need in root_api

class RootAPI < Grape::API
  mount PagesAPI
  mount UsersAPI
  mount CommentsAPI
end

routes.rb

mount RootAPI => '/'

Done!

ruby sha1withrsa

Step1: Generate private key using openssl

openssl genrsa -out rsa_pri.pem 1024

Step2: Generate public key using private.pem

openssl rsa -in rsa_pri.pem -out rsa_pub.pem -outform PEM -pubout
require "openssl"

pri = OpenSSL::PKey::RSA.new( File.read("rsa_pri.pem") )
sign = pri.sign( "sha1", "zires".force_encoding("utf-8") )

# if you want to send with hex string (high nibble first)
hex_sign = sign.unpack('H*').first

Verify it!

require "openssl"

pub = OpenSSL::PKey::RSA.new( File.read("rsa_pub.pem") )
pub.verify( "sha1", sign, "zires".force_encoding("utf-8") )
# or
pub.verify( "sha1", [hex_sign].pack("H*"), "zires".force_encoding("utf-8") )

Note: if you get public key (der format) form java like this:

308189028181009EEF0CDA83B2FF51C83EC2374BD7C0B6412AF6165471BDA22AEB99295D4549823968A4F3C31EF
1A9EBBF8B572360B53456E793A0B5A3A70CD6491EA844125D0DC349D1CE38F9A84EF1CDC0647E0B61491EEB8B47
C60FADA22BA22F03A50CC9977A9576AD26D1799F9436E819DF828A36FACFEA15AD6B59F4A2ACB2223682584B020
3010001

You need to pack it first!

File.open("rsa_pub.der", "w") {|f| f << [ File.read("rsa_pub.key") ].pack("H*") }

Thank you for this article [ https://wido.me/sunteya/java-and-ruby-use-sign-or-verify-data-by-rsa ], it helps me a lot!!

git stash recover 恢复

git stash 算是经常要用到的了,切换分支的时候常常要先把一些文件给暂存起来。备忘两个问题:

1)git stash 默认不会把untracked files加进去,如果希望把不在版本库的文件也加进去,如何做?

git stash -u

2) git pop 后refs会被dropped掉,我们会看见如下的信息:

Dropped refs/stash@{0} (f798acc46e0838e5c826d177124ab95a73ac92ca)

如何恢复?

很简单,如果我们能得到那串SHA1的值,直接

git stash apply f798acc46e0838e5c826d177124ab95a73ac92ca

如果我们找不到SHA1的值了,可以在git的–lost-found中找到:

git fsck --no-reflog | awk '/dangling commit/ {print $3}'

拿到SHA1的值只要再apply就可以了

http://stackoverflow.com/questions/89332/recover-dropped-stash-in-git

rails4 concerns

最近把rails4拉下来跑了一下,发现在controllersmodels下多了一个新的文件夹concerns

原来concerns里面放的就是之前写rails库经常用到的ActiveSupport::Concern,解决的问题是models或者controllers太臃肿了(当业务量庞大的时候),不过看起来只是代码的抽离。

相关链接:

f6bbc3f582

put-chubby-models-on-a-diet-with-concerns

顺带提一下,路由里面也加了一个concern

https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/routing/mapper.rb#L1629

concern :commentable do
  resources :comments
end

concern :image_attachable do
  resources :images, only: :index
end

# These concerns are used in Resources routing:

resources :messages, concerns: [:commentable, :image_attachable]

# or in a scope or namespace:

namespace :posts do
  concerns :commentable
end

创业之我见

创业最难的地方是人,有一帮志同道合的人,才是王道。

产品开发要有节奏,不要盲目的追求快。

产品只有两个阶段,一个是快速推出产品,一个是无限迭代产品。

如何才能快,精简才能快,轻装才能快,所以产品的第一版只要核心功能,不要把大量的时间花在无谓的“前瞻性”的讨论上面。

不是非要天天加班的创业才叫有激情。

好的idea只是创业的起点,对于成功,它所占的比例少之又少。

swftools-0.9.1 : Depends: libmpfr1ldbl (>= 2.3.2.dfsg.1-1ubuntu1) which is a virtual package

If you installed newest ubuntu version, you may get this error when apt-get install swftool-0.9.1

solution

Choose your right download link form http://packages.ubuntu.com/lucid/libmpfr1ldbl

[sudo] apt-get install libgmp3c2
cd /tmp
wget http://free.nchc.org.tw/ubuntu//pool/main/m/mpfr/libmpfr1ldbl_2.4.2-3ubuntu1_amd64.deb
dpkg -i ./libmpfr1ldbl_2.4.2-3ubuntu1_amd64.deb
[sudo] apt-get install swftool-0.9.1

Update-rc.d Command

非原创文章,只是做一下备份,转至:

Ubuntu系统Update-rc.d命令

update-rc.d详解 Debian系统启动脚本

一、Linux系统主要启动步骤

  读取 MBR 的信息,启动 Boot Manager。

  加载系统内核,启动 init 进程, init 进程是 Linux 的根进程,所有的系统进程都是它的子进程。

  init 进程读取 /etc/inittab 文件中的信息,并进入预设的运行级别。通常情况下 /etc/rcS.d/ 目录下的启动脚本首先被执行,然后是/etc/rcN.d/ 目录。

  根据 /etc/rcS.d/ 文件夹中对应的脚本启动 Xwindow 服务器 xorg,Xwindow 为 Linux 下的图形用户界面系统。

  启动登录管理器,等待用户登录。

二、运行级别

  Ubuntu中的运行级别

  0(关闭系统)
  1(单用户模式,只允许root用户对系统进行维护。)
  2 到 5(多用户模式,其中3为字符界面,5为图形界面。)
  6(重启系统)

  切换运行级别

  init [0123456Ss]

  例如:init 0 命令关机; init 6 命令重新启动

  启动项管理工具

  sudo install sysv-rc-conf //或者使用带gui的工具bum
  sudo sysv-rc-conf

三、update-rc.d命令详解

  从所有的运行级别中删除指定启动项

  update-rc.d -f remove

  按指定顺序、在指定运行级别中启动或关闭

  update-rc.d start|stop

  实例:update-rc.d apachectl start 20 2 3 4 5 . stop 20 0 1 6 .

  解析:表示在2、3、4、5这五个运行级别中,由小到大,第20个开始运行apachectl;在 0 1 6这3个运行级别中,第20个关闭apachectl。这是合并起来的写法,注意它有2个点号,效果等于下面方法:

  update-rc.d apachectl defaults

  A启动后B才能启动,B关闭后A才关闭

  update-rc.d A defaults 80 20
  update-rc.d B defaults 90 10

  启动和关闭顺序为90,级别默认

  update-rc.d defaults 90

Insert links using the defaults:
update-rc.d foobar defaults

Equivalent command using explicit argument sets:
update-rc.d foobar start 20 2 3 4 5 . stop 20 0 1 6 .

More typical command using explicit argument sets:
update-rc.d foobar start 30 2 3 4 5 . stop 70 0 1 6 .

Remove all links for a script (assuming foobar has been deleted
already):
update-rc.d foobar remove

Example of disabling a service:
update-rc.d -f foobar remove
update-rc.d foobar stop 20 2 3 4 5 .

Example of a command for installing a system initialization-and-shut‐
down script:
update-rc.d foobar start 45 S . start 31 0 6 .

Example of a command for disabling a system initialization-and-shutdown
script:
update-rc.d -f foobar remove
update-rc.d foobar stop 45 S .

修改LINUX默认启动级别
# 0 – 停机(千万不要把initdefault设置为0 )
# 1 – 单用户模式
# 2 – 多用户,但是没有NFS
# 3 – 完全多用户模式
# 4 – 没有用到
# 5 – X11
# 6 – 重新启动(千万不要把initdefault设置为6 )
# 对各个运行级的详细解释:
0 为停机,机器关闭。
1 为单用户模式,就像Win9x下的安全模式类似。
2 为多用户模式,但是没有NFS支持。
3 为完整的多用户模式,是标准的运行级。
4 一般不用,在一些特殊情况下可以用它来做一些事情。例如在笔记本电脑的电池用尽时,可以切换到这个模式来做一些设置。
5 就是X11,进到X Window系统了。
6 为重启,运行init 6机器就会重启。

修改级别
vi /etc/inittab
把id:3:initdefault:中的3改为5就是默认进入图形界面了

Rails4 Activesupport Changelog

activesupport

  • Date.beginning_of_week thread local and beginning_of_week application config option added (default is Monday).
    Pull Request #5339

    thread local并且多增加了一个配置选项,可以设置一周的起点是星期几,默认是星期一

    class Application < Rails::Application
      config.beginning_of_week = :wednesday
    end
  • An optional block can be passed to config_accessor to set its default value.
    Pull Request #7645

    config_accessor可以设置默认值:

    class User
      include ActiveSupport::Configurable
      config_accessor :hair_colors do
        [:brown, :black, :blonde, :red]
      end
    end

    User.hair_colors # => [:brown, :black, :blonde, :red]
  • An optional block can be passed to Hash#deep_merge. The block will be invoked for each duplicated key and used to resolve the conflict.
    Pull Request #7628

    Hash#deep_merge支持block:

    h1 = {x: {y: [4,5,6]}, z: [7,8,9]}
    h2 = {x: {y: [7,8,9]}, z: "xyz"}

    h1.deep_merge(h2) do |key, old, new|
      Array.wrap(old) + Array.wrap(new)
    end
    #=> {:x => {:y => [4, 5, 6, 7, 8, 9]}, :z => [7, 8, 9, "xyz"]}
  • ActiveSupport::Deprecation is now a class. It is possible to create an instance of deprecator. Backwards compatibility has been preserved.
    Pull Request #6348

    You can choose which instance of the deprecator will be used.

    deprecate :method_name, :deprecator => deprecator_instance

    You can use ActiveSupport::Deprecation in your gem.

    require 'active_support/deprecation'
    require 'active_support/core_ext/module/deprecation'

    class MyGem
      def self.deprecator
        ActiveSupport::Deprecation.new('2.0', 'MyGem')
      end

      def old_method
      end

      def new_method
      end

      deprecate :old_method => :new_method, :deprecator => deprecator
    end

    MyGem.new.old_method
    # => DEPRECATION WARNING: old_method is deprecated and will be removed from MyGem 2.0 (use new_method instead). (called from <main> at file.rb:18)
  • ERB::Util.html_escape encodes single quote as #39. Decimal form has better support in old browsers.
    Pull Request #7513
  • ActiveSupport::Callbacks: deprecate monkey patch of object callbacks. Using the #filter method.
    Pull Request #7560

https://github.com/rails/rails/blob/master/activesupport/CHANGELOG.md

ruby string percent method

Last week, when i checked a question in StackOverflow, i found an interesting thing. See the example below:

"%d hour %d minutes %d seconds" % [ 1, 20, 30 ]
# => "1 hour 20 minutes 30 seconds"

At first, i thought the percent % is just liked 11%3, so i looked into ruby-doc, i got my answer, % is nothing but a instance method of String

Format—Uses str as a format specification, and returns the result of applying it to arg. If the format specification contains more than one substitution, then arg must be an Array or Hash containing the values to be substituted. See Kernel::sprintf for details of the format string.

"%05d" % 123                              
#=> "00123"

"%-5s: %08x" % [ "ID", self.object_id ]  
#=> "ID: 200e14d6"

"foo = %{foo}" % { :foo => 'bar' }
#=> "foo = bar"

The syntax of a format sequence is follows.

%[flags][width][.precision]type

Kernel#sprintf