Module: Her::WebMock::Model
- Includes:
- WebMock::API
- Defined in:
- lib/her/webmock/model.rb
Instance Method Summary collapse
- #stub_all(collection, options = {}) ⇒ Object
- #stub_associations(klass, object) ⇒ Object
- #stub_create(object, options = {}) ⇒ Object
- #stub_find(object, options = {}) ⇒ Object
Instance Method Details
#stub_all(collection, options = {}) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/her/webmock/model.rb', line 116 def stub_all(collection, = {}) model_class = self collection_attributes = collection.map { |object| Helper.(model_class, object) } response = { model_class.pluralized_parsed_root_element => collection_attributes } response = [:response_body].merge(response) if [:response_body] request_stub = stub_request(:get, model_class.use_api.[:url] + model_class.collection_path). to_return(body: JSON.generate(response), status: 200) request_params = Helper.request_params() request_stub.with(request_params) unless request_params.empty? collection.each { |object| stub_associations(model_class, object) } request_stub end |
#stub_associations(klass, object) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/her/webmock/model.rb', line 41 def stub_associations(klass, object) attributes = object.respond_to?(:attributes) ? object.attributes : object klass.associations.each do |type, | .each do || association = attributes[[:name]] if association case type when :belongs_to Object.const_get([:class_name]).stub_find(association) else # TODO fail NotImplementedError end end end end end |
#stub_create(object, options = {}) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/her/webmock/model.rb', line 61 def stub_create(object, = {}) model_class = self attributes = object.is_a?(Her::Model) ? Helper.(model_class, object) : object fail "Must pass in an object with an id attribute" unless attributes[:id] if model_class.parsed_root_element response = { model_class.parsed_root_element => attributes } else response = attributes end attributes_without_id = attributes.except(:id) request_stub = stub_request(:post, model_class.use_api.[:url] + model_class.build_request_path(attributes_without_id)). to_return(body: JSON.generate(response), status: 200) request_params = Helper.request_params() request_stub.with(request_params) unless request_params.empty? if [:stub_related] stub_find(object) stub_all([object]) end stub_associations(model_class, object) request_stub end |
#stub_find(object, options = {}) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/her/webmock/model.rb', line 93 def stub_find(object, = {}) model_class = self attributes = object.is_a?(Her::Model) ? Helper.(model_class, object) : object if model_class.parsed_root_element response = { model_class.parsed_root_element => attributes } else response = attributes end request_stub = stub_request(:get, model_class.use_api.[:url] + model_class.build_request_path(attributes)). to_return(body: JSON.generate(response), status: 200) request_params = Helper.request_params() request_stub.with(request_params) unless request_params.empty? stub_associations(model_class, object) request_stub end |