Sha256: 01dad58d64b42e667a142c797dee066acb41873847208379f4e4976f7d4f6064

Contents?: true

Size: 1.69 KB

Versions: 2

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

module DevRuby
  class Client
    BASE_URL = 'https://dev.to/api'
    AUTHORIZATION_KEY = 'api-key'

    attr_reader :api_key, :adapter

    def initialize(api_key:, adapter: Faraday.default_adapter, stubs: nil)
      @api_key = api_key
      @adapter = adapter
      @stubs = stubs # Test stubs for requests
    end

    def articles
      DevRuby::Resources::ArticlesResource.new(self)
    end

    def comments
      DevRuby::Resources::CommentsResource.new(self)
    end

    def follows
      DevRuby::Resources::FollowsResource.new(self)
    end

    def followers
      DevRuby::Resources::FollowersResource.new(self)
    end

    def listings
      DevRuby::Resources::ListingsResource.new(self)
    end

    def organizations
      DevRuby::Resources::OrganizationsResource.new(self)
    end

    def podcast_episodes
      DevRuby::Resources::PodcastEpisodesResource.new(self)
    end

    def readinglist
      DevRuby::Resources::ReadinglistsResource.new(self)
    end

    def tags
      DevRuby::Resources::TagsResource.new(self)
    end

    def users
      DevRuby::Resources::UsersResource.new(self)
    end

    def profile_images
      DevRuby::Resources::ProfileImagesResource.new(self)
    end

    # rubocop:disable Layout/LineLength
    def connection
      @connection ||= Faraday.new(BASE_URL) do |conn|
        conn.headers[AUTHORIZATION_KEY] = api_key
        conn.request :json
        conn.response :json, content_type: 'application/json'
        conn.response :logger, DevRuby.logger, body: true, bodies: { request: true, response: true } if DevRuby.log_api_bodies
        conn.adapter adapter, @stubs
      end
    end
    # rubocop:enable Layout/LineLength
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dev_ruby-1.0.0 lib/dev_ruby/client.rb
dev_ruby-0.2.0 lib/dev_ruby/client.rb