Sha256: ad3b4fad07226558aebc35e4c7579a9fb40892a09cee94f437a9da718fc823c9

Contents?: true

Size: 1.85 KB

Versions: 5

Compression:

Stored size: 1.85 KB

Contents

module TryApi
  class Method < TryApi::Base
    typesafe_accessor :title, String
    typesafe_accessor :html, String
    typesafe_accessor :parameters, Array
    typesafe_accessor :headers, Array
    typesafe_accessor :path, String
    typesafe_accessor :method, String
    typesafe_accessor :example_responses, Array
    typesafe_accessor :project, TryApi::Project

    class << self
      def parse(hash:, project:)
        return nil if hash.blank?
        instance = self.new
        instance.project = project
        instance.title = hash[:title]
        instance.html = hash[:html]
        instance.parameters = hash[:parameters]
        instance.headers = hash[:headers]
        instance.method = hash[:method].try(:upcase)
        instance.path = hash[:path]

        instance.example_responses = []
        if hash[:example_responses].is_a? Array
          hash[:example_responses].each do |example|
            instance.example_responses << TryApi::ExampleResponse.parse(example)
          end
        else
        #   TODO raise exception ?
        end

        instance.parameters = []
        if hash[:parameters].is_a? Array
          hash[:parameters].each do |parameter|
            instance.parameters << TryApi::Parameter.parse(parameter)
          end
        else
        #   TODO raise exception ?
        end

        instance.headers = []
        if hash[:headers].is_a? Array
          hash[:headers].each do |header|
            instance.headers << TryApi::Header.parse(header)
          end
        else
        #   TODO raise exception ?
        end
        instance
      end
    end

    def to_json
      super.merge local_path: local_path, full_path: full_path
    end

    def full_path
      "#{ project.host }:#{ project.port }/#{ project.api_prefix }#{ self.path }"
    end

    def local_path
      "/#{ project.api_prefix }#{ self.path }"
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
try_api-0.0.10 app/models/try_api/method.rb
try_api-0.0.9 app/models/try_api/method.rb
try_api-0.0.8 app/models/try_api/method.rb
try_api-0.0.7 app/models/try_api/method.rb
try_api-0.0.6 app/models/try_api/method.rb