Sha256: 7f4e4a7781ed374595281d42c72a74708e9fb9c6600e23eb204f1edc7cdca77f

Contents?: true

Size: 1.79 KB

Versions: 16

Compression:

Stored size: 1.79 KB

Contents

# encoding: utf-8
require 'base64'
module PassaporteWeb

  class Http # :nodoc:

    def self.get(path='/', params={}, type='application')
      get_or_delete(:get, path, params, type)
    end

    def self.put(path='/', body={}, params={}, type='application')
      put_or_post(:put, path, body, params, type)
    end

    def self.post(path='/', body={}, params={}, type='application')
      put_or_post(:post, path, body, params, type)
    end

    def self.delete(path='/', params={}, type='application')
      get_or_delete(:delete, path, params, type)
    end

    def self.custom_auth_get(user, password, path='/', params={})
      credentials = "Basic #{::Base64.strict_encode64("#{user}:#{password}")}"
      custom_params = common_params('application').merge({authorization: credentials})
      RestClient.get(
        pw_url(path),
        {params: params}.merge(custom_params)
      )
    end

    private

    def self.put_or_post(method, path, body, params, type)
      RestClient.send(
        method,
        pw_url(path),
        encoded_body(body),
        {params: params}.merge(common_params(type))
      )
    end

    def self.get_or_delete(method, path, params, type)
      RestClient.send(
        method,
        pw_url(path),
        {params: params}.merge(common_params(type))
      )
    end

    def self.pw_url(path)
      "#{PassaporteWeb.configuration.url}#{path}"
    end

    def self.common_params(type)
      {
        authorization: (type == 'application' ? PassaporteWeb.configuration.application_credentials : PassaporteWeb.configuration.user_credentials),
        content_type: :json,
        accept: :json,
        user_agent: PassaporteWeb.configuration.user_agent
      }
    end

    def self.encoded_body(body)
      body.is_a?(Hash) ? MultiJson.encode(body) : body
    end

  end

end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
passaporteweb-client-0.5.0 lib/passaporte_web/http.rb
passaporteweb-client-0.4.0 lib/passaporte_web/http.rb
passaporteweb-client-0.0.20 lib/passaporte_web/http.rb
passaporteweb-client-0.3.0 lib/passaporte_web/http.rb
passaporteweb-client-0.2.0 lib/passaporte_web/http.rb
passaporteweb-client-0.1.0 lib/passaporte_web/http.rb
passaporteweb-client-0.0.19 lib/passaporte_web/http.rb
passaporteweb-client-0.0.18 lib/passaporte_web/http.rb
passaporteweb-client-0.0.17 lib/passaporte_web/http.rb
passaporteweb-client-0.0.16 lib/passaporte_web/http.rb
passaporteweb-client-0.0.15 lib/passaporte_web/http.rb
passaporteweb-client-0.0.14 lib/passaporte_web/http.rb
passaporteweb-client-0.0.13 lib/passaporte_web/http.rb
passaporteweb-client-0.0.12 lib/passaporte_web/http.rb
passaporteweb-client-0.0.11 lib/passaporte_web/http.rb
passaporteweb-client-0.0.10 lib/passaporte_web/http.rb