Module: Percy::Client::Connection

Included in:
Percy::Client
Defined in:
lib/percy/client/connection.rb

Defined Under Namespace

Classes: NiceErrorMiddleware, NoCookiesHTTPClientAdapter

Instance Method Summary collapse

Instance Method Details

#connectionObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/percy/client/connection.rb', line 34

def connection
  return @connection if defined?(@connection)
  parsed_uri = URI.parse(config.api_url)
  base_url = "#{parsed_uri.scheme}://#{parsed_uri.host}:#{parsed_uri.port}"
  @connection = Faraday.new(url: base_url) do |faraday|
    faraday.request :token_auth, config.access_token if config.access_token

    faraday.use Percy::Client::Connection::NoCookiesHTTPClientAdapter
    faraday.use Percy::Client::Connection::NiceErrorMiddleware
  end
  @connection
end

#get(path) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/percy/client/connection.rb', line 47

def get(path)
  response = connection.get do |request|
    request.url(path)
    request.headers['Content-Type'] = 'application/vnd.api+json'
  end
  JSON.parse(response.body)
end

#post(path, data) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/percy/client/connection.rb', line 55

def post(path, data)
  response = connection.post do |request|
    request.url(path)
    request.headers['Content-Type'] = 'application/vnd.api+json'
    request.body = data.to_json
  end
  JSON.parse(response.body)
end