Class: Blueprint::DesignContext

Inherits:
Object
  • Object
show all
Defined in:
lib/blueprint/api/rails.rb

Overview

The default design context for logging messages, etc. Also implements uitlity methods (like send())

Instance Method Summary collapse

Instance Method Details

#check_rules(source, target, type) ⇒ Object

checks that the action that the system just took does not violate any architectural rules



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/blueprint/api/rails.rb', line 95

def check_rules(source, target, type)
  # the list of violations for this action
  violations = [ ]

  # if all parameters are present then we can check for link rules
  if source.present? && target.present? && type.present?
    # check to see if there is a link rule for this source / target pair
    if RULES.has_key?(:links)
      link_rule = RULES[:links].find { |l| l[:source] == source && l[:target] == target }

      unless link_rule.nil?
        # there is a link rule - check that the type is allowed over the link
        unless link_rule[:allowed] == type
          violations << "The type #{type} is not allowed over the link #{source} -> #{target}."
        end
      end
    end
  end

  unless type.nil?
    # is the type allowed?
    if RULES.has_key?(:allowed)
      unless RULES[:allowed].map { |t| t[:name] }.include?(type)
        violations << "The type #{type} is not registered as being allowed."
      end
    end

    # is the type restricted to a certain structural element?
    unless RULES[type].nil?   # are there any rules for this type?

      if RULES[type].has_key?(:only)    # do the rules for this type include an 'only' restriction?
        allowed_element = RULES[type][:only]

        unless allowed_element.eql?(source)
          violations << "The type #{type} is allowed only within #{allowed_element} and it was used within #{source}."
        end
      end
    end
  end

  # output architectural violations
  unless violations.empty?
    p '### Architectural violations found ###'
    violations.each { |v|
      p '  ' + v
    }
    p '### End of violations ###'
  end
end

#determine_remote_repository(root_dir) ⇒ Object

figures out (and returns) the remote origin for a local git repository



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/blueprint/api/rails.rb', line 80

def determine_remote_repository(root_dir)
  Dir.chdir(root_dir) do
    git_remotes = `git remote show origin | grep 'Fetch URL: ' 2>&1`
    repo_url = git_remotes.match(/Fetch URL: (.*).git/).try(:captures)

    if !repo_url.empty?
      repo_url = repo_url[0]
      repo_url
    else
      nil
    end
  end
end

#send(event_type, properties = { }, message = { }) ⇒ Object

utility method for sending JSON payloads to the Anaxim servers - should never be called directly



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/blueprint/api/rails.rb', line 48

def send(event_type, properties = { }, message = { })

  # only send messages if we have an API key (even without one this gem can apply architectural verification rules)
  unless @api_key.nil?

    # construct the base payload
    payload = {
        :header => {
            :structure_id => @structure_id,
            :properties => properties,
            :branch => {
                :name => @branch || 'master'
            },
            :eventType => event_type
        }
    }

    # merge (append) the properties hash onto the payload (properties vary per event type)
    payload.merge! message

    @conn.post do |req|
      req.url BLUEPRINT_URL_CONTEXT
      req.headers['Content-Type'] = 'application/json'
      req.headers['X-Api-Key'] = @api_key
      req.body = payload.to_json
    end
  end

  self
end