class MingleAPI

Constants

CARD
PROJECT
VERSION

Attributes

site[R]

Public Class Methods

new(site, credentials) click to toggle source
# File lib/mingle-api.rb, line 29
def initialize(site, credentials)
  @site, @http = site, Http.new(credentials)
end

Public Instance Methods

card(project, number) click to toggle source
# File lib/mingle-api.rb, line 57
def card(project, number)
  ostruct(fetch(:projects, project_identifier(project), :cards, number), CARD)
end
cards(project) click to toggle source
# File lib/mingle-api.rb, line 53
def cards(project)
  list(fetch(:projects, project_identifier(project), :cards), CARD)
end
create_card(project, attrs) click to toggle source
# File lib/mingle-api.rb, line 61
def create_card(project, attrs)
  params = [
    ["card[name]", attrs[:name]],
    ["card[card_type_name]", attrs[:type]],
    ["card[description]", attrs[:description]]
  ]
  Array(attrs[:attachments]).each_with_index do |attachment, i|
    path, content_type = attachment
    params << ["attachments[#{i}]", UploadIO.new(File.new(path), content_type, File.basename(path))]
  end
  Array(attrs[:properties]).each_with_index do |prop, i|
    name, value = prop
    params << ["card[properties][][name]", name]
    params << ["card[properties][][value]", value]
  end
  resp = @http.post(v2(:projects, project_identifier(project), :cards), params)
  number = resp[2]["location"].split("/").last.split('.').first
  OpenStruct.new(:number => number, :url => url('projects', project_identifier(project), 'cards', number))
end
create_project(name, options={}) click to toggle source
# File lib/mingle-api.rb, line 41
def create_project(name, options={})
  identifier = name.downcase.gsub(/[^a-z0-9_]/, '_')
  @http.post(v2(:projects), {
    "project[name]" => name,
    "project[identifier]" => identifier,
    "project[description]" => options[:description],
    "project[template]" => false,
    "template_name" => options[:template] ? "yml_#{options[:template]}_template" : nil
  })
  OpenStruct.new(:identifier => identifier, :name => name, :url => url('projects', identifier))
end
project(identifier) click to toggle source
# File lib/mingle-api.rb, line 37
def project(identifier)
  ostruct(fetch(:projects, identifier), PROJECT)
end
projects() click to toggle source
# File lib/mingle-api.rb, line 33
def projects
  list(fetch(:projects), PROJECT)
end
site_api_url() click to toggle source
# File lib/mingle-api.rb, line 81
def site_api_url
  gen_site_url(api_host)
end
site_url() click to toggle source
# File lib/mingle-api.rb, line 85
def site_url
  gen_site_url(host)
end

Private Instance Methods

api_host() click to toggle source
# File lib/mingle-api.rb, line 98
def api_host
  'mingle-api.thoughtworks.com' || ENV['MINGLE_API_HOST']
end
dom(xml) click to toggle source
# File lib/mingle-api.rb, line 121
def dom(xml)
  Nokogiri::XML(xml) do |config|
    config.options = Nokogiri::XML::ParseOptions::NOBLANKS | Nokogiri::XML::ParseOptions::STRICT | Nokogiri::XML::ParseOptions::NONET
  end
end
fetch(*resources) click to toggle source
# File lib/mingle-api.rb, line 106
def fetch(*resources)
  dom(@http.get(v2(*resources))[1]).root
end
gen_site_url(h) click to toggle source
# File lib/mingle-api.rb, line 131
def gen_site_url(h)
  site =~ /^http/i ? site : "https://#{site}.#{h}"
end
host() click to toggle source
# File lib/mingle-api.rb, line 102
def host
  'mingle.thoughtworks.com' || ENV['MINGLE_HOST']
end
list(dom, attrs) click to toggle source
# File lib/mingle-api.rb, line 110
def list(dom, attrs)
  dom.children.map do |child|
    ostruct(child, attrs)
  end
end
ostruct(node, attrs) click to toggle source
# File lib/mingle-api.rb, line 116
def ostruct(node, attrs)
  macro = Macro.new
  OpenStruct.new(Hash[*macro.apply(node, attrs)])
end
project_identifier(proj) click to toggle source
# File lib/mingle-api.rb, line 90
def project_identifier(proj)
  proj.respond_to?(:identifier) ? proj.identifier : proj.to_s
end
url(*parts) click to toggle source
# File lib/mingle-api.rb, line 94
def url(*parts)
  File.join(site_url, *parts)
end
v2(*parts) click to toggle source
# File lib/mingle-api.rb, line 127
def v2(*parts)
  [File.join(site_api_url, 'api/v2', *parts.map(&:to_s)), 'xml'].join('.')
end