2016年4月10日 星期日

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

所有東西在一台機器上








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



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




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

2016年3月2日 星期三

Why I No Longer Use MVC Frameworks 



 In traditional MVC, the action (controller) would call an update method on the model and upon success (or error) decide how to update the view


The SAM pattern can be represented by the following expression:
         V = S( vm( M.present( A(data) ) ), nap(M))
which stipulates that the view V of a system can be computed, after an action A has been applied, as a pure function of the model.

In SAM, A (actions), vm (view-model), nap (next-action predicate) and S (state representation) are and must all be pure functions. With SAM, what we commonly call the “state” (the values of the properties of the system) is entirely confined to the model and the logic that changes these values is not visible outside the model itself. 







The SAM pattern changes completely the paradigm of front-end architectures because, on the foundation of TLA+, the business logic can be clearly delineated into:
  • Actions as pure functions
  • CRUD operations in the model
  • States which control automatic Actions

2016年2月27日 星期六

Making Generators in Rails 


class LayoutGenerator < Rails::Generators::Base
  source_root File.expand_path('../templates'__FILE__)
  argument :layout_name:type => :string:default => "application"
  class_option :stylesheet:type => :boolean:default => true:description => "Include stylesheet file"

  def generate_layout
    copy_file "stylesheet.css""public/stylesheets/#{file_name}.css" if options.stylesheet?
    template "layout.html.erb""app/views/layouts/#{file_name}.html.erb"
  end

  private
  def file_name
    layout_name.underscore
  end

end

every public method in the class (Rails::Generators::Base) will be executed when the generator runs

  • arguments optional:  we can define a default value for each argument. We’ll add a layout_name argument with a default value of application
  • template method: which takes similar arguments to copy_file but which will parse any erb in the template before copying it to the destination directory
  • class_option: the ability to pass an option that will stop the stylesheet file being generated. We can do this by using the class_option method