Class: HashcardsReadwise::Pusher
- Inherits:
-
Object
- Object
- HashcardsReadwise::Pusher
- Defined in:
- lib/hashcards_readwise/pusher.rb
Constant Summary collapse
- READWISE_API_URL =
"https://readwise.io/api/v2/highlights/"
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
-
#initialize(token:, logger: Logger.new($stderr, level: Logger::WARN)) ⇒ Pusher
constructor
A new instance of Pusher.
- #push(highlights) ⇒ Object
Constructor Details
#initialize(token:, logger: Logger.new($stderr, level: Logger::WARN)) ⇒ Pusher
Returns a new instance of Pusher.
14 15 16 17 |
# File 'lib/hashcards_readwise/pusher.rb', line 14 def initialize(token:, logger: Logger.new($stderr, level: Logger::WARN)) @token = token @logger = logger end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
12 13 14 |
# File 'lib/hashcards_readwise/pusher.rb', line 12 def logger @logger end |
Instance Method Details
#push(highlights) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/hashcards_readwise/pusher.rb', line 19 def push(highlights) uri = URI.parse(READWISE_API_URL) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER http.cert_store = OpenSSL::X509::Store.new.tap(&:set_default_paths) request = Net::HTTP::Post.new(uri.request_uri) request["Authorization"] = "Token #{@token}" request["Content-Type"] = "application/json" request.body = JSON.generate(highlights) logger.info("POSTing #{highlights["highlights"].size} highlights to Readwise...") response = http.request(request) case response when Net::HTTPSuccess logger.info("Success! Response: #{response.body}") { success: true, body: JSON.parse(response.body) } else logger.error("Failed with status #{response.code}") { success: false, code: response.code, body: response.body } end end |