Class: Resto::Response::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/resto/response/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



61
62
63
# File 'lib/resto/response/base.rb', line 61

def response
  @response
end

Instance Method Details

#bodyObject



49
50
51
# File 'lib/resto/response/base.rb', line 49

def body
  @response ? @response.body : nil
end

#codeObject



53
54
55
# File 'lib/resto/response/base.rb', line 53

def code
  @response ? @response.code : nil
end

#current_formatterObject



37
38
39
# File 'lib/resto/response/base.rb', line 37

def current_formatter
  @formatter ||= Resto::Format.get(@symbol || :default)
end

#format(symbol, options = {}) ⇒ Object



19
20
21
22
23
# File 'lib/resto/response/base.rb', line 19

def format(symbol, options = {})
  xpath = options[:xpath]
  xpath(xpath) if xpath
  formatter(Resto::Format.get(@symbol = symbol))
end

#formatter(formatter) ⇒ Object



33
34
35
# File 'lib/resto/response/base.rb', line 33

def formatter(formatter)
  tap { @formatter = formatter }
end

#http_response(response) ⇒ Object



41
42
43
# File 'lib/resto/response/base.rb', line 41

def http_response(response)
  tap { @response = response }
end

#klass(klass) ⇒ Object



10
11
12
# File 'lib/resto/response/base.rb', line 10

def klass(klass)
  tap { @klass = klass }
end

#read_bodyObject



45
46
47
# File 'lib/resto/response/base.rb', line 45

def read_body
  body ? current_formatter.decode(body, :xpath => read_xpath) : nil
end

#read_xpathObject



29
30
31
# File 'lib/resto/response/base.rb', line 29

def read_xpath
  @xpath || "//#{@klass.to_s.downcase}"
end

#to_collectionObject



73
74
75
76
77
78
79
80
81
# File 'lib/resto/response/base.rb', line 73

def to_collection
  return self unless @translator

  (read_body || []).map do |hash|
    @translator.call(@klass, hash).tap do |instance|
      instance.response = self
    end
  end
end

#to_objectObject



63
64
65
66
67
68
69
70
71
# File 'lib/resto/response/base.rb', line 63

def to_object
  return self unless @translator

  body = read_body ? read_body.first : nil

  @translator.call(@klass, body).tap do |instance|
    instance.response = self
  end
end

#translator(translator) ⇒ Object



14
15
16
17
# File 'lib/resto/response/base.rb', line 14

def translator(translator)
  @translator = Resto::Translator::ResponseFactory.create(translator)
  self
end

#valid?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/resto/response/base.rb', line 57

def valid?
  (/\A20\d{1}\z/ =~ code.to_s) == 0
end

#xpath(xpath) ⇒ Object



25
26
27
# File 'lib/resto/response/base.rb', line 25

def xpath(xpath)
  tap { @xpath = xpath }
end