apify 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/.gitignore +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +25 -0
  4. data/Rakefile +40 -0
  5. data/VERSION +1 -0
  6. data/apify.gemspec +103 -0
  7. data/app/helpers/apify_helper.rb +14 -0
  8. data/app/views/apify/api/_actions.html.erb +38 -0
  9. data/app/views/apify/api/_client.html.erb +38 -0
  10. data/app/views/apify/api/_overview.html.erb +9 -0
  11. data/app/views/apify/api/_protocol.html.erb +38 -0
  12. data/app/views/apify/api/docs.html.erb +66 -0
  13. data/lib/apify.rb +7 -0
  14. data/lib/apify/action.rb +98 -0
  15. data/lib/apify/api.rb +65 -0
  16. data/lib/apify/api_controller.rb +99 -0
  17. data/lib/apify/client.rb +52 -0
  18. data/lib/apify/errors.rb +12 -0
  19. data/lib/apify/exchange.rb +53 -0
  20. data/lib/apify/schema_helper.rb +56 -0
  21. data/spec/apify/action_spec.rb +35 -0
  22. data/spec/apify/client_spec.rb +24 -0
  23. data/spec/app_root/app/controllers/api_controller.rb +8 -0
  24. data/spec/app_root/app/controllers/application_controller.rb +3 -0
  25. data/spec/app_root/app/models/api.rb +42 -0
  26. data/spec/app_root/config/boot.rb +114 -0
  27. data/spec/app_root/config/database.yml +21 -0
  28. data/spec/app_root/config/environment.rb +14 -0
  29. data/spec/app_root/config/environments/in_memory.rb +0 -0
  30. data/spec/app_root/config/environments/mysql.rb +0 -0
  31. data/spec/app_root/config/environments/postgresql.rb +0 -0
  32. data/spec/app_root/config/environments/sqlite.rb +0 -0
  33. data/spec/app_root/config/environments/sqlite3.rb +0 -0
  34. data/spec/app_root/config/routes.rb +12 -0
  35. data/spec/app_root/lib/console_with_fixtures.rb +4 -0
  36. data/spec/app_root/script/console +7 -0
  37. data/spec/controllers/api_controller_spec.rb +155 -0
  38. data/spec/rcov.opts +2 -0
  39. data/spec/spec.opts +4 -0
  40. data/spec/spec_helper.rb +31 -0
  41. metadata +151 -0
@@ -0,0 +1,114 @@
1
+ # Allow customization of the rails framework path
2
+ RAILS_FRAMEWORK_ROOT = (ENV['RAILS_FRAMEWORK_ROOT'] || "#{File.dirname(__FILE__)}/../../../../../../vendor/rails") unless defined?(RAILS_FRAMEWORK_ROOT)
3
+
4
+ # Don't change this file!
5
+ # Configure your app in config/environment.rb and config/environments/*.rb
6
+
7
+ RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
8
+
9
+ module Rails
10
+ class << self
11
+ def boot!
12
+ unless booted?
13
+ preinitialize
14
+ pick_boot.run
15
+ end
16
+ end
17
+
18
+ def booted?
19
+ defined? Rails::Initializer
20
+ end
21
+
22
+ def pick_boot
23
+ (vendor_rails? ? VendorBoot : GemBoot).new
24
+ end
25
+
26
+ def vendor_rails?
27
+ File.exist?(RAILS_FRAMEWORK_ROOT)
28
+ end
29
+
30
+ def preinitialize
31
+ load(preinitializer_path) if File.exist?(preinitializer_path)
32
+ end
33
+
34
+ def preinitializer_path
35
+ "#{RAILS_ROOT}/config/preinitializer.rb"
36
+ end
37
+ end
38
+
39
+ class Boot
40
+ def run
41
+ load_initializer
42
+ Rails::Initializer.run(:set_load_path)
43
+ end
44
+ end
45
+
46
+ class VendorBoot < Boot
47
+ def load_initializer
48
+ require "#{RAILS_FRAMEWORK_ROOT}/railties/lib/initializer"
49
+ Rails::Initializer.run(:install_gem_spec_stubs)
50
+ end
51
+ end
52
+
53
+ class GemBoot < Boot
54
+ def load_initializer
55
+ self.class.load_rubygems
56
+ load_rails_gem
57
+ require 'initializer'
58
+ end
59
+
60
+ def load_rails_gem
61
+ if version = self.class.gem_version
62
+ gem 'rails', version
63
+ else
64
+ gem 'rails'
65
+ end
66
+ rescue Gem::LoadError => load_error
67
+ $stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
68
+ exit 1
69
+ end
70
+
71
+ class << self
72
+ def rubygems_version
73
+ Gem::RubyGemsVersion rescue nil
74
+ end
75
+
76
+ def gem_version
77
+ if defined? RAILS_GEM_VERSION
78
+ RAILS_GEM_VERSION
79
+ elsif ENV.include?('RAILS_GEM_VERSION')
80
+ ENV['RAILS_GEM_VERSION']
81
+ else
82
+ parse_gem_version(read_environment_rb)
83
+ end
84
+ end
85
+
86
+ def load_rubygems
87
+ require 'rubygems'
88
+ min_version = '1.1.1'
89
+ unless rubygems_version >= min_version
90
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
91
+ exit 1
92
+ end
93
+
94
+ rescue LoadError
95
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
96
+ exit 1
97
+ end
98
+
99
+ def parse_gem_version(text)
100
+ $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
101
+ end
102
+
103
+ private
104
+ def read_environment_rb
105
+ environment_rb = "#{RAILS_ROOT}/config/environment.rb"
106
+ environment_rb = "#{HELPER_RAILS_ROOT}/config/environment.rb" unless File.exists?(environment_rb)
107
+ File.read(environment_rb)
108
+ end
109
+ end
110
+ end
111
+ end
112
+
113
+ # All that for this:
114
+ Rails.boot!
@@ -0,0 +1,21 @@
1
+ in_memory:
2
+ adapter: sqlite3
3
+ database: ":memory:"
4
+ verbosity: quiet
5
+ sqlite:
6
+ adapter: sqlite
7
+ dbfile: plugin_test.sqlite.db
8
+ sqlite3:
9
+ adapter: sqlite3
10
+ dbfile: plugin_test.sqlite3.db
11
+ postgresql:
12
+ adapter: postgresql
13
+ username: postgres
14
+ password: postgres
15
+ database: plugin_test
16
+ mysql:
17
+ adapter: mysql
18
+ host: localhost
19
+ username: root
20
+ password:
21
+ database: plugin_test
@@ -0,0 +1,14 @@
1
+ require File.join(File.dirname(__FILE__), 'boot')
2
+
3
+ Rails::Initializer.run do |config|
4
+ config.cache_classes = false
5
+ config.whiny_nils = true
6
+ config.action_controller.session = { :key => "_myapp_session", :secret => "gwirofjweroijger8924rt2zfwehfuiwehb1378rifowenfoqwphf23" }
7
+ config.plugin_locators.unshift(
8
+ Class.new(Rails::Plugin::Locator) do
9
+ def plugins
10
+ [Rails::Plugin.new(File.expand_path('.'))]
11
+ end
12
+ end
13
+ ) unless defined?(PluginTestHelper::PluginLocator)
14
+ end
@@ -0,0 +1,12 @@
1
+ ActionController::Routing::Routes.draw do |map|
2
+
3
+ Api.draw_routes(map)
4
+
5
+ # map.connect 'api/hello', :controller => 'api', :action => 'hello', :conditions => { :method => :get }
6
+ # map.connect 'api/fail', :controller => 'api', :action => 'fail', :conditions => { :method => :get }
7
+ # map.connect 'api/echo_args', :controller => 'api', :action => 'echo_args', :conditions => { :method => :get }
8
+ # map.connect 'api/with_args_schema', :controller => 'api', :action => 'with_args_schema', :conditions => { :method => :get }
9
+ # map.connect 'api/with_value_schema', :controller => 'api', :action => 'with_value_schema', :conditions => { :method => :get }
10
+ # map.connect 'api/songs', :controller => 'api', :action => 'index_songs', :conditions => { :method => :get }
11
+
12
+ end
@@ -0,0 +1,4 @@
1
+ # Loads fixtures into the database when running the test app via the console
2
+ (ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : Dir.glob(File.join(Rails.root, '../fixtures/*.{yml,csv}'))).each do |fixture_file|
3
+ Fixtures.create_fixtures(File.join(Rails.root, '../fixtures'), File.basename(fixture_file, '.*'))
4
+ end
@@ -0,0 +1,7 @@
1
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
2
+ libs = " -r irb/completion"
3
+ libs << " -r test/test_helper"
4
+ libs << " -r console_app"
5
+ libs << " -r console_with_helpers"
6
+ libs << " -r console_with_fixtures"
7
+ exec "#{irb} #{libs} --simple-prompt"
@@ -0,0 +1,155 @@
1
+ require 'spec_helper'
2
+
3
+ describe ApiController do
4
+
5
+ authenticate = lambda do |*args|
6
+ user = args[0] || 'user'
7
+ password = args[1] || 'password'
8
+ request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(user, password)
9
+ end
10
+
11
+ describe 'authentication' do
12
+
13
+ it "should deny access with the wrong user" do
14
+ instance_exec('wrong-user', 'password', &authenticate)
15
+ post :ping
16
+ response.code.should == '401'
17
+ end
18
+
19
+ it "should deny access with the wrong password" do
20
+ instance_exec('user', 'wrong-password', &authenticate)
21
+ post :ping
22
+ response.code.should == '401'
23
+ end
24
+
25
+ it "should grant access with the correct user and password" do
26
+ instance_exec('user', 'password', &authenticate)
27
+ post :ping
28
+ response.code.should == '200'
29
+ end
30
+
31
+ it "should allow an option to skip authentication" do
32
+ controller.skip_authentication = true
33
+ post :ping
34
+ response.code.should == '200'
35
+ controller.skip_authentication = false
36
+ post :ping
37
+ response.code.should == '401'
38
+ end
39
+
40
+ end
41
+
42
+ describe 'error handling' do
43
+
44
+ it 'should fill exception messages into the response body and return a 500 error code' do
45
+ instance_exec(&authenticate)
46
+ post :fail
47
+ response.code.should == '500'
48
+ response.body == 'error message'
49
+ end
50
+
51
+ end
52
+
53
+ describe 'args' do
54
+
55
+ it 'should have access to the args' do
56
+ instance_exec(&authenticate)
57
+ args = {'foo' => 'bar'}
58
+ post :echo_args, :args => args.to_json
59
+ response.code.should == '200'
60
+ JSON.parse(response.body).should == args
61
+ end
62
+
63
+ end
64
+
65
+ describe 'argument schemas' do
66
+
67
+ it "should validate the presence of a property" do
68
+ instance_exec(&authenticate)
69
+ post :with_args_schema, :args => {}.to_json
70
+ response.code.should == '500'
71
+ response.body.should include('Invalid request args')
72
+ response.body.should include('string_arg is missing')
73
+ end
74
+
75
+ it "should validate value types" do
76
+ instance_exec(&authenticate)
77
+ post :with_args_schema, :args => {'string_arg' => 123}.to_json
78
+ response.code.should == '500'
79
+ response.body.should include('Invalid request args')
80
+ response.body.should include('a string is required')
81
+ end
82
+
83
+ it "should allow requests that fit the schema" do
84
+ instance_exec(&authenticate)
85
+ post :with_args_schema, :args => {'string_arg' => 'a string'}.to_json
86
+ response.code.should == '200'
87
+ end
88
+
89
+ it "should render the schema if requested" do
90
+ instance_exec(&authenticate)
91
+ post :with_args_schema, :schema => 'args'
92
+ response.code.should == '200'
93
+ JSON.parse(response.body).should == {
94
+ "type" => "object",
95
+ "properties" => {
96
+ "string_arg" => { "type" => "string" }
97
+ }}
98
+ end
99
+
100
+ end
101
+
102
+ describe 'value schemas' do
103
+
104
+ it "should validate the presence of a property" do
105
+ instance_exec(&authenticate)
106
+ post :with_value_schema, :args => {}.to_json
107
+ response.code.should == '500'
108
+ response.body.should include('Invalid response value')
109
+ response.body.should include('string_value is missing')
110
+ end
111
+
112
+ it "should validate value types" do
113
+ instance_exec(&authenticate)
114
+ post :with_value_schema, :args => {'string_value' => 123}.to_json
115
+ response.code.should == '500'
116
+ response.body.should include('Invalid response value')
117
+ response.body.should include('a string is required')
118
+ end
119
+
120
+ it "should return responses that fit the schema" do
121
+ instance_exec(&authenticate)
122
+ post :with_value_schema, :args => {'string_value' => 'a string'}.to_json
123
+ response.code.should == '200'
124
+ JSON.parse(response.body).should == {'string_value' => 'a string'}
125
+ end
126
+
127
+ it "should render the schema if requested" do
128
+ instance_exec(&authenticate)
129
+ post :with_value_schema, :schema => 'value'
130
+ response.code.should == '200'
131
+ JSON.parse(response.body).should == {
132
+ "type" => "object",
133
+ "properties" => {
134
+ "string_value" => { "type" => "string" }
135
+ }}
136
+ end
137
+
138
+ end
139
+
140
+ describe 'auto-generated documentation' do
141
+ integrate_views
142
+
143
+ it 'should render auto-generated API documentation' do
144
+ instance_exec(&authenticate)
145
+ get :docs
146
+ response.code.should == '200'
147
+ response.body.should include('ping')
148
+ response.body.should include('fail')
149
+ response.body.should include('echo_args')
150
+ end
151
+
152
+ end
153
+
154
+ end
155
+
@@ -0,0 +1,2 @@
1
+ --exclude "spec/*,gems/*"
2
+ --rails
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --format progress
3
+ --loadby mtime
4
+ --reverse
@@ -0,0 +1,31 @@
1
+ $: << File.join(File.dirname(__FILE__), "/../lib" )
2
+
3
+ # Set the default environment to sqlite3's in_memory database
4
+ ENV['RAILS_ENV'] ||= 'in_memory'
5
+
6
+ # Load the Rails environment and testing framework
7
+ require "#{File.dirname(__FILE__)}/app_root/config/environment"
8
+ require "#{File.dirname(__FILE__)}/../lib/apify"
9
+
10
+ require 'rubygems'
11
+
12
+ gem 'jsonschema', '=2.0.0'
13
+ gem 'webmock', '=1.3.4'
14
+ gem 'rest-client', '=1.5.1'
15
+
16
+ require 'spec/rails'
17
+ require 'jsonschema'
18
+ require 'webmock/rspec'
19
+ require 'restclient'
20
+
21
+ # Undo changes to RAILS_ENV
22
+ silence_warnings {RAILS_ENV = ENV['RAILS_ENV']}
23
+
24
+ # Run the migrations
25
+ ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate")
26
+
27
+ Spec::Runner.configure do |config|
28
+ config.use_transactional_fixtures = true
29
+ config.use_instantiated_fixtures = false
30
+ config.include WebMock
31
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: apify
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 3
8
+ - 0
9
+ version: 0.3.0
10
+ platform: ruby
11
+ authors:
12
+ - Henning Koch
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-08-28 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: json
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: jsonschema
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ version: "0"
42
+ type: :runtime
43
+ version_requirements: *id002
44
+ - !ruby/object:Gem::Dependency
45
+ name: rest-client
46
+ prerelease: false
47
+ requirement: &id003 !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ type: :runtime
55
+ version_requirements: *id003
56
+ description: "Compact definition of JSON APIs for Rails applications. "
57
+ email: github@makandra.de
58
+ executables: []
59
+
60
+ extensions: []
61
+
62
+ extra_rdoc_files:
63
+ - README.md
64
+ files:
65
+ - .gitignore
66
+ - MIT-LICENSE
67
+ - README.md
68
+ - Rakefile
69
+ - VERSION
70
+ - apify.gemspec
71
+ - app/helpers/apify_helper.rb
72
+ - app/views/apify/api/_actions.html.erb
73
+ - app/views/apify/api/_client.html.erb
74
+ - app/views/apify/api/_overview.html.erb
75
+ - app/views/apify/api/_protocol.html.erb
76
+ - app/views/apify/api/docs.html.erb
77
+ - lib/apify.rb
78
+ - lib/apify/action.rb
79
+ - lib/apify/api.rb
80
+ - lib/apify/api_controller.rb
81
+ - lib/apify/client.rb
82
+ - lib/apify/errors.rb
83
+ - lib/apify/exchange.rb
84
+ - lib/apify/schema_helper.rb
85
+ - spec/apify/action_spec.rb
86
+ - spec/apify/client_spec.rb
87
+ - spec/app_root/app/controllers/api_controller.rb
88
+ - spec/app_root/app/controllers/application_controller.rb
89
+ - spec/app_root/app/models/api.rb
90
+ - spec/app_root/config/boot.rb
91
+ - spec/app_root/config/database.yml
92
+ - spec/app_root/config/environment.rb
93
+ - spec/app_root/config/environments/in_memory.rb
94
+ - spec/app_root/config/environments/mysql.rb
95
+ - spec/app_root/config/environments/postgresql.rb
96
+ - spec/app_root/config/environments/sqlite.rb
97
+ - spec/app_root/config/environments/sqlite3.rb
98
+ - spec/app_root/config/routes.rb
99
+ - spec/app_root/lib/console_with_fixtures.rb
100
+ - spec/app_root/script/console
101
+ - spec/controllers/api_controller_spec.rb
102
+ - spec/rcov.opts
103
+ - spec/spec.opts
104
+ - spec/spec_helper.rb
105
+ has_rdoc: true
106
+ homepage: http://github.com/makandra/apify
107
+ licenses: []
108
+
109
+ post_install_message:
110
+ rdoc_options:
111
+ - --charset=UTF-8
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ segments:
119
+ - 0
120
+ version: "0"
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ segments:
126
+ - 0
127
+ version: "0"
128
+ requirements: []
129
+
130
+ rubyforge_project:
131
+ rubygems_version: 1.3.6
132
+ signing_key:
133
+ specification_version: 3
134
+ summary: Compact definition of JSON APIs for Rails applications.
135
+ test_files:
136
+ - spec/apify/action_spec.rb
137
+ - spec/apify/client_spec.rb
138
+ - spec/spec_helper.rb
139
+ - spec/app_root/app/controllers/api_controller.rb
140
+ - spec/app_root/app/controllers/application_controller.rb
141
+ - spec/app_root/app/models/api.rb
142
+ - spec/app_root/lib/console_with_fixtures.rb
143
+ - spec/app_root/config/environments/in_memory.rb
144
+ - spec/app_root/config/environments/mysql.rb
145
+ - spec/app_root/config/environments/postgresql.rb
146
+ - spec/app_root/config/environments/sqlite.rb
147
+ - spec/app_root/config/environments/sqlite3.rb
148
+ - spec/app_root/config/boot.rb
149
+ - spec/app_root/config/environment.rb
150
+ - spec/app_root/config/routes.rb
151
+ - spec/controllers/api_controller_spec.rb