Class: DevRuby::Client

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

Constant Summary collapse

BASE_URL =
'https://dev.to/api'
API_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
15
16
# File 'lib/dev_ruby/client.rb', line 10

def initialize(api_key:, adapter: Faraday.default_adapter, stubs: nil)
  @api_key = api_key
  @adapter = adapter

  # Test stubs for requests
  @stubs = stubs
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



18
19
20
# File 'lib/dev_ruby/client.rb', line 18

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

#connectionObject



22
23
24
25
26
27
28
29
# File 'lib/dev_ruby/client.rb', line 22

def connection
  @connection ||= Faraday.new(BASE_URL) do |conn|
    conn.headers[API_KEY] = api_key
    conn.request :json
    conn.response :json, content_type: 'application/json'
    conn.adapter adapter, @stubs
  end
end