api_explorer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +13 -0
  3. data/Rakefile +40 -0
  4. data/app/assets/images/api_explorer/clear.png +0 -0
  5. data/app/assets/images/api_explorer/delete.png +0 -0
  6. data/app/assets/images/api_explorer/maximize.png +0 -0
  7. data/app/assets/images/api_explorer/minimize.png +0 -0
  8. data/app/assets/javascripts/api_explorer/api.js +193 -0
  9. data/app/assets/javascripts/api_explorer/application.js +15 -0
  10. data/app/assets/stylesheets/api_explorer/api.css +355 -0
  11. data/app/assets/stylesheets/api_explorer/application.css +13 -0
  12. data/app/controllers/api_explorer/api_controller.rb +103 -0
  13. data/app/controllers/api_explorer/application_controller.rb +4 -0
  14. data/app/helpers/api_explorer/api_helper.rb +4 -0
  15. data/app/helpers/api_explorer/application_helper.rb +4 -0
  16. data/app/views/api_explorer/api/_authentication.html.erb +36 -0
  17. data/app/views/api_explorer/api/_headers.html.erb +16 -0
  18. data/app/views/api_explorer/api/_response.html.erb +44 -0
  19. data/app/views/api_explorer/api/history.html.erb +3 -0
  20. data/app/views/api_explorer/api/index.html.erb +42 -0
  21. data/app/views/api_explorer/api/parameters.html.erb +8 -0
  22. data/app/views/layouts/api_explorer/application.html.erb +16 -0
  23. data/config/routes.rb +6 -0
  24. data/lib/api_explorer/engine.rb +13 -0
  25. data/lib/api_explorer/version.rb +3 -0
  26. data/lib/api_explorer.rb +11 -0
  27. data/lib/tasks/api_explorer_tasks.rake +4 -0
  28. data/test/api_explorer_test.rb +7 -0
  29. data/test/dummy/README.rdoc +261 -0
  30. data/test/dummy/Rakefile +7 -0
  31. data/test/dummy/app/assets/javascripts/application.js +15 -0
  32. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  33. data/test/dummy/app/controllers/application_controller.rb +3 -0
  34. data/test/dummy/app/helpers/application_helper.rb +2 -0
  35. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  36. data/test/dummy/config/application.rb +59 -0
  37. data/test/dummy/config/boot.rb +10 -0
  38. data/test/dummy/config/database.yml +25 -0
  39. data/test/dummy/config/environment.rb +5 -0
  40. data/test/dummy/config/environments/development.rb +39 -0
  41. data/test/dummy/config/environments/production.rb +67 -0
  42. data/test/dummy/config/environments/test.rb +37 -0
  43. data/test/dummy/config/initializers/api_explorer.rb +2 -0
  44. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  45. data/test/dummy/config/initializers/inflections.rb +15 -0
  46. data/test/dummy/config/initializers/mime_types.rb +5 -0
  47. data/test/dummy/config/initializers/secret_token.rb +7 -0
  48. data/test/dummy/config/initializers/session_store.rb +8 -0
  49. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  50. data/test/dummy/config/locales/en.yml +5 -0
  51. data/test/dummy/config/routes.rb +4 -0
  52. data/test/dummy/config.ru +4 -0
  53. data/test/dummy/db/development.sqlite3 +0 -0
  54. data/test/dummy/lib/file2.json +19 -0
  55. data/test/dummy/log/development.log +34015 -0
  56. data/test/dummy/public/404.html +26 -0
  57. data/test/dummy/public/422.html +26 -0
  58. data/test/dummy/public/500.html +25 -0
  59. data/test/dummy/public/favicon.ico +0 -0
  60. data/test/dummy/script/rails +6 -0
  61. data/test/dummy/tmp/cache/assets/BCA/FB0/sprockets%2Fe82897852042905e6a55f41605476313 +0 -0
  62. data/test/dummy/tmp/cache/assets/BF5/890/sprockets%2F684520e56b0024497f60b05747f55726 +0 -0
  63. data/test/dummy/tmp/cache/assets/C71/0A0/sprockets%2F440cecbe76112423851222560ad76a69 +0 -0
  64. data/test/dummy/tmp/cache/assets/C76/1D0/sprockets%2F14bb2119e8211782e884ac51420c844f +0 -0
  65. data/test/dummy/tmp/cache/assets/C86/740/sprockets%2Fd291ae7ec9685900b4e9e61805540154 +0 -0
  66. data/test/dummy/tmp/cache/assets/C8D/630/sprockets%2Fbbf5677980d2960c627d13474229c09f +0 -0
  67. data/test/dummy/tmp/cache/assets/CD5/440/sprockets%2F231be739243c2143636084ed780ccbbf +0 -0
  68. data/test/dummy/tmp/cache/assets/CEB/9F0/sprockets%2F85995403fefce2339fb188022b0639fc +0 -0
  69. data/test/dummy/tmp/cache/assets/CEC/210/sprockets%2F410259cd9c7949aaa82a889a9a305c77 +0 -0
  70. data/test/dummy/tmp/cache/assets/D0E/BB0/sprockets%2F63c187da761f36afe42a39ffe1574300 +0 -0
  71. data/test/dummy/tmp/cache/assets/D34/1D0/sprockets%2Fa04676c94303a7caa9014ff0f40eed65 +0 -0
  72. data/test/dummy/tmp/cache/assets/D3F/600/sprockets%2F258e09a6a3c12804b9a1bac297589dee +0 -0
  73. data/test/dummy/tmp/cache/assets/D43/AF0/sprockets%2F2ea31d99937e20b3979db19a0afb5e54 +0 -0
  74. data/test/dummy/tmp/cache/assets/D43/C10/sprockets%2F72328aa49e1e8a195434d4a2c3fbed99 +0 -0
  75. data/test/dummy/tmp/cache/assets/D50/950/sprockets%2F4714ce629d693fd95c1fd16ff7773a1b +0 -0
  76. data/test/dummy/tmp/cache/assets/D75/A90/sprockets%2F69a9c4dd529aa26bed3d8c85217d282c +0 -0
  77. data/test/dummy/tmp/cache/assets/D7B/0A0/sprockets%2F012437e537bed2cf9787aaf79db6e81c +0 -0
  78. data/test/dummy/tmp/cache/assets/D8B/310/sprockets%2F8c1640fafd6202b711c3da935daead71 +0 -0
  79. data/test/dummy/tmp/cache/assets/D8E/620/sprockets%2Faf16b0cae770d1267bc54cb11af0979b +0 -0
  80. data/test/dummy/tmp/cache/assets/D91/A50/sprockets%2F5b7712f13ba0c0a7acaad46b6897e2e5 +0 -0
  81. data/test/dummy/tmp/cache/assets/DAE/590/sprockets%2Fa3bfa2a9b53e4a0cd5db1f075a7301b2 +0 -0
  82. data/test/dummy/tmp/cache/assets/DC0/9D0/sprockets%2F82f4ea5964ff684c24ff37fcfec6a986 +0 -0
  83. data/test/dummy/tmp/cache/assets/DDD/8B0/sprockets%2F2641be1d4b59fd5fd668e5ab696ede5b +0 -0
  84. data/test/dummy/tmp/cache/assets/E0D/7A0/sprockets%2F96836bff2f6ddbbbe38383e65bd3cff1 +0 -0
  85. data/test/dummy/tmp/cache/assets/E18/210/sprockets%2Fc57cf0c8d4dea938d2ba30a6a3ebc01a +0 -0
  86. data/test/dummy/tmp/cache/assets/E2B/C80/sprockets%2F2e57f0fe8fbcd9317bdaa4b2ad250ef9 +0 -0
  87. data/test/dummy/tmp/cache/assets/E44/090/sprockets%2Fd0c29ad3e8eb0ab42df8ae45a0aab36b +0 -0
  88. data/test/dummy/tmp/cache/assets/ECB/620/sprockets%2Ff199edb8fca027ddc9cbdcf471cfd8fc +0 -0
  89. data/test/functional/api_explorer/api_controller_test.rb +9 -0
  90. data/test/integration/navigation_test.rb +10 -0
  91. data/test/test_helper.rb +15 -0
  92. data/test/unit/helpers/api_explorer/api_helper_test.rb +6 -0
  93. metadata +235 -0
@@ -0,0 +1,103 @@
1
+ require_dependency "api_explorer/application_controller"
2
+
3
+ module ApiExplorer
4
+ class ApiController < ApplicationController
5
+ before_filter :read_file
6
+
7
+ def index
8
+
9
+ end
10
+
11
+ def method
12
+ @method = @methods[params[:position].to_i - 1]
13
+
14
+ render :json=> {
15
+ :parameters_html=> (
16
+ render_to_string("api_explorer/api/parameters", :locals=>{:parameters=>@method['parameters']} ,:layout => false)
17
+ ), :description=>@method['description']
18
+ }
19
+ end
20
+
21
+ def execute
22
+ require 'net/http'
23
+ headers = params[:header][:name].zip(params[:header][:value])
24
+ headers.select!{|header| !header[0].empty? && !header[1].empty?}
25
+
26
+ headers.map!{|header| {:name=> header[0], :value=> header[1]}}
27
+
28
+ uri = URI::HTTP.build(:host=>request.host, :port=> request.port, :path=>'/' + params[:url])
29
+ http = Net::HTTP.new(uri.host, uri.port)
30
+
31
+ if params[:method].upcase == 'GET'
32
+ request = Net::HTTP::Get.new(uri.request_uri)
33
+ elsif params[:method].upcase == 'POST'
34
+ request = Net::HTTP::Post.new(uri.request_uri)
35
+ elsif params[:method].upcase == 'PUT'
36
+ request = Net::HTTP::Put.new(uri.request_uri)
37
+ elsif params[:method].upcase == 'DELETE'
38
+ request = Net::HTTP::Delete.new(uri.request_uri)
39
+ end
40
+
41
+ request.set_form_data( params.except([:action, :controller, :header, :authentication_type, :auth]) )
42
+ headers.each do |header|
43
+ request[header[:name]] = header[:value]
44
+ end
45
+ if params[:authentication_type] == 'basic_auth'
46
+ request.basic_auth params[:auth][:basic_auth_user], params[:auth][:basic_auth_password]
47
+ elsif params[:authentication_type] == 'hash_url_auth'
48
+ string_to_convert = uri.to_s
49
+ hash_verification = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), params[:auth][:secret_key], string_to_convert)).strip
50
+ request[params[:auth][:header_field]] = hash_verification
51
+ end
52
+
53
+
54
+ response = http.request(request)
55
+
56
+ raw_response = response.body
57
+ response_html = ''
58
+
59
+ if response.header['Content-Type'].include? 'application/json'
60
+ tokens = CodeRay.scan(JSON.pretty_generate(JSON.parse(raw_response)), :json)
61
+ response_html = tokens.div
62
+ elsif response.header['Content-Type'].include? 'application/xml'
63
+ tokens = CodeRay.scan(raw_response, :xml)
64
+ response_html = tokens.div
65
+ elsif response.header['Content-Type'].include? 'text/html'
66
+ tokens = CodeRay.scan(raw_response, :html)
67
+ response_html = tokens.div
68
+ else
69
+ response_html = '<div>' + raw_response + '</div>'
70
+ end
71
+
72
+ request_html = CodeRay.scan(request.to_yaml, :yaml).div
73
+
74
+
75
+ curr_time = DateTime.now
76
+ timestamp = curr_time.strftime('%Y%m%d%H%M%S%L')
77
+
78
+ history_html = render_to_string 'api_explorer/api/history', :locals=>{:request_html=>request_html,
79
+ :response_html=>response_html, :timestamp=>timestamp}, :layout=>false
80
+
81
+
82
+
83
+ render :json =>{ :response_html=> response_html , :request_html => request_html,
84
+ :history_html=>history_html, :date=> curr_time.strftime('%H:%M:%S'),
85
+ :timestamp => timestamp,
86
+ :http_method=>params[:method].upcase, :request_url=>params[:url]
87
+ } , :layout=>false
88
+
89
+ end
90
+ protected
91
+ def read_file
92
+ json_string = ''
93
+ if ApiExplorer::use_file
94
+ json_path = ApiExplorer::json_path
95
+ json_string = File.read(json_path)
96
+ else
97
+ json_string = ApiExplorer::json_string
98
+ end
99
+
100
+ @methods = JSON.parse(json_string)["methods"]
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,4 @@
1
+ module ApiExplorer
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ApiExplorer
2
+ module ApiHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ApiExplorer
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,36 @@
1
+ <div id="authentication_container" class="fields_section">
2
+ <div id="clear_authentication" title="Clear authentication"></div>
3
+ <div id="minimize_authentication" class="minimize" title="Minimize"></div>
4
+
5
+ <h2>Authentication</h2>
6
+ <br />
7
+ <div class="editor-label"> Method</div>
8
+
9
+ <%= select_tag 'authentication_type', options_for_select([['None', ''], ['Basic', 'basic_auth'], ["Hash URL", 'hash_url_auth']]) %>
10
+ <div id="basic_auth" class="auth_wrapper">
11
+ <div class="editor-label"> User</div>
12
+ <div class="editor-field"> <input type="text" placeholder="Value" name="auth[basic_auth_user]" class="parameter_input"/> </div>
13
+ <div class="editor-label"> Password</div>
14
+ <div class="editor-field"> <input type="text" placeholder="Value" name="auth[basic_auth_password]" class="parameter_input"/> </div>
15
+ </div>
16
+
17
+ <div id="hash_url_auth" class="auth_wrapper">
18
+ <div class="auth_method_description">
19
+ Hash authentication uses HMAC-SHA1 method to encrypt the requested URL using the specified secret key. It sends on the specified HTTP header the results, encoding it using Base 64.
20
+ </div>
21
+ <div class="information_field">
22
+ <div class="editor-label">Hash method</div>
23
+ <div class="editor-label">HMAC-SHA1</div>
24
+ <div style="clear: both"></div>
25
+ </div>
26
+ <div class="editor-label">Header field</div>
27
+ <div class="editor-field"> <input type="text" placeholder="Value" name="auth[header_field]" class="parameter_input"/> </div>
28
+ <div class="editor-label">Secret Key</div>
29
+ <div class="editor-field"> <input type="text" placeholder="Value" name="auth[secret_key]" class="parameter_input"/> </div>
30
+
31
+ </div>
32
+
33
+ <div id="hash_url_auth">
34
+ </div>
35
+
36
+ </div>
@@ -0,0 +1,16 @@
1
+ <div id="headers_container" class="fields_section">
2
+ <div id="clear_headers" title="Clear parameters"></div>
3
+ <div id="minimize_headers" class="minimize" title="Minimize"></div>
4
+
5
+ <h2>HTTP Headers</h2>
6
+ <div class="headers_line">
7
+ <div class="header_field">
8
+ <input type="text" placeholder="Header name" name="header[name][]" class="parameter_input" />
9
+ </div>
10
+ <div class="header_field">
11
+ <input type="text" placeholder="Value" name="header[value][]" class="parameter_input" />
12
+ </div>
13
+ </div>
14
+
15
+
16
+ </div>
@@ -0,0 +1,44 @@
1
+ <div id="response_placeholder" class="fields_section">
2
+ <h2>Requests</h2>
3
+ <div class="tabs">
4
+
5
+ <div class="tab tab_request">
6
+ <input type="radio" id="tab-1" name="tab-group-1">
7
+ <label for="tab-1">Request</label>
8
+
9
+ <div class="content overflow">
10
+ Please make a request
11
+ </div>
12
+ </div>
13
+
14
+ <div class="tab tab_response">
15
+ <input type="radio" id="tab-2" name="tab-group-1" checked="">
16
+ <label for="tab-2">Response</label>
17
+
18
+ <div class="content overflow">
19
+ Please make a request
20
+ </div>
21
+ </div>
22
+
23
+ <div class="tab tab_history">
24
+ <input type="radio" id="tab-3" name="tab-group-1">
25
+ <label for="tab-3">History</label>
26
+
27
+ <div class="content overflow">
28
+ <div class="history_wrapper">
29
+ <div class="history_content">
30
+ </div>
31
+ <div class="history_menu">
32
+
33
+ </div>
34
+ <div style="clear:both">
35
+ </div>
36
+ </div>
37
+
38
+ </div>
39
+ </div>
40
+
41
+ </div>
42
+
43
+ </div>
44
+
@@ -0,0 +1,3 @@
1
+ <div class="content_wrapper <%= timestamp %>">
2
+ <%= response_html.html_safe %>
3
+ </div>
@@ -0,0 +1,42 @@
1
+ <div class="panel_header">
2
+ API Explorer
3
+ </div>
4
+ <div class="panel_body">
5
+ <select class="methods" id="api_methods">
6
+ <option value="">Select one</option>
7
+ <%= @methods.each do |method| %>
8
+ <option data-method="<%= method["method"] %>" value="<%= method["url"] %>"><%= method["name"]%> (<%= method["method"]%> <%= method["url"]%> )</option>
9
+ <% end %>
10
+
11
+
12
+ </select>
13
+ <br /><br />
14
+ <div id="method_description"></div>
15
+
16
+
17
+
18
+ <form class="mvm" id="graph_form" action="" method="get">
19
+ <table>
20
+ <tbody>
21
+ <tr>
22
+ <td>
23
+ <span id="api_prefix" title="Show host"> → / </span>
24
+ </td>
25
+ <td class="full_width_cell">
26
+ <input type="text" class="inputtext" id="graph_path" name="path">
27
+ </td>
28
+ <td></td>
29
+ <td>
30
+ <button value="1" class="" id="api_explorer_submit" type="submit">Try</button>
31
+ </td>
32
+ </tr>
33
+ </tbody>
34
+ </table>
35
+ </form>
36
+ <div id="parameters_container" class="fields_section"></div>
37
+ <%= render 'headers' %>
38
+ <%= render 'authentication' %>
39
+ <%= render 'response' %>
40
+
41
+
42
+ </div>
@@ -0,0 +1,8 @@
1
+ <div id="clear_parameters" title="Clear parameters"></div>
2
+ <div id="minimize_parameters" class="minimize" title="Minimize"></div>
3
+
4
+ <h2>Parameters</h2>
5
+ <% parameters.each do |parameter| %>
6
+ <div class="editor-label"> <%= parameter['name'] %> </div>
7
+ <div class="editor-field"> <input type="text" placeholder="Value" name="<%= parameter['name']%>" class="parameter_input"/> </div>
8
+ <% end %>
@@ -0,0 +1,16 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>ApiExplorer</title>
5
+ <%= stylesheet_link_tag "api_explorer/application", :media => "all" %>
6
+ <%= javascript_include_tag "api_explorer/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body class="api_explorer">
10
+ <div class="container">
11
+ <div class="panel">
12
+ <%= yield %>
13
+ </div>
14
+ </div>
15
+ </body>
16
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,6 @@
1
+ ApiExplorer::Engine.routes.draw do
2
+ root :to => "api#index"
3
+
4
+ get :method, :to=>'api#method'
5
+ post :execute, :to=>'api#execute'
6
+ end
@@ -0,0 +1,13 @@
1
+
2
+ module ApiExplorer
3
+ class Engine < ::Rails::Engine
4
+ isolate_namespace ApiExplorer
5
+
6
+
7
+ end
8
+
9
+ def self.config(&block)
10
+ yield Engine.config if block
11
+ Engine.config
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module ApiExplorer
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,11 @@
1
+ require "api_explorer/engine"
2
+
3
+ module ApiExplorer
4
+ mattr_accessor :json_string
5
+ mattr_accessor :json_path
6
+ mattr_accessor :use_file
7
+
8
+ self.use_file = true
9
+ self.json_path = '/lib/file.json'
10
+
11
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :api_explorer do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class ApiExplorerTest < ActiveSupport::TestCase
4
+ test "truth" do
5
+ assert_kind_of Module, ApiExplorer
6
+ end
7
+ end
@@ -0,0 +1,261 @@
1
+ == Welcome to Rails
2
+
3
+ Rails is a web-application framework that includes everything needed to create
4
+ database-backed web applications according to the Model-View-Control pattern.
5
+
6
+ This pattern splits the view (also called the presentation) into "dumb"
7
+ templates that are primarily responsible for inserting pre-built data in between
8
+ HTML tags. The model contains the "smart" domain objects (such as Account,
9
+ Product, Person, Post) that holds all the business logic and knows how to
10
+ persist themselves to a database. The controller handles the incoming requests
11
+ (such as Save New Account, Update Product, Show Post) by manipulating the model
12
+ and directing data to the view.
13
+
14
+ In Rails, the model is handled by what's called an object-relational mapping
15
+ layer entitled Active Record. This layer allows you to present the data from
16
+ database rows as objects and embellish these data objects with business logic
17
+ methods. You can read more about Active Record in
18
+ link:files/vendor/rails/activerecord/README.html.
19
+
20
+ The controller and view are handled by the Action Pack, which handles both
21
+ layers by its two parts: Action View and Action Controller. These two layers
22
+ are bundled in a single package due to their heavy interdependence. This is
23
+ unlike the relationship between the Active Record and Action Pack that is much
24
+ more separate. Each of these packages can be used independently outside of
25
+ Rails. You can read more about Action Pack in
26
+ link:files/vendor/rails/actionpack/README.html.
27
+
28
+
29
+ == Getting Started
30
+
31
+ 1. At the command prompt, create a new Rails application:
32
+ <tt>rails new myapp</tt> (where <tt>myapp</tt> is the application name)
33
+
34
+ 2. Change directory to <tt>myapp</tt> and start the web server:
35
+ <tt>cd myapp; rails server</tt> (run with --help for options)
36
+
37
+ 3. Go to http://localhost:3000/ and you'll see:
38
+ "Welcome aboard: You're riding Ruby on Rails!"
39
+
40
+ 4. Follow the guidelines to start developing your application. You can find
41
+ the following resources handy:
42
+
43
+ * The Getting Started Guide: http://guides.rubyonrails.org/getting_started.html
44
+ * Ruby on Rails Tutorial Book: http://www.railstutorial.org/
45
+
46
+
47
+ == Debugging Rails
48
+
49
+ Sometimes your application goes wrong. Fortunately there are a lot of tools that
50
+ will help you debug it and get it back on the rails.
51
+
52
+ First area to check is the application log files. Have "tail -f" commands
53
+ running on the server.log and development.log. Rails will automatically display
54
+ debugging and runtime information to these files. Debugging info will also be
55
+ shown in the browser on requests from 127.0.0.1.
56
+
57
+ You can also log your own messages directly into the log file from your code
58
+ using the Ruby logger class from inside your controllers. Example:
59
+
60
+ class WeblogController < ActionController::Base
61
+ def destroy
62
+ @weblog = Weblog.find(params[:id])
63
+ @weblog.destroy
64
+ logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
65
+ end
66
+ end
67
+
68
+ The result will be a message in your log file along the lines of:
69
+
70
+ Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1!
71
+
72
+ More information on how to use the logger is at http://www.ruby-doc.org/core/
73
+
74
+ Also, Ruby documentation can be found at http://www.ruby-lang.org/. There are
75
+ several books available online as well:
76
+
77
+ * Programming Ruby: http://www.ruby-doc.org/docs/ProgrammingRuby/ (Pickaxe)
78
+ * Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
79
+
80
+ These two books will bring you up to speed on the Ruby language and also on
81
+ programming in general.
82
+
83
+
84
+ == Debugger
85
+
86
+ Debugger support is available through the debugger command when you start your
87
+ Mongrel or WEBrick server with --debugger. This means that you can break out of
88
+ execution at any point in the code, investigate and change the model, and then,
89
+ resume execution! You need to install ruby-debug to run the server in debugging
90
+ mode. With gems, use <tt>sudo gem install ruby-debug</tt>. Example:
91
+
92
+ class WeblogController < ActionController::Base
93
+ def index
94
+ @posts = Post.all
95
+ debugger
96
+ end
97
+ end
98
+
99
+ So the controller will accept the action, run the first line, then present you
100
+ with a IRB prompt in the server window. Here you can do things like:
101
+
102
+ >> @posts.inspect
103
+ => "[#<Post:0x14a6be8
104
+ @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>,
105
+ #<Post:0x14a6620
106
+ @attributes={"title"=>"Rails", "body"=>"Only ten..", "id"=>"2"}>]"
107
+ >> @posts.first.title = "hello from a debugger"
108
+ => "hello from a debugger"
109
+
110
+ ...and even better, you can examine how your runtime objects actually work:
111
+
112
+ >> f = @posts.first
113
+ => #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
114
+ >> f.
115
+ Display all 152 possibilities? (y or n)
116
+
117
+ Finally, when you're ready to resume execution, you can enter "cont".
118
+
119
+
120
+ == Console
121
+
122
+ The console is a Ruby shell, which allows you to interact with your
123
+ application's domain model. Here you'll have all parts of the application
124
+ configured, just like it is when the application is running. You can inspect
125
+ domain models, change values, and save to the database. Starting the script
126
+ without arguments will launch it in the development environment.
127
+
128
+ To start the console, run <tt>rails console</tt> from the application
129
+ directory.
130
+
131
+ Options:
132
+
133
+ * Passing the <tt>-s, --sandbox</tt> argument will rollback any modifications
134
+ made to the database.
135
+ * Passing an environment name as an argument will load the corresponding
136
+ environment. Example: <tt>rails console production</tt>.
137
+
138
+ To reload your controllers and models after launching the console run
139
+ <tt>reload!</tt>
140
+
141
+ More information about irb can be found at:
142
+ link:http://www.rubycentral.org/pickaxe/irb.html
143
+
144
+
145
+ == dbconsole
146
+
147
+ You can go to the command line of your database directly through <tt>rails
148
+ dbconsole</tt>. You would be connected to the database with the credentials
149
+ defined in database.yml. Starting the script without arguments will connect you
150
+ to the development database. Passing an argument will connect you to a different
151
+ database, like <tt>rails dbconsole production</tt>. Currently works for MySQL,
152
+ PostgreSQL and SQLite 3.
153
+
154
+ == Description of Contents
155
+
156
+ The default directory structure of a generated Ruby on Rails application:
157
+
158
+ |-- app
159
+ | |-- assets
160
+ | | |-- images
161
+ | | |-- javascripts
162
+ | | `-- stylesheets
163
+ | |-- controllers
164
+ | |-- helpers
165
+ | |-- mailers
166
+ | |-- models
167
+ | `-- views
168
+ | `-- layouts
169
+ |-- config
170
+ | |-- environments
171
+ | |-- initializers
172
+ | `-- locales
173
+ |-- db
174
+ |-- doc
175
+ |-- lib
176
+ | |-- assets
177
+ | `-- tasks
178
+ |-- log
179
+ |-- public
180
+ |-- script
181
+ |-- test
182
+ | |-- fixtures
183
+ | |-- functional
184
+ | |-- integration
185
+ | |-- performance
186
+ | `-- unit
187
+ |-- tmp
188
+ | `-- cache
189
+ | `-- assets
190
+ `-- vendor
191
+ |-- assets
192
+ | |-- javascripts
193
+ | `-- stylesheets
194
+ `-- plugins
195
+
196
+ app
197
+ Holds all the code that's specific to this particular application.
198
+
199
+ app/assets
200
+ Contains subdirectories for images, stylesheets, and JavaScript files.
201
+
202
+ app/controllers
203
+ Holds controllers that should be named like weblogs_controller.rb for
204
+ automated URL mapping. All controllers should descend from
205
+ ApplicationController which itself descends from ActionController::Base.
206
+
207
+ app/models
208
+ Holds models that should be named like post.rb. Models descend from
209
+ ActiveRecord::Base by default.
210
+
211
+ app/views
212
+ Holds the template files for the view that should be named like
213
+ weblogs/index.html.erb for the WeblogsController#index action. All views use
214
+ eRuby syntax by default.
215
+
216
+ app/views/layouts
217
+ Holds the template files for layouts to be used with views. This models the
218
+ common header/footer method of wrapping views. In your views, define a layout
219
+ using the <tt>layout :default</tt> and create a file named default.html.erb.
220
+ Inside default.html.erb, call <% yield %> to render the view using this
221
+ layout.
222
+
223
+ app/helpers
224
+ Holds view helpers that should be named like weblogs_helper.rb. These are
225
+ generated for you automatically when using generators for controllers.
226
+ Helpers can be used to wrap functionality for your views into methods.
227
+
228
+ config
229
+ Configuration files for the Rails environment, the routing map, the database,
230
+ and other dependencies.
231
+
232
+ db
233
+ Contains the database schema in schema.rb. db/migrate contains all the
234
+ sequence of Migrations for your schema.
235
+
236
+ doc
237
+ This directory is where your application documentation will be stored when
238
+ generated using <tt>rake doc:app</tt>
239
+
240
+ lib
241
+ Application specific libraries. Basically, any kind of custom code that
242
+ doesn't belong under controllers, models, or helpers. This directory is in
243
+ the load path.
244
+
245
+ public
246
+ The directory available for the web server. Also contains the dispatchers and the
247
+ default HTML files. This should be set as the DOCUMENT_ROOT of your web
248
+ server.
249
+
250
+ script
251
+ Helper scripts for automation and generation.
252
+
253
+ test
254
+ Unit and functional tests along with fixtures. When using the rails generate
255
+ command, template test files will be generated for you and placed in this
256
+ directory.
257
+
258
+ vendor
259
+ External libraries that the application depends on. Also includes the plugins
260
+ subdirectory. If the app has frozen rails, those gems also go here, under
261
+ vendor/rails/. This directory is in the load path.
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
3
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
4
+
5
+ require File.expand_path('../config/application', __FILE__)
6
+
7
+ Dummy::Application.load_tasks
@@ -0,0 +1,15 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // the compiled file.
9
+ //
10
+ // WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11
+ // GO AFTER THE REQUIRES BELOW.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end