api_resource 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. metadata +179 -123
  2. data/.document +0 -5
  3. data/.rspec +0 -5
  4. data/.travis.yml +0 -4
  5. data/Gemfile +0 -37
  6. data/Gemfile.lock +0 -190
  7. data/Guardfile +0 -27
  8. data/Rakefile +0 -49
  9. data/VERSION +0 -1
  10. data/api_resource.gemspec +0 -180
  11. data/lib/api_resource.rb +0 -130
  12. data/lib/api_resource/association_activation.rb +0 -19
  13. data/lib/api_resource/associations.rb +0 -218
  14. data/lib/api_resource/associations/association_proxy.rb +0 -116
  15. data/lib/api_resource/associations/belongs_to_remote_object_proxy.rb +0 -16
  16. data/lib/api_resource/associations/dynamic_resource_scope.rb +0 -23
  17. data/lib/api_resource/associations/generic_scope.rb +0 -68
  18. data/lib/api_resource/associations/has_many_remote_object_proxy.rb +0 -16
  19. data/lib/api_resource/associations/has_many_through_remote_object_proxy.rb +0 -13
  20. data/lib/api_resource/associations/has_one_remote_object_proxy.rb +0 -24
  21. data/lib/api_resource/associations/multi_argument_resource_scope.rb +0 -15
  22. data/lib/api_resource/associations/multi_object_proxy.rb +0 -84
  23. data/lib/api_resource/associations/related_object_hash.rb +0 -12
  24. data/lib/api_resource/associations/relation_scope.rb +0 -25
  25. data/lib/api_resource/associations/resource_scope.rb +0 -32
  26. data/lib/api_resource/associations/scope.rb +0 -132
  27. data/lib/api_resource/associations/single_object_proxy.rb +0 -82
  28. data/lib/api_resource/attributes.rb +0 -243
  29. data/lib/api_resource/base.rb +0 -717
  30. data/lib/api_resource/callbacks.rb +0 -45
  31. data/lib/api_resource/connection.rb +0 -195
  32. data/lib/api_resource/core_extensions.rb +0 -7
  33. data/lib/api_resource/custom_methods.rb +0 -117
  34. data/lib/api_resource/decorators.rb +0 -6
  35. data/lib/api_resource/decorators/caching_decorator.rb +0 -20
  36. data/lib/api_resource/exceptions.rb +0 -99
  37. data/lib/api_resource/formats.rb +0 -22
  38. data/lib/api_resource/formats/json_format.rb +0 -25
  39. data/lib/api_resource/formats/xml_format.rb +0 -36
  40. data/lib/api_resource/local.rb +0 -12
  41. data/lib/api_resource/log_subscriber.rb +0 -15
  42. data/lib/api_resource/mocks.rb +0 -277
  43. data/lib/api_resource/model_errors.rb +0 -82
  44. data/lib/api_resource/observing.rb +0 -27
  45. data/lib/api_resource/railtie.rb +0 -24
  46. data/lib/api_resource/scopes.rb +0 -48
  47. data/nohup.out +0 -63
  48. data/spec/lib/api_resource_spec.rb +0 -43
  49. data/spec/lib/associations_spec.rb +0 -751
  50. data/spec/lib/attributes_spec.rb +0 -191
  51. data/spec/lib/base_spec.rb +0 -655
  52. data/spec/lib/callbacks_spec.rb +0 -68
  53. data/spec/lib/connection_spec.rb +0 -137
  54. data/spec/lib/local_spec.rb +0 -20
  55. data/spec/lib/mocks_spec.rb +0 -45
  56. data/spec/lib/model_errors_spec.rb +0 -29
  57. data/spec/lib/prefixes_spec.rb +0 -107
  58. data/spec/spec_helper.rb +0 -82
  59. data/spec/support/mocks/association_mocks.rb +0 -63
  60. data/spec/support/mocks/error_resource_mocks.rb +0 -21
  61. data/spec/support/mocks/prefix_model_mocks.rb +0 -5
  62. data/spec/support/mocks/test_resource_mocks.rb +0 -44
  63. data/spec/support/requests/association_requests.rb +0 -31
  64. data/spec/support/requests/error_resource_requests.rb +0 -25
  65. data/spec/support/requests/prefix_model_requests.rb +0 -7
  66. data/spec/support/requests/test_resource_requests.rb +0 -38
  67. data/spec/support/test_resource.rb +0 -72
  68. data/spec/tmp/DIR +0 -0
@@ -1,68 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- include ApiResource
4
-
5
- describe "Should put callbacks around save, create, update, and destroy by default" do
6
-
7
- before(:all) do
8
- # This defines all the callbacks to check and see if they are fired
9
- TestResource.class_eval <<-EOE, __FILE__, __LINE__ + 1
10
- attr_accessor :s_val, :c_val, :u_val, :d_val
11
- before_save :bs_cb; after_save :as_cb
12
- before_create :bc_cb; after_create :ac_cb
13
- before_update :bu_cb; after_update :au_cb
14
- before_destroy :bd_cb; after_destroy :ad_cb
15
-
16
- def bs_cb
17
- @s_val = 1
18
- end
19
- def as_cb
20
- @s_val += 1
21
- end
22
- def bc_cb
23
- @c_val = 1
24
- end
25
- def ac_cb
26
- @c_val += 1
27
- end
28
- def bu_cb
29
- @u_val = 1
30
- end
31
- def au_cb
32
- @u_val += 1
33
- end
34
- def bd_cb
35
- @d_val = 1
36
- end
37
- def ad_cb
38
- @d_val += 1
39
- end
40
- EOE
41
- end
42
-
43
- it "should fire save and create callbacks when saving a new record" do
44
- tr = TestResource.new(:name => "Ethan", :age => 20)
45
- tr.save.should be_true
46
- tr.s_val.should eql(2)
47
- tr.c_val.should eql(2)
48
- tr.u_val.should be_nil
49
- end
50
-
51
- it "should fire save and update callbacks when updating a record" do
52
- tr = TestResource.new(:id => 1, :name => "Ethan", :age => 20)
53
- tr.name = "Test"
54
- tr.age = 21
55
- tr.save.should be_true
56
- tr.s_val.should eql(2)
57
- tr.c_val.should be_nil
58
- tr.u_val.should eql(2)
59
- end
60
-
61
- it "should only fire destroy callbacks when destroying a record" do
62
- tr = TestResource.new(:id => 1, :name => "Ethan", :age => 20)
63
- tr.destroy.should be_true
64
- tr.d_val.should eql(2)
65
- tr.s_val.should be_nil
66
- end
67
-
68
- end
@@ -1,137 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- include ApiResource
4
-
5
- describe Connection do
6
-
7
- it "should be able to set the token directly on ApiResource" do
8
- ApiResource.token = "123"
9
- ApiResource::Base.token.should eql "123"
10
- end
11
-
12
- it "should be able to set a default token value, which is passed through each request" do
13
- ApiResource::Mocks::Connection.expects(:get).with("/test_resources/1.json", {"Accept"=>"application/json", "Lifebooker-Token" => "abc"}).returns(ApiResource::Mocks::MockResponse.new({}))
14
- ApiResource::Base.token = "abc"
15
- TestResource.find(1)
16
- end
17
-
18
- it "should set the Lifebooker-Token if one is present for GET requests" do
19
- token = Kernel.rand(100000).to_s
20
- ApiResource::Mocks::Connection.expects(:get).with("/test_resources/1.json", {"Accept"=>"application/json", 'Lifebooker-Token' => "#{token}"}).returns(ApiResource::Mocks::MockResponse.new({}))
21
-
22
- ApiResource::Base.token = token
23
-
24
- TestResource.connection.get("/test_resources/1.json")
25
- end
26
-
27
- it "should set the Lifebooker-Token if one is present for DELETE requests" do
28
- token = Kernel.rand(100000).to_s
29
- ApiResource::Mocks::Connection.expects(:delete).with("/test_resources/1.json", {"Accept"=>"application/json", 'Lifebooker-Token' => "#{token}"}).returns(ApiResource::Mocks::MockResponse.new({}))
30
-
31
- ApiResource::Base.token = token
32
-
33
- TestResource.connection.delete("/test_resources/1.json")
34
- end
35
-
36
- it "should set the Lifebooker-Token if one is present for :head requests" do
37
- token = Kernel.rand(100000).to_s
38
- ApiResource::Mocks::Connection.expects(:head).with("/test_resources/1.json", {"Accept"=>"application/json", 'Lifebooker-Token' => "#{token}"}).returns(ApiResource::Mocks::MockResponse.new({}))
39
-
40
- ApiResource::Base.token = token
41
-
42
- TestResource.connection.head("/test_resources/1.json")
43
- end
44
-
45
- it "should set the Lifebooker-Token if one is present for POST requests" do
46
- token = Kernel.rand(100000).to_s
47
- ApiResource::Mocks::Connection.expects(:post).with("/test_resources/1.json", {}, {"Content-Type"=>"application/json", 'Lifebooker-Token' => "#{token}"}).returns(ApiResource::Mocks::MockResponse.new({}))
48
-
49
- ApiResource::Base.token = token
50
-
51
- TestResource.connection.post("/test_resources/1.json")
52
- end
53
-
54
- it "should set the Lifebooker-Token if one is present for PUT requests" do
55
- token = Kernel.rand(100000).to_s
56
- ApiResource::Mocks::Connection.expects(:put).with("/test_resources/1.json", {}, {"Content-Type"=>"application/json", 'Lifebooker-Token' => "#{token}"}).returns(ApiResource::Mocks::MockResponse.new({}))
57
-
58
- ApiResource::Base.token = token
59
-
60
- TestResource.connection.put("/test_resources/1.json")
61
- end
62
-
63
- it "should set its headers upon initialization" do
64
- token = Kernel.rand(100000).to_s
65
- ApiResource::Base.token = token
66
-
67
- TestResource.connection.headers.include?("Lifebooker-Token").should eql true
68
- TestResource.connection.headers["Lifebooker-Token"] = token
69
- end
70
-
71
- it "should reset headers upon initialization" do
72
- token = Kernel.rand(100000).to_s
73
- ApiResource::Base.token = token
74
-
75
- TestResource.connection.headers.include?("Lifebooker-Token").should eql true
76
- TestResource.connection.headers["Lifebooker-Token"] = token
77
- end
78
-
79
- it "should be able to set a token for a given block" do
80
- ApiResource::Base.token = "123456"
81
- begin
82
- ApiResource.with_token("testing") do
83
- ApiResource::Base.token.should eql "testing"
84
- raise "AAAH"
85
- end
86
- rescue => e
87
- # should still reset the token
88
- end
89
- ApiResource::Base.token.should eql "123456"
90
- end
91
-
92
- it "should provider a method to regenerate its connection" do
93
- conn = ApiResource::Base.connection
94
- conn.should be ApiResource::Base.connection
95
- ApiResource.reset_connection
96
- conn.should_not be ApiResource::Base.connection
97
- end
98
-
99
- context "No Mocks" do
100
- before(:all) do
101
- ApiResource::Mocks.remove
102
- end
103
- after(:all) do
104
- ApiResource::Mocks.init
105
- ApiResource.timeout = 10
106
- ApiResource.open_timeout = 10
107
- end
108
- it "should be able to set a timeout for its connection" do
109
- ApiResource.timeout = 1
110
- ApiResource.timeout.should eql 1
111
- ApiResource.open_timeout = 1
112
- ApiResource.open_timeout.should eql 1
113
-
114
- ApiResource::Base.connection.send(:http, "/test").options[:timeout].should eql 1
115
- ApiResource::Base.connection.send(:http, "/test").options[:open_timeout].should eql 1
116
-
117
- ApiResource.timeout = 100
118
- ApiResource::Base.connection.send(:http, "/test").options[:timeout].should eql 100
119
-
120
- end
121
-
122
- it "should time out if RestClient takes too long" do
123
-
124
- # hopefully google won't actually respond this fast :)
125
- ApiResource.timeout = 0.001
126
- ApiResource::Base.site = "http://www.google.com"
127
- lambda{
128
- ApiResource::Base.connection.get("/")
129
- }.should raise_error(ApiResource::RequestTimeout)
130
-
131
- end
132
-
133
- end
134
-
135
-
136
-
137
- end
@@ -1,20 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
- require 'json'
3
-
4
- include ApiResource
5
-
6
- describe "Local" do
7
-
8
- it "should not go to the server to fetch a resource definition" do
9
- ApiResource::Connection.any_instance.expects(:get).never
10
- class MyTestResource < ApiResource::Local
11
- scope :test, {:test => true}
12
- end
13
- mtr = MyTestResource.new
14
- # should still have scopes
15
- MyTestResource.reload_class_attributes
16
- mtr.scopes.should_not be_blank
17
-
18
- end
19
-
20
- end
@@ -1,45 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
- require 'json'
3
-
4
- include ApiResource
5
-
6
- describe Mocks do
7
-
8
- # we set up the mocks in spec helper, so we can just assert this
9
- it "should hijack the connection" do
10
- ApiResource::Mocks::Interface.any_instance.expects(:get).once.returns(
11
- ApiResource::Mocks::MockResponse.new({}, {:headers => {"Content-type" => "application/json"}, :status_code => 200})
12
- )
13
- TestResource.reload_class_attributes
14
- end
15
-
16
- it "should allow the user to raise errors for invalid responsed" do
17
- old_err_status = ApiResource.raise_missing_definition_error
18
- ApiResource::Base.raise_missing_definition_error = true
19
-
20
- lambda {
21
- class MyNewInvalidResource < ApiResource::Base; end
22
- MyNewInvalidResource.new
23
- }.should raise_error(ApiResource::ResourceNotFound)
24
-
25
- ApiResource.raise_missing_definition_error = old_err_status
26
- end
27
-
28
- it "should merge params for a request" do
29
- resp = ApiResource::Base.connection.get(
30
- "/mock_with_block/1?#{{:test => "abc"}.to_query}"
31
- )
32
- resp["id"].should eql "1"
33
- resp["test"].should eql "abc"
34
- end
35
-
36
- context "Mock Request" do
37
- context "Initialize" do
38
- it "should correctly assign blank params" do
39
- request = ApiResource::Mocks::MockRequest.new(:get, "/authenticate.json?token=")
40
- request.params.should eql({"token" => ""})
41
- end
42
- end
43
- end
44
-
45
- end
@@ -1,29 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- include ApiResource
4
-
5
- describe "Saving Resources with errors" do
6
-
7
- before(:all) do
8
- ErrorResource.include_root_in_json = true
9
- end
10
-
11
- context "Remote Errors" do
12
-
13
- it "should be able to handle errors as a hash" do
14
- t = ErrorResource.new(:name => "Ethan", :age => 12)
15
- t.save.should be_false
16
- t.errors.should_not be_nil
17
- t.errors['name'].should_not be_nil
18
- end
19
-
20
- it "should be able to handle errors as full messages" do
21
- t = ErrorFullMessageResource.new(:name => "Ethan", :age => 12)
22
- t.save.should be_false
23
- t.errors.should_not be_nil
24
- t.errors['name'].should_not be_nil
25
- end
26
-
27
- end
28
-
29
- end
@@ -1,107 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "With Prefixes" do
4
-
5
- let(:prefix_model) do
6
- PrefixModel.new({:foreign_key_id => "123", :name => "test"})
7
- end
8
-
9
- before(:each) do
10
- PrefixModel.reload_class_attributes
11
- end
12
-
13
- context ".find" do
14
-
15
- it "should use the prefix to find a single record when given as a param" do
16
- PrefixModel.connection.expects(:get)
17
- .with(
18
- "/foreign/123/prefix_models/456.json",
19
- instance_of(Hash)
20
- )
21
- .returns({})
22
- PrefixModel.find(456, :params => {:foreign_key_id => 123})
23
- end
24
-
25
- it "should not use the prefix to find a single record when not given as a param to avoid automatic failure" do
26
- PrefixModel.connection.expects(:get)
27
- .with(
28
- "/prefix_models/456.json",
29
- instance_of(Hash)
30
- )
31
- .returns({})
32
- PrefixModel.find(456)
33
- end
34
- end
35
-
36
- context "#create" do
37
-
38
- it "should use the prefix to create a new record" do
39
- prefix_model.send(:connection).expects(:post)
40
- .with(
41
- "/foreign/123/prefix_models.json",
42
- {"prefix_model" => {"name" => "test"}}.to_json,
43
- instance_of(Hash)
44
- )
45
- prefix_model.save
46
- end
47
-
48
- end
49
-
50
- context "#first" do
51
-
52
- it "should use the prefix to find records" do
53
- prefix_model.send(:connection).expects(:get)
54
- .with(
55
- "/foreign/123/prefix_models.json",
56
- instance_of(Hash)
57
- )
58
- .returns([])
59
- PrefixModel.first(:params => {:foreign_key_id => 123})
60
- end
61
-
62
- it "should not use the prefix to find records when not given as a param to avoid automatic failure" do
63
- prefix_model.send(:connection).expects(:get)
64
- .with(
65
- "/prefix_models.json",
66
- instance_of(Hash)
67
- )
68
- .returns([])
69
- PrefixModel.first
70
- end
71
-
72
- end
73
-
74
- context "#destroy" do
75
-
76
- it "should use the prefix to destroy a record" do
77
-
78
- prefix_model.id = 456
79
- prefix_model.send(:connection).expects(:delete)
80
- .with(
81
- "/foreign/123/prefix_models/456.json",
82
- instance_of(Hash)
83
- )
84
- prefix_model.destroy
85
-
86
- end
87
-
88
- end
89
-
90
- context "#update" do
91
-
92
- it "should use the prefix to update a record" do
93
- prefix_model.id = 456
94
- prefix_model.name = "changed name"
95
- prefix_model.send(:connection).expects(:put)
96
- .with(
97
- "/foreign/123/prefix_models/456.json",
98
- {"prefix_model" => {"name" => "changed name"}}.to_json,
99
- instance_of(Hash)
100
- )
101
- prefix_model.save
102
-
103
- end
104
-
105
- end
106
-
107
- end
data/spec/spec_helper.rb DELETED
@@ -1,82 +0,0 @@
1
- require 'rubygems'
2
- require 'spork'
3
- #uncomment the following line to use spork with the debugger
4
- #require 'spork/ext/ruby-debug'
5
-
6
- Spork.prefork do
7
- # Loading more in this block will cause your tests to run faster. However,
8
- # if you change any configuration or code from libraries loaded here, you'll
9
- # need to restart spork for it take effect.
10
-
11
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
12
- $LOAD_PATH.unshift(File.dirname(__FILE__))
13
- require 'bundler'
14
- require 'api_resource'
15
- require 'simplecov'
16
-
17
-
18
- SimpleCov.start do
19
- add_filter "/spec/"
20
- end
21
-
22
- SimpleCov.at_exit do
23
- SimpleCov.result.format!
24
- end
25
-
26
- # Requires supporting files with custom matchers and macros, etc,
27
- # in ./support/ and its subdirectories.
28
- Bundler.require(:default, :development)
29
- Debugger.start
30
-
31
- # Requires supporting files with custom matchers and macros, etc,
32
- # in ./support/ and its subdirectories.
33
- #ApiResource.load_mocks_and_factories
34
- ApiResource.site = 'http://localhost:3000'
35
- ApiResource.format = :json
36
- ApiResource.load_mocks_and_factories
37
-
38
- ApiResource.logger.level = Log4r::INFO
39
-
40
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
41
-
42
-
43
-
44
- RSpec.configure do |config|
45
- config.mock_with :mocha
46
- end
47
-
48
- end
49
-
50
- Spork.each_run do
51
- # This code will be run each time you run your specs.
52
-
53
- end
54
-
55
- # --- Instructions ---
56
- # Sort the contents of this file into a Spork.prefork and a Spork.each_run
57
- # block.
58
- #
59
- # The Spork.prefork block is run only once when the spork server is started.
60
- # You typically want to place most of your (slow) initializer code in here, in
61
- # particular, require'ing any 3rd-party gems that you don't normally modify
62
- # during development.
63
- #
64
- # The Spork.each_run block is run each time you run your specs. In case you
65
- # need to load files that tend to change during development, require them here.
66
- # With Rails, your application modules are loaded automatically, so sometimes
67
- # this block can remain empty.
68
- #
69
- # Note: You can modify files loaded *from* the Spork.each_run block without
70
- # restarting the spork server. However, this file itself will not be reloaded,
71
- # so if you change any of the code inside the each_run block, you still need to
72
- # restart the server. In general, if you have non-trivial code in this file,
73
- # it's advisable to move it into a separate file so you can easily edit it
74
- # without restarting spork. (For example, with RSpec, you could move
75
- # non-trivial code into a file spec/support/my_helper.rb, making sure that the
76
- # spec/support/* files are require'd from inside the each_run block.)
77
- #
78
- # Any code that is left outside the two blocks will be run during preforking
79
- # *and* during each_run -- that's probably not what you want.
80
- #
81
- # These instructions should self-destruct in 10 seconds. If they don't, feel
82
- # free to delete them.