Class: MingleAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/mingle-api.rb,
lib/mingle-api/http.rb,
lib/mingle-api/macro.rb,
lib/mingle-api/version.rb

Defined Under Namespace

Classes: Http, Macro

Constant Summary collapse

PROJECT =
[:name, :identifier, :description]
CARD =
[:name, :description,
  {
    "card_type name" => :type,
    'properties property' => lambda do |node|
      name = text(node, 'name')
      value = case node[:type_description]
      when /team list/
        text(node, 'value login')
      else
        text(node, 'value')
      end
      [name.downcase, value]
    end
  }
]
VERSION =
"0.0.3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, credentials) ⇒ MingleAPI

Returns a new instance of MingleAPI.



29
30
31
# File 'lib/mingle-api.rb', line 29

def initialize(site, credentials)
  @site, @http = site, Http.new(credentials)
end

Instance Attribute Details

#siteObject (readonly)

Returns the value of attribute site.



10
11
12
# File 'lib/mingle-api.rb', line 10

def site
  @site
end

Instance Method Details

#card(project, number) ⇒ Object



57
58
59
# File 'lib/mingle-api.rb', line 57

def card(project, number)
  ostruct(fetch(:projects, project_identifier(project), :cards, number), CARD)
end

#cards(project) ⇒ Object



53
54
55
# File 'lib/mingle-api.rb', line 53

def cards(project)
  list(fetch(:projects, project_identifier(project), :cards), CARD)
end

#create_card(project, attrs) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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 = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# 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) ⇒ Object



37
38
39
# File 'lib/mingle-api.rb', line 37

def project(identifier)
  ostruct(fetch(:projects, identifier), PROJECT)
end

#projectsObject



33
34
35
# File 'lib/mingle-api.rb', line 33

def projects
  list(fetch(:projects), PROJECT)
end

#site_api_urlObject



81
82
83
# File 'lib/mingle-api.rb', line 81

def site_api_url
  gen_site_url(api_host)
end

#site_urlObject



85
86
87
# File 'lib/mingle-api.rb', line 85

def site_url
  gen_site_url(host)
end