Module: Stocktwits::Dispatcher::Shared

Included in:
Basic, Oauth, Plain
Defined in:
lib/stocktwits/dispatcher/shared.rb

Instance Method Summary collapse

Instance Method Details

#append_extension_to(path) ⇒ Object

def post!(status)

self.post('/statuses/update.json', :status => status)

end



9
10
11
12
13
# File 'lib/stocktwits/dispatcher/shared.rb', line 9

def append_extension_to(path)
  path, query_string = *(path.split("?"))
  path << '.json' unless path.match(/\.(:?xml|json)\z/i)
  "#{path}#{"?#{query_string}" if query_string}"
end

#handle_response(response) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/stocktwits/dispatcher/shared.rb', line 15

def handle_response(response)
  case response
  when Net::HTTPOK 
    begin
      JSON.parse(response.body)
    rescue JSON::ParserError
      response.body
    end
  when Net::HTTPUnauthorized
    raise Stocktwits::Dispatcher::Unauthorized, 'The credentials provided did not authorize the user.'
  else
    message = begin
      JSON.parse(response.body)['error']
    rescue JSON::ParserError
      if match = response.body.match(/<error>(.*)<\/error>/)
        match[1]
      else
        raise response.body
        'An error occurred processing your Stocktwits request. '
      end
    end

    raise Stocktwits::Dispatcher::Error, message
  end
end