Class: TeamSnap::Response
- Inherits:
-
Object
- Object
- TeamSnap::Response
- Defined in:
- lib/teamsnap/response.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#client ⇒ Object
Returns the value of attribute client.
-
#collection ⇒ Object
Returns the value of attribute collection.
-
#href ⇒ Object
Returns the value of attribute href.
-
#message ⇒ Object
Returns the value of attribute message.
-
#objects ⇒ Object
Returns the value of attribute objects.
-
#resp ⇒ Object
Returns the value of attribute resp.
-
#status ⇒ Object
Returns the value of attribute status.
-
#via ⇒ Object
Returns the value of attribute via.
Class Method Summary collapse
Instance Method Summary collapse
- #errors? ⇒ Boolean
-
#initialize(opts = {}) ⇒ Response
constructor
A new instance of Response.
- #process_action ⇒ Object
- #process_error ⇒ Object
- #process_info ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(opts = {}) ⇒ Response
Returns a new instance of Response.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/teamsnap/response.rb', line 53 def initialize(opts = {}) [ :args, :client, :collection, :href, :message, :objects, :resp, :status, :via ].each do |attribute_name| instance_variable_set("@#{attribute_name}", opts.fetch(attribute_name, nil)) end if resp if resp.success? if via == :get process_info else process_action end else process_error end end end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
50 51 52 |
# File 'lib/teamsnap/response.rb', line 50 def args @args end |
#client ⇒ Object
Returns the value of attribute client.
50 51 52 |
# File 'lib/teamsnap/response.rb', line 50 def client @client end |
#collection ⇒ Object
Returns the value of attribute collection.
50 51 52 |
# File 'lib/teamsnap/response.rb', line 50 def collection @collection end |
#href ⇒ Object
Returns the value of attribute href.
50 51 52 |
# File 'lib/teamsnap/response.rb', line 50 def href @href end |
#message ⇒ Object
Returns the value of attribute message.
50 51 52 |
# File 'lib/teamsnap/response.rb', line 50 def @message end |
#objects ⇒ Object
Returns the value of attribute objects.
50 51 52 |
# File 'lib/teamsnap/response.rb', line 50 def objects @objects end |
#resp ⇒ Object
Returns the value of attribute resp.
50 51 52 |
# File 'lib/teamsnap/response.rb', line 50 def resp @resp end |
#status ⇒ Object
Returns the value of attribute status.
50 51 52 |
# File 'lib/teamsnap/response.rb', line 50 def status @status end |
#via ⇒ Object
Returns the value of attribute via.
50 51 52 |
# File 'lib/teamsnap/response.rb', line 50 def via @via end |
Class Method Details
.load_collection(resp) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/teamsnap/response.rb', line 7 def load_collection(resp) if resp.success? return JSON.parse(resp.body, :symbolize_names => true).fetch(:collection) else content_type = resp.headers["content-type"] if content_type && content_type.match("json") if resp.status == 404 raise TeamSnap::NotFound.new("Object not found.") else raise TeamSnap::Error.new( TeamSnap::Api.parse_error(resp) ) end else raise TeamSnap::Error.new( "`#{resp.env.method}` call was unsuccessful. " + "Unexpected response content-type. " + "Check TeamSnap APIv3 connection") end end end |
.process(client, resp, via, href, args) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/teamsnap/response.rb', line 29 def process(client, resp, via, href, args) response_object = self.new( :args => args, :client => client, :href => href, :resp => resp, :status => resp.status, :via => via ) if resp.success? if via == :get response_object.process_info else response_object.process_action end else response_object.process_error end end |
Instance Method Details
#errors? ⇒ Boolean
109 110 111 |
# File 'lib/teamsnap/response.rb', line 109 def errors? @status / 100 != 2 end |
#process_action ⇒ Object
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/teamsnap/response.rb', line 80 def process_action body = if @resp.body.nil? || @resp.body.empty? {} else JSON.parse(@resp.body, :symbolize_names => true) || {} end @collection = body.fetch(:collection) { {} } @message = "`#{@via}` call was successful" @objects = TeamSnap::Item.load_items(@client, @collection) end |
#process_error ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/teamsnap/response.rb', line 91 def process_error if @resp.headers["content-type"].match("json") body = JSON.parse(@resp.body, :symbolize_names => true) || {} @collection = body.fetch(:collection) { {} } @message = TeamSnap::Api.parse_error(@resp) @objects = TeamSnap::Item.load_items(@client, @collection) else raise TeamSnap::Error.new( "`#{@via}` call with arguments #{@args} was unsuccessful. " + "The server returned a status of #{@status}." ) end end |
#process_info ⇒ Object
73 74 75 76 77 78 |
# File 'lib/teamsnap/response.rb', line 73 def process_info body = JSON.parse(@resp.body, :symbolize_names => true) @collection = body.fetch(:collection) { {} } @message = "Data retrieved successfully" @objects = TeamSnap::Item.load_items(@client, @collection) end |
#success? ⇒ Boolean
105 106 107 |
# File 'lib/teamsnap/response.rb', line 105 def success? @status / 100 == 2 end |