Sha256: 8aadaecdaaab13c12c05a962536691a5aac949393318cebe2d607fdd938d734e

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

module Wordnik
  
  class Operation
    require 'active_model'
    include ActiveModel::Validations
    include ActiveModel::Conversion
    extend ActiveModel::Naming
  
    attr_accessor :endpoint, :http_method, :summary, :notes, :parameters, :response, :open, 
      :nickname, :deprecated, :category, :error_responses, :response_class
  
    validates_presence_of :endpoint, :http_method, :summary, :notes, :parameters, :response, :open
  
    def initialize(endpoint, attributes = {})
      self.endpoint = endpoint
      
      attributes.each do |name, value|
        send("#{name.to_s.underscore.to_sym}=", value)
      end
      
      # munge that meth!
      self.http_method = self.http_method.to_s.downcase
    
      # Generate OperationParameter instances from JSON
      if self.parameters
        self.parameters = self.parameters.map do |parameterData|
          OperationParameter.new(parameterData)
        end
      else
        self.parameters = []
      end
      
      self.nickname = self.nickname.underscore
    end
    
    # A globally unique identifier for the operation
    def slug
      [self.endpoint.resource.name, self.nickname].join("_")
    end
      
    def get?
      self.http_method.downcase == "get"
    end
  
    # Can this operation be run in the sandbox?
    def sandboxable?
      self.get?
    end

    # It's an ActiveModel thing..
    def persisted?
      false
    end

    def positional_parameter_names
      self.parameters.select(&:positional?).compact.map(&:name)
    end
    
    def required_kwargs
      self.parameters.map do |parameter|
        next if parameter.name.to_sym == :format
        next if parameter.positional?
        next unless parameter.required?
        parameter
      end.compact
    end
    
    def optional_kwargs
      self.parameters.map do |parameter|
        next if parameter.required?
        parameter
      end.compact      
    end

  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wordnik-4.06.06 lib/wordnik/operation.rb