Class: HCl::TimesheetResource

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

Direct Known Subclasses

DayEntry, Project, Task

Defined Under Namespace

Classes: AuthFailure, Failure, ThrottleFailure

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ TimesheetResource

Returns a new instance of TimesheetResource.



38
39
40
# File 'lib/hcl/timesheet_resource.rb', line 38

def initialize params
  @data = params
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



86
87
88
# File 'lib/hcl/timesheet_resource.rb', line 86

def method_missing method, *args
  @data.key?(method.to_sym) ? @data[method] : super
end

Class Method Details

.configure(opts = nil) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/hcl/timesheet_resource.rb', line 17

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

.connectObject



54
55
56
57
58
59
# File 'lib/hcl/timesheet_resource.rb', line 54

def self.connect
  Net::HTTP.new("#{subdomain}.harvestapp.com", (ssl ? 443 : 80)).tap do |https|
    https.use_ssl = ssl
    https.verify_mode = OpenSSL::SSL::VERIFY_NONE if ssl
  end
end

.delete(action) ⇒ Object



50
51
52
# File 'lib/hcl/timesheet_resource.rb', line 50

def self.delete action
  http_do Net::HTTP::Delete, action
end

.get(action) ⇒ Object



42
43
44
# File 'lib/hcl/timesheet_resource.rb', line 42

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

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



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

def self.http_do method_class, action, data = nil
  https = connect
  request = method_class.new "/#{action}"
  request.basic_auth , password
  request.content_type = 'application/xml'
  request['Accept']    = 'application/xml'
  response = https.request request, data
  case response
  when Net::HTTPSuccess
    response.body
  when Net::HTTPFound
    raise Failure, "Redirected! Perhaps your ssl configuration variable is set incorrectly?"
  when Net::HTTPServiceUnavailable
    raise ThrottleFailure, response
  when Net::HTTPUnauthorized
    raise AuthFailure, "Login failed."
  else
    raise Failure, "Unexpected response from the upstream API."
  end
end

.post(action, data) ⇒ Object



46
47
48
# File 'lib/hcl/timesheet_resource.rb', line 46

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

.xml_to_hash(elem) ⇒ Object



94
95
96
97
98
99
# File 'lib/hcl/timesheet_resource.rb', line 94

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

Instance Method Details

#idObject



82
83
84
# File 'lib/hcl/timesheet_resource.rb', line 82

def id
  @data[:id]
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/hcl/timesheet_resource.rb', line 90

def respond_to? method
  (@data && @data.key?(method.to_sym)) || super
end