2016年4月10日 星期日

singleton_method

singleton_method means object’s specific method

when we define method on specific object, will insert the singleton class between object and his class.
  1. puts example2. class  
  2.   
  3. class  << example2  
  4.   puts  self  
  5. end  

輸出結果:
  1. ExampleClass  
  2. #<Class:#<ExampleClass:0x28305d>>  


And if we add class method on class just like add singleton method on class 

instance_eval 與class_eval

instance_eval’s receiver must be an instance. and if an instance call instance_eval => mean you can define this instance’s singleton_method.
and because class is a instance of Class => so class also can call instance_eval => define this class’s singleton_method => class method

class_eval’s receiver must be an class. and if class call class_eval => you can define this class’s instance_method

instance_eval must be call by instance, use to define singleton_methods

class_eval must be call by class, use to define instance_methods 

Rails 啟動過程

1 啟動!
  1. call ruby rails in RVM folder => railties/bin/rails => require "rails/cli"
  2. railties/lib/rails/app_rails_loader.rb : find ‘bin/rails’
  3. bin/rails: require_relative '../config/boot and require 'rails/commands'
  4. config/boot.rb: setting Bundler, Gemfile
  5. rails/commands.rb: setting aliases, require 'rails/commands/commands_tasks'
  6. rails/commands/command_tasks.rb: run command, => rails/commands/server => require ‘fileutils, ‘optparse', ‘action_dispatch’, require 'rails'
  7. actionpack/lib/action_dispatch.rb: response routes
  8. rails/commands/server.rb: inherited from Rack::Server: call Rack::Server’s initialize。
  9. Rack: lib/rack/server.rb: provide interface for app on rack base, => setting options
  10. config/application.rb: app settings
  11. Rails::Server#start: still will call Rack::Server.start
  12. config/environment.rb
  13. config/application.rb => require ‘rails/all'

2 載入 Rails

  1. railties/lib/rails/all.rb
  2. config/environment.rb
  3. railties/lib/rails/application.rb: initialize!
  4. Rack: lib/rack/server.rb

2016年4月5日 星期二

oauth 2.0 work flow



oAuth want to replace traditional user/password flow, more security.

User don’t need to provide his user/password to third party( like small game on FB), when want to play small game, 
will flow oauth work flow to get your permission to get access token to share your data with small game

U:  User (resource owner)
C:  Client (application)
A:  Authorization server
R:  Resource server

  1. C register on A, apply clientID & Secret, and also need to set redirect_url
  2. when user want to use his resource to do something, through C
  3. if not login in,
    1. 1) C set Authorization Request to U
    2. 2) U response Authorization Grant to C
    3. 3) C set Authorization Grant to A
    4. 4) A response Access Token to C
    5. 5) C use Access token to R to get resource
    6. 6) R response Protected Resource to C

some variable:
response_type => code or token, if code, have next phase authorization
                                                      if token, will response token directly
client_id => C register on A
redirect_uri => just for know response to where
scope => set protected what resources
state => any word you want to sent
code => when response_type set code, will append on redirect_uri
client_secret => C register on A
grant_type => for validate code available
access_token => use this to get api



the max different between oAuth1.0a and oAuth 2.0 is scope 






2016年3月30日 星期三

Stubs, Mocks, and Spies

stubs:
create fake object, and assume this fake object can response what information, response, make sure the main tested object can get the consistent result

mocks
same with stubs, but must be executed




 from https://danielzhangqinglong.github.io/2015/04/07/rspec-mock/

比如說, 現在要去測試一下Twitter是不是成功發了一條推文:
1
2
3
4
5
6
7
8
9
10
require 'twitter'
class TwitterEmotion
def tweet
twitter_client.update "I am very happy"
end
def twitter_client
Twitter::REST::Client .new
end
end
測試代碼:
1
2
3
4
it "should tweet successfully" do
emotion = TwitterEmotion. new
expect{ emotion.tweet }.not_to raise_error
end
但是這真的會把你的推文發生出去,還有,如果網絡環境不好或者Twitter的服務器沒有及時處理請求,那麼我這邊的測試就會跑不通,等等問題. 
正因為如此,才需要把使用Twitter服務的部分用mock代替.這樣測試就不會和Twitter進行交互了.下面是使用mock後的測試:
1
2
3
4
5
6
7
8
9
10
11
12
it "should tweet successfully" do
# mock Twitter client
twitter_client_mock = double( 'Twitter client' )
# 讓update方法可以在mock對像上調用
allow(twitter_client_mock). to receive(:update)
emotion = TwitterEmotion. new
allow(emotion). to receive(:twitter_client).and_return(twitter_client_mock)
# 如果在emotion上調用了twitter_client, 那麼就返回twitter_client_mock
expect{ emotion.tweet }.not_to raise_error
end



allow (想要替換方法的對象) . to receive (所要替換的方法) . and_return (返回值對象)


allow
(mock對象) . to receive (方法) . and_raise (異常)

Initialize the serializer

ActiveModel::ArraySerializer.new(Funding.all, each_serializer: FundingSerializer).to_json




For me I passed the view_context to the array serializer:
ActiveModel::ArraySerializer.new(your_array, each_serializer: YourSerializer, scope: self.view_context)

how to pass scope

https://github.com/rails-api/active_model_serializers/issues/510




2016年3月3日 星期四

好的架構源於不停地衍變

架構的演進


http://www.csdn.net/article/2015-10-24/2826028

所有東西在一台機器上








資料庫變成瓶頸
改成分佈式架構



將業務層 資料庫層 在做垂直的拆分 減輕壓力




多一個服務層 來較用呼叫其他方法