Class: Linketysplit::ApiEndpoints

Inherits:
Object
  • Object
show all
Defined in:
lib/linketysplit/api_endpoints.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, linketysplit_origin = "https://linketysplit.com") ⇒ ApiEndpoints

Returns a new instance of ApiEndpoints.



23
24
25
26
# File 'lib/linketysplit/api_endpoints.rb', line 23

def initialize(api_key, linketysplit_origin="https://linketysplit.com")
  @api_key = api_key
  @linketysplit_origin = linketysplit_origin
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



20
21
22
# File 'lib/linketysplit/api_endpoints.rb', line 20

def api_key
  @api_key
end

#linketysplit_originObject (readonly)

Returns the value of attribute linketysplit_origin.



20
21
22
# File 'lib/linketysplit/api_endpoints.rb', line 20

def linketysplit_origin
  @linketysplit_origin
end

Instance Method Details

#article(permalink) ⇒ Object



66
67
68
69
70
# File 'lib/linketysplit/api_endpoints.rb', line 66

def article(permalink)
  encoded_permalink = CGI.escape(permalink)
  url = "#{linketysplit_origin}/api/v1/publication/article/#{encoded_permalink}"
  get(url)
end

#get(url) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/linketysplit/api_endpoints.rb', line 40

def get(url)
  response = Typhoeus.get(
    url,
    headers: { "Authorization" => "Bearer #{api_key}", "Accept" => "application/json" }
  )
  handle_response(response)
end

#handle_response(response) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/linketysplit/api_endpoints.rb', line 28

def handle_response(response)
  if response.success?
    { error: nil, data: JSON.parse(response.body) }
  elsif response.timed_out?
    { error: ApiEndpointFailedError.new("API request to #{url} timed out"), data: nil }
  elsif response.code == 0
    { error: ApiEndpointFailedError.new("API request to #{url} failed"), data: nil }
  else
    { error: ApiError.new(response), data: nil }
  end
end

#post(url, data) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/linketysplit/api_endpoints.rb', line 48

def post(url, data)
  response = Typhoeus.post(
    url,
    headers: {
      "Authorization" => "Bearer #{api_key}",
      "Content-Type"  => "application/json",
      "Accept"        => "application/json"
    },
    body: JSON.dump(data)
  )
  handle_response(response)
end

#publicationObject



61
62
63
64
# File 'lib/linketysplit/api_endpoints.rb', line 61

def publication
  url = "#{linketysplit_origin}/api/v1/publication"
  get(url)
end

#upsert_article(permalink) ⇒ Object



72
73
74
75
76
# File 'lib/linketysplit/api_endpoints.rb', line 72

def upsert_article(permalink)
  url = "#{linketysplit_origin}/api/v1/publication/article"
  data = { permalink: }
  post(url, data)
end

#verify_article_access(article_access_id) ⇒ Object



78
79
80
81
82
# File 'lib/linketysplit/api_endpoints.rb', line 78

def verify_article_access(article_access_id)
  url = "#{linketysplit_origin}/api/v1/publication/verify-article-access"
  data = { articleAccessId: article_access_id }
  post(url, data)
end