Module: Pigeon::Utils

Included in:
Nuntium, Verboice
Defined in:
lib/pigeon/utils.rb

Instance Method Summary collapse

Instance Method Details

#delete(path) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/pigeon/utils.rb', line 61

def delete(path)
  resource = RestClient::Resource.new @url, @options
  resource = resource[path].delete
  yield resource, nil
rescue => ex
  yield nil, ex
end

#get(path) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/pigeon/utils.rb', line 27

def get(path)
  resource = RestClient::Resource.new @url, @options
  resource = resource[path].get
  yield resource, nil
rescue => ex
  yield nil, ex
end

#get_json(path) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/pigeon/utils.rb', line 35

def get_json(path)
  get(path) do |response, error|
    raise self.class.error_class.new error.message if error

    elem = JSON.parse response.body
    elem.map! { |x| with_indifferent_access x } if elem.is_a? Array
    elem
  end
end

#handle_channel_error(error) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/pigeon/utils.rb', line 10

def handle_channel_error(error)
  if error.is_a? RestClient::BadRequest
    response = JSON.parse error.response.body
    raise self.class.error_class.new response['summary'], Hash[response['properties'].map {|e| [e.keys[0], e.values[0]]}]
  else
    raise self.class.error_class.new error.message
  end
end

#post(path, data) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/pigeon/utils.rb', line 45

def post(path, data)
  resource = RestClient::Resource.new @url, @options
  resource = resource[path].post(data)
  yield resource, nil
rescue => ex
  yield nil, ex
end

#put(path, data) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/pigeon/utils.rb', line 53

def put(path, data)
  resource = RestClient::Resource.new @url, @options
  resource = resource[path].put(data)
  yield resource, nil
rescue => ex
  yield nil, ex
end

#to_query(hash) ⇒ Object



4
5
6
7
8
# File 'lib/pigeon/utils.rb', line 4

def to_query(hash)
  hash.map do |key, value|
    "#{key.to_s}=#{CGI.escape(value.to_s)}"
  end.join('&')
end

#with_indifferent_access(hash) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/pigeon/utils.rb', line 19

def with_indifferent_access(hash)
  if hash.respond_to? :with_indifferent_access
    hash.with_indifferent_access
  else
    hash
  end
end