Class: Fathom::Resource
- Inherits:
-
Object
show all
- Defined in:
- lib/fathom/resource.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = {}, rate_limit_info: nil) ⇒ Resource
7
8
9
10
|
# File 'lib/fathom/resource.rb', line 7
def initialize(attributes = {}, rate_limit_info: nil)
@attributes = attributes
@rate_limit_info = rate_limit_info
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/fathom/resource.rb', line 29
def method_missing(method_name, *args, &)
method_str = method_name.to_s
if method_str.end_with?("=")
@attributes[method_str.chop] = args.first
elsif @attributes.key?(method_str)
@attributes[method_str]
else
super
end
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
5
6
7
|
# File 'lib/fathom/resource.rb', line 5
def attributes
@attributes
end
|
#rate_limit_info ⇒ Object
Returns the value of attribute rate_limit_info.
5
6
7
|
# File 'lib/fathom/resource.rb', line 5
def rate_limit_info
@rate_limit_info
end
|
Class Method Details
.all(params = {}) ⇒ Object
52
53
54
55
56
57
58
|
# File 'lib/fathom/resource.rb', line 52
def all(params = {})
response = client.get(resource_path, params)
data = response["items"] || response["data"] || []
data.map { |attrs| new(attrs, rate_limit_info: client.rate_limiter.to_h) }
end
|
.client ⇒ Object
46
|
# File 'lib/fathom/resource.rb', line 46
def client = @client ||= Client.new
|
.create(attributes = {}) ⇒ Object
67
68
69
70
71
72
|
# File 'lib/fathom/resource.rb', line 67
def create(attributes = {})
response = client.post(resource_path, attributes)
data = response["data"] || response[resource_name] || response
new(data, rate_limit_info: client.rate_limiter.to_h)
end
|
.resource_name ⇒ Object
48
|
# File 'lib/fathom/resource.rb', line 48
def resource_name = name.split("::").last.downcase
|
.resource_path ⇒ Object
50
|
# File 'lib/fathom/resource.rb', line 50
def resource_path = "#{resource_name}s"
|
.retrieve(id) ⇒ Object
60
61
62
63
64
65
|
# File 'lib/fathom/resource.rb', line 60
def retrieve(id)
response = client.get("#{resource_path}/#{id}")
data = response["data"] || response[resource_name] || response
new(data, rate_limit_info: client.rate_limiter.to_h)
end
|
.search(query, params = {}) ⇒ Object
74
75
76
77
78
79
80
|
# File 'lib/fathom/resource.rb', line 74
def search(query, params = {})
search_params = params.merge(q: query)
response = client.get("#{resource_path}/search", search_params)
data = response["data"] || response[resource_path] || []
data.map { |attrs| new(attrs, rate_limit_info: client.rate_limiter.to_h) }
end
|
Instance Method Details
#[](key) ⇒ Object
14
|
# File 'lib/fathom/resource.rb', line 14
def [](key) = @attributes[key.to_s]
|
#[]=(key, value) ⇒ Object
16
17
18
|
# File 'lib/fathom/resource.rb', line 16
def []=(key, value)
@attributes[key.to_s] = value
end
|
#delete ⇒ Object
93
94
95
96
97
98
|
# File 'lib/fathom/resource.rb', line 93
def delete
self.class.client.delete("#{self.class.resource_path}/#{id}")
@rate_limit_info = self.class.client.rate_limiter.to_h
true
end
|
#id ⇒ Object
12
|
# File 'lib/fathom/resource.rb', line 12
def id = @attributes["id"]
|
#inspect ⇒ Object
24
25
26
|
# File 'lib/fathom/resource.rb', line 24
def inspect
"#<#{self.class.name}:0x#{object_id.to_s(16)} id=#{id.inspect} #{@attributes.keys.join(", ")}>"
end
|
#reload ⇒ Object
100
101
102
103
104
105
106
107
108
|
# File 'lib/fathom/resource.rb', line 100
def reload
response = self.class.client.get("#{self.class.resource_path}/#{id}")
data = response["data"] || response[self.class.resource_name] || response
@attributes = data
@rate_limit_info = self.class.client.rate_limiter.to_h
self
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
40
41
42
43
|
# File 'lib/fathom/resource.rb', line 40
def respond_to_missing?(method_name, include_private = false)
method_str = method_name.to_s
method_str.end_with?("=") || @attributes.key?(method_str) || super
end
|
#to_h ⇒ Object
20
|
# File 'lib/fathom/resource.rb', line 20
def to_h = @attributes
|
#to_json ⇒ Object
22
|
# File 'lib/fathom/resource.rb', line 22
def to_json(...) = @attributes.to_json(...)
|
#update(attributes = {}) ⇒ Object
83
84
85
86
87
88
89
90
91
|
# File 'lib/fathom/resource.rb', line 83
def update(attributes = {})
response = self.class.client.patch("#{self.class.resource_path}/#{id}", attributes)
data = response["data"] || response[self.class.resource_name] || response
@attributes.merge!(data)
@rate_limit_info = self.class.client.rate_limiter.to_h
self
end
|