Class: TeamSnap::Response

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#argsObject

Returns the value of attribute args.



50
51
52
# File 'lib/teamsnap/response.rb', line 50

def args
  @args
end

#clientObject

Returns the value of attribute client.



50
51
52
# File 'lib/teamsnap/response.rb', line 50

def client
  @client
end

#collectionObject

Returns the value of attribute collection.



50
51
52
# File 'lib/teamsnap/response.rb', line 50

def collection
  @collection
end

#hrefObject

Returns the value of attribute href.



50
51
52
# File 'lib/teamsnap/response.rb', line 50

def href
  @href
end

#messageObject

Returns the value of attribute message.



50
51
52
# File 'lib/teamsnap/response.rb', line 50

def message
  @message
end

#objectsObject

Returns the value of attribute objects.



50
51
52
# File 'lib/teamsnap/response.rb', line 50

def objects
  @objects
end

#respObject

Returns the value of attribute resp.



50
51
52
# File 'lib/teamsnap/response.rb', line 50

def resp
  @resp
end

#statusObject

Returns the value of attribute status.



50
51
52
# File 'lib/teamsnap/response.rb', line 50

def status
  @status
end

#viaObject

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

Returns:

  • (Boolean)


109
110
111
# File 'lib/teamsnap/response.rb', line 109

def errors?
  @status / 100 != 2
end

#process_actionObject



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_errorObject



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_infoObject



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

Returns:

  • (Boolean)


105
106
107
# File 'lib/teamsnap/response.rb', line 105

def success?
  @status / 100 == 2
end