Class: Mingle

Inherits:
Object
  • Object
show all
Defined in:
lib/mingle.rb

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
  }
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, credentials) ⇒ Mingle

Returns a new instance of Mingle.



28
29
30
# File 'lib/mingle.rb', line 28

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

Instance Attribute Details

#siteObject (readonly)

Returns the value of attribute site.



9
10
11
# File 'lib/mingle.rb', line 9

def site
  @site
end

Instance Method Details

#card(project, number) ⇒ Object



56
57
58
# File 'lib/mingle.rb', line 56

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

#cards(project) ⇒ Object



52
53
54
# File 'lib/mingle.rb', line 52

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

#create_card(project, attrs) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mingle.rb', line 60

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



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mingle.rb', line 40

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



36
37
38
# File 'lib/mingle.rb', line 36

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

#projectsObject



32
33
34
# File 'lib/mingle.rb', line 32

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

#site_api_urlObject



80
81
82
# File 'lib/mingle.rb', line 80

def site_api_url
  gen_site_url(api_host)
end

#site_urlObject



84
85
86
# File 'lib/mingle.rb', line 84

def site_url
  gen_site_url(host)
end