Class: Mavenlink::Base
- Inherits:
-
Object
show all
- Includes:
- HTTParty
- Defined in:
- lib/mavenlink/base.rb,
lib/mavenlink/client.rb
Direct Known Subclasses
AdditionalItem, Asset, Client, Event, Expense, Invoice, Participant, Post, Story, TimeEntry, User, Workspace, WorkspaceInvitation
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(json, options = {}) ⇒ Base
Returns a new instance of Base.
11
12
13
14
15
|
# File 'lib/mavenlink/base.rb', line 11
def initialize(json, options = {})
self.path_params = options[:path_params] || {}
self.basic_auth = options[:basic_auth]
set_json json
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method) ⇒ Object
165
166
167
|
# File 'lib/mavenlink/base.rb', line 165
def method_missing(method)
json.has_key?(method.to_s) ? json[method.to_s] : super
end
|
Instance Attribute Details
#basic_auth ⇒ Object
Returns the value of attribute basic_auth.
9
10
11
|
# File 'lib/mavenlink/base.rb', line 9
def basic_auth
@basic_auth
end
|
#errors ⇒ Object
Returns the value of attribute errors.
9
10
11
|
# File 'lib/mavenlink/base.rb', line 9
def errors
@errors
end
|
#json ⇒ Object
Returns the value of attribute json.
9
10
11
|
# File 'lib/mavenlink/base.rb', line 9
def json
@json
end
|
#path_params ⇒ Object
Returns the value of attribute path_params.
9
10
11
|
# File 'lib/mavenlink/base.rb', line 9
def path_params
@path_params
end
|
Instance Method Details
#as_json(*args) ⇒ Object
95
96
97
|
# File 'lib/mavenlink/base.rb', line 95
def as_json(*args)
@json.as_json(*args)
end
|
#delete_request(path, options = {}) ⇒ Object
54
55
56
57
|
# File 'lib/mavenlink/base.rb', line 54
def delete_request(path, options = {})
puts "DELETE #{path}" if self.class.debug
self.class.delete path, :query => options, :basic_auth => basic_auth
end
|
#destroy ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/mavenlink/base.rb', line 72
def destroy
response = delete_request(request_path)
if response.code == 200
true
elsif response.code == 422
self.errors = response.parsed_response['errors']
false
else
raise "Server error code #{response.code}"
end
end
|
#get_request(path, options = {}) ⇒ Object
39
40
41
42
|
# File 'lib/mavenlink/base.rb', line 39
def get_request(path, options = {})
puts "GET #{path}" if self.class.debug
self.class.get path, :query => options, :basic_auth => basic_auth
end
|
#id ⇒ Object
17
18
19
|
# File 'lib/mavenlink/base.rb', line 17
def id
json['id']
end
|
#join_paths(original, append) ⇒ Object
31
32
33
34
35
36
37
|
# File 'lib/mavenlink/base.rb', line 31
def join_paths(original, append)
original = original + "/" unless original =~ /\/$/
original = "/" + original unless original =~ /^\//
append = append.gsub(/^\//, '')
uri = URI.parse("http://www.example.com#{original}")
(uri + append).path.gsub(/\/$/, '')
end
|
#post_request(path, options = {}) ⇒ Object
49
50
51
52
|
# File 'lib/mavenlink/base.rb', line 49
def post_request(path, options = {})
puts "POST #{path}" if self.class.debug
self.class.post path, :body => options, :basic_auth => basic_auth
end
|
#put_request(path, options = {}) ⇒ Object
44
45
46
47
|
# File 'lib/mavenlink/base.rb', line 44
def put_request(path, options = {})
puts "PUT #{path}" if self.class.debug
self.class.put path, :body => options, :basic_auth => basic_auth
end
|
#reload(options = {}) ⇒ Object
84
85
86
87
88
89
90
91
92
|
# File 'lib/mavenlink/base.rb', line 84
def reload(options = {})
result = get_request(request_path, options)
if result.code == 200
set_json result.parsed_response
else
raise "Server error code #{result.code}"
end
self
end
|
#request_path ⇒ Object
21
22
23
24
25
26
27
28
29
|
# File 'lib/mavenlink/base.rb', line 21
def request_path
p = self.class.request_path.to_s
params = path_params.dup
params[:id] = id if id
params.each do |key, value|
p = p.gsub(key.inspect, value.to_s)
end
p
end
|
#update(changes) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/mavenlink/base.rb', line 59
def update(changes)
response = put_request(request_path, self.class.class_name => changes)
if response.code == 200
set_json response.parsed_response
true
elsif response.code == 422
self.errors = response.parsed_response['errors']
false
else
raise "Server error code #{response.code}"
end
end
|