Class: Crowdkit::API::ResponseWrapper

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
RequestMethods
Defined in:
lib/crowdkit/api/response_wrapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RequestMethods

#agent, #method

Constructor Details

#initialize(sawyer, client) ⇒ ResponseWrapper

Returns a new instance of ResponseWrapper.



12
13
14
15
# File 'lib/crowdkit/api/response_wrapper.rb', line 12

def initialize(sawyer, client)
  @sawyer = sawyer
  @client = client
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Coerce any method calls for body attributes



97
98
99
100
101
102
103
# File 'lib/crowdkit/api/response_wrapper.rb', line 97

def method_missing(method_name, *args, &block)
  if self.has_key?(method_name)
    self.[](method_name, &block)
  else
    super
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



4
5
6
# File 'lib/crowdkit/api/response_wrapper.rb', line 4

def client
  @client
end

#sawyerObject (readonly)

Returns the value of attribute sawyer.



4
5
6
# File 'lib/crowdkit/api/response_wrapper.rb', line 4

def sawyer
  @sawyer
end

Instance Method Details

#[](key) ⇒ Object

Lookup an attribute from the body if hash, otherwise behave like array index. Convert any key to string before calling.



28
29
30
31
32
33
34
# File 'lib/crowdkit/api/response_wrapper.rb', line 28

def [](key)
  if self.data.is_a?(Array)
    self.data[key]
  else
    self.data.send(:"#{key}")
  end
end

#current_statusObject



36
37
38
39
40
41
42
# File 'lib/crowdkit/api/response_wrapper.rb', line 36

def current_status
  if self.data["current_status"]
    do_get(self.data["current_status"].rels["self"].href)
  else
    nil
  end
end

#dataObject



17
18
19
# File 'lib/crowdkit/api/response_wrapper.rb', line 17

def data
  @sawyer.data
end

#each(to_enum = true, &block) ⇒ Object

Iterate over each resource inside the body



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/crowdkit/api/response_wrapper.rb', line 46

def each(to_enum = true, &block)
  body_parts = self.data.respond_to?(:each) ? self.data : [self.data]
  if block_given?
    body_parts.each &block
    if self.auto_pagination && self.has_next_page?
      next_page.each &block
    end
  else
    res = self
    if self.auto_pagination && res.has_next_page?
      res = next_page
      body_parts.concat(res.each(false))
    end
    result = to_enum ? body_parts.to_enum : body_parts
  end
  #Reset auto pagination option
  client.auto_pagination = nil
  result
end

#has_key?(key) ⇒ Boolean

Check if body has an attribute

Returns:

  • (Boolean)


90
91
92
93
# File 'lib/crowdkit/api/response_wrapper.rb', line 90

def has_key?(key)
  res = self.data.is_a?(Array) ? self.data.first : self.data
  res.key?(key.to_sym)
end

#has_next_page?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/crowdkit/api/response_wrapper.rb', line 72

def has_next_page?
  @sawyer.rels[:next]
end

#headersObject



21
22
23
# File 'lib/crowdkit/api/response_wrapper.rb', line 21

def headers
  @sawyer.headers
end

#inspectObject

Print only response body



117
118
119
# File 'lib/crowdkit/api/response_wrapper.rb', line 117

def inspect
  self.data.inspect
end

#rate_limit_remainingObject



84
85
86
# File 'lib/crowdkit/api/response_wrapper.rb', line 84

def rate_limit_remaining
  self.headers["X-RateLimit-Remaining"]
end

#respond_to?(method_name) ⇒ Boolean

Check if method is defined on the body

Returns:

  • (Boolean)


107
108
109
110
111
112
113
# File 'lib/crowdkit/api/response_wrapper.rb', line 107

def respond_to?(method_name)
  if self.has_key?(method_name)
    true
  else
    super
  end
end