Class: ActivityMapper::Connector

Inherits:
Object
  • Object
show all
Defined in:
lib/activity_mapper/connector.rb

Defined Under Namespace

Classes: ConnectorLogger, Error

Class Method Summary collapse

Class Method Details

.deserialize(response_body, format) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/activity_mapper/connector.rb', line 27

def self.deserialize(response_body, format)
  # Convert response data to Hash
  begin
    case format
      when :xml
        XmlSimple.xml_in(response_body)
      else
        JSON.parse(response_body)
    end 
  rescue JSON::ParserError => e
    raise Connector.error(e, "Unable to decode JSON response, got: '#{response_body.to_s.slice(0, 400)}'")
  end
end

.fetch(url, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/activity_mapper/connector.rb', line 6

def self.fetch(url, options = {})
  options[:method] ||= :get

  warning_in_test_env(url)

  agent = WWW::Mechanize.new
  agent.user_agent_alias = 'Mac FireFox'
  begin
    logger.info("Fetching #{url} (:method=#{options[:method]})")
    response = agent.send(options[:method], url)
  rescue => e
    raise error(e, "Unable to fetch #{url}")
  end
  # TODO deal with redirects n stuff
  if response.code.to_i != 200
    raise error(e, "Got invalid HTTP response (#{response.code}) for #{url}")
  end

  response.body
end