Module: JSONAPIonify::Api::Action::Documentation
- Included in:
- JSONAPIonify::Api::Action
- Defined in:
- lib/jsonapionify/api/action/documentation.rb
Instance Method Summary collapse
- #documentation_object(base, resource, name, include_path, label) ⇒ Object
- #example_input(resource) ⇒ Object
- #example_requests(resource, url) ⇒ Object
- #sample_context(resource) ⇒ Object
- #sample_request(resource, request) ⇒ Object
Instance Method Details
#documentation_object(base, resource, name, include_path, label) ⇒ Object
3 4 5 6 7 8 9 10 11 |
# File 'lib/jsonapionify/api/action/documentation.rb', line 3 def documentation_object(base, resource, name, include_path, label) url = build_path(base, name.to_s, include_path) path = URI.parse(url).path OpenStruct.new( id: [request_method, path].join('-').parameterize, label: label, sample_requests: example_requests(resource, url) ) end |
#example_input(resource) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/jsonapionify/api/action/documentation.rb', line 13 def example_input(resource) request = Server::Request.env_for('http://example.org', request_method) context = resource.new( request: request, context_definitions: sample_context(resource) ).exec { |c| c } case @example_input when :resource { 'data' => resource.build_resource( context: context, instance: resource.example_instance_for_action(name, context), links: false ).as_json }.to_json when :resource_identifier { 'data' => resource.build_resource_identifier( instance: resource.example_instance_for_action(name, context) ).as_json }.to_json when Proc @example_input.call end end |
#example_requests(resource, url) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/jsonapionify/api/action/documentation.rb', line 39 def example_requests(resource, url) responses.map do |response| opts = {} opts['CONTENT_TYPE'] = content_type if @example_input accept = response.accept || response.example_accept opts['HTTP_ACCEPT'] = accept if content_type == 'application/vnd.api+json' && @example_input opts[:input] = example_input(resource) end url = "#{url}.#{response.extension}" if response.extension request = Server::Request.env_for(url, request_method, opts) response = Server::MockResponse.new(*sample_request(resource, request)) OpenStruct.new( request: request.http_string, response: response.http_string ) end end |
#sample_context(resource) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/jsonapionify/api/action/documentation.rb', line 59 def sample_context(resource) resource.context_definitions.dup.tap do |defs| collection_context = proc do |context| 3.times.map { resource.example_instance_for_action(action.name, context) } end defs[:_is_example_] = Context.new(:_is_example_, readonly: true) { true } defs[:collection] = Context.new(:collection, &collection_context) defs[:paginated_collection] = Context.new(:paginated_collection) { |collection:| collection } defs[:instance] = Context.new(:instance, readonly: true) { |collection:| collection.first } defs[:owner_context] = Context.new(:owner_context, readonly: true) { ContextDelegate::Mock.new } if defs.has_key? :owner_context end end |
#sample_request(resource, request) ⇒ Object
72 73 74 |
# File 'lib/jsonapionify/api/action/documentation.rb', line 72 def sample_request(resource, request) call(resource, request, context_definitions: sample_context(resource), callbacks: false) end |