Class: Whatser::Response

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/whatser/api/response.rb

Constant Summary collapse

API_RESPONSE_KEYS =
['data','http_status','version','scope','page','per_page','more','error','error_description','error_uri']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_or_hash, opts = {}) ⇒ Response

Returns a new instance of Response.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/whatser/api/response.rb', line 15

def initialize(json_or_hash, opts={})
  return if json_or_hash.blank?
  
  begin
    hash = json_or_hash.is_a?(String) ? JSON.parse(json_or_hash) : json_or_hash
  rescue JSON::ParserError
    hash = {}
  end
  
  load_from_hash(hash, opts[:keys])
  ensure_status_code( opts[:code] )
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



11
12
13
# File 'lib/whatser/api/response.rb', line 11

def access_token
  @access_token
end

#dataObject

Returns the value of attribute data.



7
8
9
# File 'lib/whatser/api/response.rb', line 7

def data
  @data
end

#errorObject

Returns the value of attribute error.



9
10
11
# File 'lib/whatser/api/response.rb', line 9

def error
  @error
end

#error_descriptionObject

Returns the value of attribute error_description.



9
10
11
# File 'lib/whatser/api/response.rb', line 9

def error_description
  @error_description
end

#error_uriObject

Returns the value of attribute error_uri.



9
10
11
# File 'lib/whatser/api/response.rb', line 9

def error_uri
  @error_uri
end

#expires_inObject

Returns the value of attribute expires_in.



11
12
13
# File 'lib/whatser/api/response.rb', line 11

def expires_in
  @expires_in
end

#http_statusObject

Returns the value of attribute http_status.



7
8
9
# File 'lib/whatser/api/response.rb', line 7

def http_status
  @http_status
end

#moreObject

Returns the value of attribute more.



8
9
10
# File 'lib/whatser/api/response.rb', line 8

def more
  @more
end

#pageObject

Returns the value of attribute page.



8
9
10
# File 'lib/whatser/api/response.rb', line 8

def page
  @page
end

#per_pageObject

Returns the value of attribute per_page.



8
9
10
# File 'lib/whatser/api/response.rb', line 8

def per_page
  @per_page
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



11
12
13
# File 'lib/whatser/api/response.rb', line 11

def refresh_token
  @refresh_token
end

#scopeObject

Returns the value of attribute scope.



7
8
9
# File 'lib/whatser/api/response.rb', line 7

def scope
  @scope
end

#versionObject

Returns the value of attribute version.



7
8
9
# File 'lib/whatser/api/response.rb', line 7

def version
  @version
end

Instance Method Details

#confict?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/whatser/api/response.rb', line 72

def confict?
  http_status == 409
end

#data_enumObject



40
41
42
# File 'lib/whatser/api/response.rb', line 40

def data_enum
  data.blank? ? [] : (data.is_a?(Array) ? data : [data])
end

#each(&block) ⇒ Object



44
45
46
# File 'lib/whatser/api/response.rb', line 44

def each(&block)
  data_enum.each(&block)
end

#ensure_status_code(code) ⇒ Object



28
29
30
# File 'lib/whatser/api/response.rb', line 28

def ensure_status_code(code)
  self.http_status ||= code
end

#forbidden?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/whatser/api/response.rb', line 56

def forbidden?
  http_status == 403
end

#load_from_hash(hash, keys = nil) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/whatser/api/response.rb', line 32

def load_from_hash(hash,keys=nil)
  return unless hash.is_a?(Hash)
  keys ||= API_RESPONSE_KEYS
  keys.each do |a|
    send("#{a}=", hash[a]) if hash.has_key?(a)
  end
end

#not_acceptable?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/whatser/api/response.rb', line 68

def not_acceptable?
  http_status == 406
end

#not_allowed?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/whatser/api/response.rb', line 64

def not_allowed?
  http_status == 405
end

#not_found?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/whatser/api/response.rb', line 60

def not_found?
  http_status == 404
end

#server_error?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/whatser/api/response.rb', line 80

def server_error?
  http_status == 500
end

#succeeded?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/whatser/api/response.rb', line 48

def succeeded?
  http_status == 200
end

#unauthorized?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/whatser/api/response.rb', line 52

def unauthorized?
  http_status == 401
end

#unprocessable_entity?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/whatser/api/response.rb', line 76

def unprocessable_entity?
  http_status == 422
end