Class: Kanbantastic::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations, HTTParty
Defined in:
lib/kanbantastic/base.rb

Direct Known Subclasses

Column, Task

Constant Summary collapse

RECTIFY_TIME_FOR =
[:updated_at, :created_at, :moved_at]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Base

Returns a new instance of Base.



12
13
14
15
# File 'lib/kanbantastic/base.rb', line 12

def initialize(config)
  @config = config
  self.valid?
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



10
11
12
# File 'lib/kanbantastic/base.rb', line 10

def config
  @config
end

Class Method Details

.find_project_id(project_name, workspace_name, api_key) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/kanbantastic/base.rb', line 65

def self.find_project_id(project_name, workspace_name, api_key)
  setup_headers(api_key)
  workspaces = get(base_uri(workspace_name) + "/user/workspaces.json").parsed_response
  if workspace_name
    workspace = workspaces.select{|w| w['name'] == workspace_name }.first
    project = workspace['projects'].select{|p| p['name'] == project_name }.first if workspace
  end
  return project['id'] if project
end

.invalid_response_error(msg, response) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/kanbantastic/base.rb', line 79

def self.invalid_response_error(msg, response)
  begin
    text = response.map{|k,v| v.map{|e| "#{k} #{e}" }.join(', ')}.join(', ')
  rescue
    raise msg
  end
  raise "#{msg} #{text}"
end

.request(method, config, url, options = {}) ⇒ Object



75
76
77
# File 'lib/kanbantastic/base.rb', line 75

def self.request(method, config, url, options={})
  new(config).send(method.to_s, url, options)
end

Instance Method Details

#assign_attributes(params = {}) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/kanbantastic/base.rb', line 57

def assign_attributes(params={})
  params.each do |key, value|
    method_name = key.to_s+"="
    self.send(method_name, value) if self.respond_to?(method_name)
  end
  return self
end

#get(url, options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/kanbantastic/base.rb', line 17

def get(url, options={})
  check_parameter_format(:url => [url, String], :options => [options, Hash])

  self.class.setup_headers(config.api_key)
  response = self.class.get(self.class.base_uri(config.workspace) + url, options)
  if response.code == 200
    self.class.parse_response(response)
  else
    raise response.headers['status']
  end
end

#post(url, options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/kanbantastic/base.rb', line 29

def post(url, options={})
  check_parameter_format(:url => [url, String], :options => [options, Hash])

  self.class.setup_headers(config.api_key)
  response = self.class.post(self.class.base_uri(config.workspace) + url, options)
  if response.code == 201
    self.class.parse_response(response)
  else
    raise response.headers['status']
  end
end

#project_idObject



53
54
55
# File 'lib/kanbantastic/base.rb', line 53

def project_id
  config.project_id
end

#put(url, options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/kanbantastic/base.rb', line 41

def put(url, options={})
  check_parameter_format(:url => [url, String], :options => [options, Hash])

  self.class.setup_headers(config.api_key)
  response = self.class.put(self.class.base_uri(config.workspace) + url, options)
  if response.code == 200
    self.class.parse_response(response)
  else
    raise response.headers['status']
  end
end