Sha256: 1c0ad1ff9870c1056bb8e9bfd12a3113f4e5d3b36d74df755d731ca3252a2f96

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

module Spearly
  module Cloud
    class Client
      def initialize(token)
        @token = token
      end

      def get_sites(params = {})
        uri = "#{ENV.fetch('SPEARLY_API_URL', nil)}/sites"
        uri_parsed = Addressable::URI.parse(uri).normalize.to_s
        client = Faraday.default_connection

        res = client.get(uri_parsed,
                         params.as_json,
                         'Accept' => 'application/vnd.spearly.v2+json',
                         'Authorization' => @token)

        if res.status == 200
          JSON.parse(res.body)['data']
        else
          []
        end
      end

      def get_site(site_id)
        uri = "#{ENV.fetch('SPEARLY_API_URL', nil)}/sites/#{site_id}"
        uri_parsed = Addressable::URI.parse(uri).normalize.to_s
        client = Faraday.default_connection

        res = client.get(uri_parsed,
                         nil,
                         'Accept' => 'application/vnd.spearly.v2+json',
                         'Authorization' => @token)

        if res.status == 200
          JSON.parse(res.body)['data']
        else
          {}
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spearly-sdk-ruby-0.10.0 lib/spearly/cloud/client.rb
spearly-sdk-ruby-0.9.0 lib/spearly/cloud/client.rb