Module: MxHero::API::Communication
- Included in:
- Client, Directories, EmailSync, Groups, PSTConverter
- Defined in:
- lib/communication.rb
Instance Method Summary collapse
-
#call(method, url, body = nil, more_options = {}) ⇒ HTTP::Message
Make a HTTP call.
-
#headers(options = {}) ⇒ Object
Default headers.
- #json_parse(json) ⇒ Hash
Instance Method Details
#call(method, url, body = nil, more_options = {}) ⇒ HTTP::Message
Make a HTTP call
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/communication.rb', line 17 def call(method, url, body = nil, = {}) unless @client @client ||= HTTPClient.new(:force_basic_auth => true) end @client.set_auth(url, @username, @password) response = @client.request(method, url, nil, body, headers()) raise "Unauthorized" if response.status == 401 unless [:throw_exception] == false raise "An error ocurred when try to communicate with the API\nError: #{response.inspect}" if (500..600).include?(response.status) end response end |
#headers(options = {}) ⇒ Object
Default headers
31 32 33 34 35 36 37 38 39 |
# File 'lib/communication.rb', line 31 def headers( = {}) accept = .fetch(:accept, 'application/json') content_type = .fetch(:content_type, 'application/json') return { 'Accept' => accept, 'Content-Type' => content_type, "Authorization" => "Basic " + ::Base64.encode64(@username + ':' + @password).gsub("\n",''), }.merge(@as_user ? { "X-MxHero-As-User" => @as_user } : {}) end |
#json_parse(json) ⇒ Hash
42 43 44 |
# File 'lib/communication.rb', line 42 def json_parse(json) JSON.parse(json, symbolize_names: true) end |