Class: DevRuby::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/dev_ruby/client.rb

Constant Summary collapse

BASE_URL =
'https://dev.to/api'
AUTHORIZATION_KEY =
'api-key'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, adapter: Faraday.default_adapter, stubs: nil) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
# File 'lib/dev_ruby/client.rb', line 10

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

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



8
9
10
# File 'lib/dev_ruby/client.rb', line 8

def adapter
  @adapter
end

#api_keyObject (readonly)

Returns the value of attribute api_key.



8
9
10
# File 'lib/dev_ruby/client.rb', line 8

def api_key
  @api_key
end

Instance Method Details

#articlesObject



16
17
18
# File 'lib/dev_ruby/client.rb', line 16

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

#commentsObject



20
21
22
# File 'lib/dev_ruby/client.rb', line 20

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

#connectionObject

rubocop:disable Layout/LineLength



61
62
63
64
65
66
67
68
69
# File 'lib/dev_ruby/client.rb', line 61

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

#followersObject



28
29
30
# File 'lib/dev_ruby/client.rb', line 28

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

#followsObject



24
25
26
# File 'lib/dev_ruby/client.rb', line 24

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

#listingsObject



32
33
34
# File 'lib/dev_ruby/client.rb', line 32

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

#organizationsObject



36
37
38
# File 'lib/dev_ruby/client.rb', line 36

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

#podcast_episodesObject



40
41
42
# File 'lib/dev_ruby/client.rb', line 40

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

#profile_imagesObject



56
57
58
# File 'lib/dev_ruby/client.rb', line 56

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

#readinglistObject



44
45
46
# File 'lib/dev_ruby/client.rb', line 44

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

#tagsObject



48
49
50
# File 'lib/dev_ruby/client.rb', line 48

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

#usersObject



52
53
54
# File 'lib/dev_ruby/client.rb', line 52

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