Module: Credly::Connection

Included in:
Client
Defined in:
lib/credly/connection.rb

Instance Method Summary collapse

Instance Method Details

#connectionObject



27
28
29
# File 'lib/credly/connection.rb', line 27

def connection
  @connection ||= new_connection
end

#delete(path, params = Hash.new, headers = Hash.new) ⇒ Object



43
44
45
# File 'lib/credly/connection.rb', line 43

def delete(path, params = Hash.new, headers = Hash.new)
  request(:delete, path, params, headers)
end

#get(path, params = Hash.new, headers = Hash.new) ⇒ Object



31
32
33
# File 'lib/credly/connection.rb', line 31

def get(path, params = Hash.new, headers = Hash.new)
  request(:get, path, params, headers)
end

#new_connectionObject



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

def new_connection
  default_options = {
    :headers => {
      :accept       => 'application/json',
      :content_type => 'application/x-www-form-urlencoded',
      :user_agent   => Credly.user_agent,
    }
  }
  Faraday.new(self.base_url, default_options) do |builder|
    builder.use Credly::Response::FollowRedirects
    builder.request :multipart
    builder.request :url_encoded
    # builder.use Faraday::Request::Multipart
    # builder.use Faraday::Request::UrlEncoded

    builder.response :logger, ::Logger.new(STDOUT), :bodies => true if Credly.debugging?
    builder.adapter Faraday.default_adapter
  end
end

#post(path, params = Hash.new, headers = Hash.new) ⇒ Object



35
36
37
# File 'lib/credly/connection.rb', line 35

def post(path, params = Hash.new, headers = Hash.new)
  request(:post, path, params, headers)
end

#put(path, params = Hash.new, headers = Hash.new) ⇒ Object



39
40
41
# File 'lib/credly/connection.rb', line 39

def put(path, params = Hash.new, headers = Hash.new)
  request(:put, path, params, headers)
end

#request(method, path, params, headers) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/credly/connection.rb', line 47

def request(method, path, params, headers)
  response = connection.send(method) do |request|
    case method.to_sym
    when :delete, :get
      request.url(path, params)
    when :post, :put
      request.path = path
      request.body = params unless params.empty?
    end
    request.headers.merge!(headers)
  end
  response.body
end