Class: HCl::TimesheetResource

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

Direct Known Subclasses

DayEntry, Project, Task

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ TimesheetResource

Returns a new instance of TimesheetResource.



25
26
27
# File 'lib/hcl/timesheet_resource.rb', line 25

def initialize params
  @data = params
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



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

def method_missing method, *args
  if @data.key? method.to_sym
    @data[method]
  else
    super
  end
end

Class Method Details

.configure(opts = nil) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/hcl/timesheet_resource.rb', line 3

def self.configure opts = nil
  if opts
    self. = opts['login']
    self.password = opts['password']
    self.subdomain = opts['subdomain']
  else
    yield self
  end
end

.get(action) ⇒ Object



29
30
31
# File 'lib/hcl/timesheet_resource.rb', line 29

def self.get action
  https_do Net::HTTP::Get, action
end

.https_do(method_class, action, data = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hcl/timesheet_resource.rb', line 37

def self.https_do method_class, action, data = nil
  https   = Net::HTTP.new "#{subdomain}.harvestapp.com", 443
  request = method_class.new "/#{action}"
  https.use_ssl = true
  request.basic_auth , password
  request.content_type = 'application/xml'
  request['Accept']    = 'application/xml'
  response = https.request request, data
  return response.body
  if response.kind_of? Net::HTTPSuccess
    response.body
  else
    raise 'failure'
  end
end

.post(action, data) ⇒ Object



33
34
35
# File 'lib/hcl/timesheet_resource.rb', line 33

def self.post action, data
  https_do Net::HTTP::Post, action, data
end

.xml_to_hash(elem) ⇒ Object



65
66
67
68
69
70
# File 'lib/hcl/timesheet_resource.rb', line 65

def self.xml_to_hash elem
  elem.elements.map { |e| e.name }.inject({}) do |a, f|
    a[f.to_sym] = elem.elements[f].text if elem.elements[f]
    a
  end
end

Instance Method Details

#idObject



53
54
55
# File 'lib/hcl/timesheet_resource.rb', line 53

def id
  @data[:id]
end