Class: PagecordCLI::Client

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

Defined Under Namespace

Classes: Error

Constant Summary collapse

OPEN_TIMEOUT =
10
READ_TIMEOUT =
30
WRITE_TIMEOUT =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, base_url:) ⇒ Client

Returns a new instance of Client.



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

def initialize(api_key:, base_url:)
  @api_key = api_key
  @base_url = base_url
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



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

def base_url
  @base_url
end

Instance Method Details

#create_post(params) ⇒ Object



33
34
35
# File 'lib/pagecord_cli/client.rb', line 33

def create_post(params)
  request(json_request(Net::HTTP::Post, "/posts", params))
end

#update_post(token, params) ⇒ Object



37
38
39
# File 'lib/pagecord_cli/client.rb', line 37

def update_post(token, params)
  request(json_request(Net::HTTP::Patch, "/posts/#{token}", params))
end

#upload_attachment(path) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/pagecord_cli/client.rb', line 41

def upload_attachment(path)
  uri = uri_for("/attachments")
  http_request = Net::HTTP::Post.new(uri)
  http_request["Authorization"] = "Bearer #{api_key}"
  http_request.set_form([
    [
      "file",
      File.open(path, "rb"),
      { filename: File.basename(path), content_type: content_type(path) }
    ]
  ], "multipart/form-data")

  request(http_request)
end

#verify!Object



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

def verify!
  request(Net::HTTP::Get.new(uri_for("/posts")))
  true
end