Module: AppReport::Client

Defined in:
lib/app_report/client.rb

Constant Summary collapse

@@connection =
nil
@@endpoint =
'http://reports.simpleservic.es'

Class Method Summary collapse

Class Method Details

.connectionObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/app_report/client.rb', line 38

def self.connection
  if @@connection.nil?
    @@connection = Faraday.new(:url => @@endpoint) { |faraday|
      faraday.request :url_encoded
      faraday.adapter Faraday.default_adapter # same as :net_http
    }
  end

  @@connection
end

.post(path, params = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/app_report/client.rb', line 25

def self.post path, params = {}
  response = connection.post do |request|
    request.url path

    request.headers['Content-Type'] = 'application/json'
    request.body                    = MultiJson.dump params
  end

  raise_error_messages! response.body

  response
end

.raise_error_messages!(response_body) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/app_report/client.rb', line 49

def self.raise_error_messages! response_body
  response_body = MultiJson.load response_body

  if response_body.try(:[], 'messages').try(:[], 'has_any_error')
    error_messages = response_body['messages']['messages'].map { |m|
      m['message']
    }

    raise AppReport::Errors::APIError, \
      "AppReport API error: #{error_messages}"
  end
end