Module: Percy::Client::Connection

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

Defined Under Namespace

Classes: FaradayNiceErrorMiddleware

Instance Method Summary collapse

Instance Method Details

#connectionObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/percy/client/connection.rb', line 22

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 Faraday::Adapter::HTTPClient
    faraday.use Percy::Client::Connection::FaradayNiceErrorMiddleware
  end
  @connection
end

#get(path) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/percy/client/connection.rb', line 35

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



43
44
45
46
47
48
49
50
# File 'lib/percy/client/connection.rb', line 43

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