Class: Twin

Inherits:
Object
  • Object
show all
Defined in:
lib/twin.rb,
lib/twin/resources.rb

Defined Under Namespace

Classes: Response

Constant Summary collapse

X_CASCADE =
'X-Cascade'.freeze
PASS =
'pass'.freeze
PATH_INFO =
'PATH_INFO'.freeze
AUTHORIZATION =
'HTTP_AUTHORIZATION'.freeze
DEFAULT_OPTIONS =
{ :model => 'TwinAdapter' }
DEFAULT_STATUS_PARAMS =
{
  :id => nil,
  :text => "",
  :user => nil,
  :created_at => "Mon Jan 01 00:00:00 +0000 1900",
  :source => "web",
  :coordinates => nil,
  :truncated => false,
  :favorited => false,
  :contributors => nil,
  :annotations => nil,
  :geo => nil,
  :place => nil,
  :in_reply_to_screen_name => nil,
  :in_reply_to_user_id => nil,
  :in_reply_to_status_id => nil
}
DEFAULT_USER_INFO =
{
  :id => nil,
  :screen_name => nil,
  :name => "",
  :description => "",
  :profile_image_url => nil,
  :url => nil,
  :location => nil,
  :created_at => "Mon Jan 01 00:00:00 +0000 1900",
  :profile_sidebar_fill_color => "ffffff",
  :profile_background_tile => false,
  :profile_sidebar_border_color => "ffffff",
  :profile_link_color => "8b8b9c",
  :profile_use_background_image => false,
  :profile_background_image_url => nil,
  :profile_background_color => "FFFFFF",
  :profile_text_color => "000000",
  :follow_request_sent => false,
  :contributors_enabled => false,
  :favourites_count => 0,
  :lang => "en",
  :followers_count => 0,
  :protected => false,
  :geo_enabled => false,
  :utc_offset => 0,
  :verified => false,
  :time_zone => "London",
  :notifications => false,
  :statuses_count => 0,
  :friends_count => 0,
  :following => true
}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Twin

Returns a new instance of Twin.



30
31
32
33
# File 'lib/twin.rb', line 30

def initialize(app, options = {})
  @app = app
  @options = DEFAULT_OPTIONS.merge options
end

Class Attribute Details

.resourcesObject

Returns the value of attribute resources.



18
19
20
# File 'lib/twin.rb', line 18

def resources
  @resources
end

Instance Attribute Details

#capturesObject

Returns the value of attribute captures.



15
16
17
# File 'lib/twin.rb', line 15

def captures
  @captures
end

#content_typeObject

Returns the value of attribute content_type.



15
16
17
# File 'lib/twin.rb', line 15

def content_type
  @content_type
end

#current_userObject

Returns the value of attribute current_user.



15
16
17
# File 'lib/twin.rb', line 15

def current_user
  @current_user
end

#formatObject

Returns the value of attribute format.



15
16
17
# File 'lib/twin.rb', line 15

def format
  @format
end

#requestObject

Returns the value of attribute request.



15
16
17
# File 'lib/twin.rb', line 15

def request
  @request
end

Class Method Details

.resource(path, &block) ⇒ Object



23
24
25
26
# File 'lib/twin.rb', line 23

def self.resource(path, &block)
  reg = %r{^/(?:1/)?#{path}(?:\.(\w+))?$}
  self.resources << [reg, block]
end

Instance Method Details

#call(env) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/twin.rb', line 35

def call(env)
  path = normalize_path(env[PATH_INFO])
  matches = nil
  
  if path != '/' and found = recognize_resource(path)
    block, matches = found
    
    # TODO: bail out early if authentication failed
    twin_token = env[AUTHORIZATION] =~ / oauth_token="(.+?)"/ && $1
    authenticated_user = twin_token && model.find_by_twin_token(twin_token)
    
    clone = self.dup
    clone.request = Rack::Request.new env
    clone.captures = matches[1..-1]
    clone.format = clone.captures.pop
    clone.current_user = authenticated_user
    
    clone.perform block
  else
    # [404, {'Content-Type' => 'text/plain', X_CASCADE => PASS}, ['Not Found']]
    @app.call(env)
  end
end

#perform(block) ⇒ Object



65
66
67
68
# File 'lib/twin.rb', line 65

def perform(block)
  response = instance_eval &block
  generate_response response
end

#recognize_resource(path) ⇒ Object



59
60
61
62
63
# File 'lib/twin.rb', line 59

def recognize_resource(path)
  matches = nil
  pair = self.class.resources.find { |reg, block| matches = path.match(reg) }
  pair && [pair[1], matches]
end