Sha256: d2a723649a21b5257dbf95a44e236f0c220d10e08c091fc41138912791a57b76

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

require 'json'

module Slurper
  class Story

    attr_accessor :attributes
    def initialize(attrs={})
      self.attributes = (attrs || {}).symbolize_keys

      if attributes[:owned_by].present?
        puts "DEPRECATION WARNING: `owned_by` is no longer a supported attribute. Please change to `requested_by`."
        attributes[:requested_by] = attributes[:owned_by]
      end
    end

    def to_json
      {
        name: name,
        description: description,
        story_type: story_type,
        labels: labels,
        estimate: estimate,
        requested_by_id: requested_by_id
      }.reject { |k,v| v.blank? }.to_json
    end

    def save
      (@response = Slurper::Client.create(self))

      @response.code == 200 or @response.code == 201
    end

    def error_message; @response.body end

    def name;       attributes[:name]       end
    def estimate;   attributes[:estimate]   end
    def story_type; attributes[:story_type] end

    def description
      return nil unless attributes[:description].present?
      attributes[:description].split("\n").map(&:strip).join("\n")
    end

    def labels
      return [] unless attributes[:labels].present?
      attributes[:labels].split(',').map do |tag|
        {name: tag}
      end
    end

    def requested_by
      attributes[:requested_by].presence || Slurper::Config.requested_by
    end

    def requested_by_id
      Slurper::User.find_by_name(requested_by).try(:id) if requested_by.present?
    end

    def valid?
      if name.blank?
        raise "Name is blank for story:\n#{to_json}"
      elsif !requested_by_id
        raise "Could not find requester \"#{requested_by}\" in project"
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slurper-2.2.2 lib/slurper/story.rb