Class: Kongkit::Client::Resource
- Inherits:
-
Object
- Object
- Kongkit::Client::Resource
- Includes:
- Enumerable
- Defined in:
- lib/kongkit/client/resource.rb
Instance Method Summary collapse
-
#[](key) ⇒ Object
Allow fields to be retrieved via Hash notation.
-
#each(&block) ⇒ Enumerator
Calls block once for each key in the data hash, passing the key-value pair as parameters.
-
#error? ⇒ Boolean
Returns true if there is any error.
-
#initialize(client, data, status_code) ⇒ Resource
constructor
A new instance of Resource.
-
#inspect ⇒ String
Returns a string containing a human-readable representation of this object.
-
#next ⇒ Kongkit::Client::Resource
Retrieves the next resource.
-
#to_json ⇒ Hash
Return the JSON representation of the resource.
Constructor Details
#initialize(client, data, status_code) ⇒ Resource
Returns a new instance of Resource.
6 7 8 9 10 |
# File 'lib/kongkit/client/resource.rb', line 6 def initialize(client, data, status_code) @client = client @data = data @status_code = status_code end |
Instance Method Details
#[](key) ⇒ Object
Allow fields to be retrieved via Hash notation
16 17 18 19 20 |
# File 'lib/kongkit/client/resource.rb', line 16 def [](key) data[key] rescue NoMethodError nil end |
#each(&block) ⇒ Enumerator
Calls block once for each key in the data hash, passing the key-value pair as parameters
25 26 27 |
# File 'lib/kongkit/client/resource.rb', line 25 def each(&block) data.each(&block) end |
#error? ⇒ Boolean
Returns true if there is any error
47 48 49 |
# File 'lib/kongkit/client/resource.rb', line 47 def error? status_code >= 400 end |
#inspect ⇒ String
Returns a string containing a human-readable representation of this object
40 41 42 |
# File 'lib/kongkit/client/resource.rb', line 40 def inspect data.inspect end |
#next ⇒ Kongkit::Client::Resource
Retrieves the next resource
32 33 34 35 |
# File 'lib/kongkit/client/resource.rb', line 32 def next return nil if data[:next].nil? client.get(data[:next]) end |
#to_json ⇒ Hash
Return the JSON representation of the resource
54 55 56 |
# File 'lib/kongkit/client/resource.rb', line 54 def to_json data.merge({status_code: status_code}) end |