Class: Logbook::Client

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

Defined Under Namespace

Classes: NotAuthorized, ServerError

Constant Summary collapse

API_URL =
"https://logbook.me/entries"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Client

Returns a new instance of Client.



14
15
16
# File 'lib/logbook/client.rb', line 14

def initialize(key)
  @api_key = key
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



12
13
14
# File 'lib/logbook/client.rb', line 12

def api_key
  @api_key
end

Instance Method Details

#send_entries(entries) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/logbook/client.rb', line 18

def send_entries(entries)
  uri = URI.parse API_URL

  req = Net::HTTP::Post.new(uri.path)
  req.content_type = 'application/x-www-form-urlencoded'
  req.body = request_body(entries)

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  response = http.start {|session| session.request(req) }

  case response
    when Net::HTTPSuccess
      true
    when Net::HTTPUnauthorized
      raise NotAuthorized
    else
      raise ServerError
  end
end