Skip to content

Commit

Permalink
chore(config): Move the faraday connection
Browse files Browse the repository at this point in the history
  • Loading branch information
JuanVqz committed Jan 21, 2023
1 parent f2656d1 commit 3713cdf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
19 changes: 15 additions & 4 deletions lib/noko_cli/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,24 @@

module NokoCli
class Config # :nodoc:
attr_reader :token, :url, :adapter, :stubs
attr_reader :adapter, :stubs, :token, :url

def initialize(adapter: Faraday.default_adapter, stubs: nil)
@token = ENV.fetch("NOKO_TOKEN", nil)
@url = "https://api.nokotime.com/v2"
def initialize(adapter: Faraday.default_adapter, stubs: nil, token: ENV.fetch("NOKO_TOKEN", nil))
@adapter = adapter
@stubs = stubs
@token = token
@url = "https://api.nokotime.com/v2"
end

def conn
@conn ||=
Faraday.new({ url: url, params: { noko_token: token } }) do |f|
unless stubs
f.request :json
f.response :json, content_type: "application/json"
end
f.adapter adapter, stubs
end
end
end
end
18 changes: 3 additions & 15 deletions lib/noko_cli/entries.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
# frozen_string_literal: true

require "faraday"
require "tty-table"

module NokoCli
# This is an entry resource, which could be listed
class Entries
attr_reader :conn

def initialize(config:)
@token = config.token
@url = config.url
@adapter = config.adapter
@stubs = config.stubs
@conn = config.conn
end

def list
Expand All @@ -19,16 +17,6 @@ def list

private

def conn
@conn ||= Faraday.new({ url: @url, params: { noko_token: @token } }) do |f|
unless @stubs
f.request :json
f.response :json, content_type: "application/json"
end
f.adapter @adapter, @stubs
end
end

def current_user_entries
conn.get("current_user/entries").body
end
Expand Down

0 comments on commit 3713cdf

Please sign in to comment.