Class: Kongkit::Client::Resource

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/kongkit/client/resource.rb

Instance Method Summary collapse

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

Parameters:

  • key (Symbol)

    A symbol key

Returns:

  • (Object)

    from data if exists


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

Returns:

  • (Enumerator)

    Enumerator


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

Returns:

  • (Boolean)

    `true` if the response was an error


47
48
49
# File 'lib/kongkit/client/resource.rb', line 47

def error?
  status_code >= 400
end

#inspectString

Returns a string containing a human-readable representation of this object

Returns:

  • (String)

    human-readable representation of this object


40
41
42
# File 'lib/kongkit/client/resource.rb', line 40

def inspect
  data.inspect
end

#nextKongkit::Client::Resource

Retrieves the next resource

Returns:


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_jsonHash

Return the JSON representation of the resource

Returns:

  • (Hash)

    JSON representation


54
55
56
# File 'lib/kongkit/client/resource.rb', line 54

def to_json
  data.merge({status_code: status_code}).to_json
end