Class: Writeas::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



11
12
13
14
15
16
17
18
19
# File 'lib/writeas/client.rb', line 11

def initialize
  @base_url = "https://write.as/"

  @conn ||= Faraday.new(@base_url) do |conf|
    conf.response :json
    conf.request :json
    conf.adapter Faraday.default_adapter
  end
end

Instance Attribute Details

#base_urlObject

Returns the value of attribute base_url.



9
10
11
# File 'lib/writeas/client.rb', line 9

def base_url
  @base_url
end

#connObject

Returns the value of attribute conn.



9
10
11
# File 'lib/writeas/client.rb', line 9

def conn
  @conn
end

Instance Method Details

#delete_post(post_id:, token:) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/writeas/client.rb', line 89

def delete_post(post_id:, token:)
  response = @conn.delete("/api/posts/#{post_id}", {token: token})

  if error_response?(response)
    raise ClientError.new(response.reason_phrase, response.status)
  else
    return true
  end
end

#publish_post(body:, title: nil, font: nil, lang: nil, rtl: nil, created: nil, crosspost: nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/writeas/client.rb', line 37

def publish_post(body:, title: nil, font: nil, lang: nil, rtl: nil, created: nil, crosspost: nil)
  request_body = {
    body: body,
    title: title,
    font: font,
    lang: lang,
    rtl: rtl,
    created: created,
    crosspost: crosspost
  }

  response = @conn.post('/api/posts', request_body.to_json)

  if error_response?(response)
    raise ClientError.new(response.reason_phrase, response.status)
  else
    post = Writeas::Post.new(response.body)
    return post
  end
end

#render_markdown(raw_body) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/writeas/client.rb', line 21

def render_markdown(raw_body)
  body = {
    raw_body: raw_body
  }

  response = @conn.post('/api/markdown', body.to_json)

  if error_response?(response)
    raise ClientError.new(response.reason_phrase, response.status)
  else
    client_response = Writeas::MarkdownResponse.new(response.body)
  end

  return client_response
end

#retrieve_post(post_id:) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/writeas/client.rb', line 58

def retrieve_post(post_id:)
  response = @conn.get("/api/posts/#{post_id}")

  if error_response?(response)
    raise ClientError.new(response.reason_phrase, response.status)
  else
    post = Writeas::Post.new(response.body)
    return post
  end
end

#update_post(post_id:, body:, token:, title: nil, font: nil, lang: nil, rtl: nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/writeas/client.rb', line 69

def update_post(post_id:, body:, token:, title: nil, font: nil, lang: nil, rtl: nil)
  request_body = {
    body: body,
    token: token,
    title: title,
    font: font,
    lang: lang,
    rtl: rtl
  }

  response = @conn.post("/api/posts/#{post_id}", request_body.to_json)

  if error_response?(response)
    raise ClientError.new(response.reason_phrase, response.status)
  else
    post = Writeas::Post.new(response.body)
    return post
  end
end