Class: TeamApi::ApiImpl

Inherits:
Object
  • Object
show all
Includes:
ApiImplErrorHelpers, ApiImplSnippetHelpers
Defined in:
lib/team_api/api_impl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, baseurl) ⇒ ApiImpl

Returns a new instance of ApiImpl.



12
13
14
15
16
17
# File 'lib/team_api/api_impl.rb', line 12

def initialize(site, baseurl)
  @site = site
  @data = site.data
  @index_endpoints = []
  @baseurl = baseurl
end

Instance Attribute Details

#baseurlObject

Returns the value of attribute baseurl.



8
9
10
# File 'lib/team_api/api_impl.rb', line 8

def baseurl
  @baseurl
end

#dataObject

Returns the value of attribute data.



8
9
10
# File 'lib/team_api/api_impl.rb', line 8

def data
  @data
end

#index_endpointsObject

Returns the value of attribute index_endpoints.



8
9
10
# File 'lib/team_api/api_impl.rb', line 8

def index_endpoints
  @index_endpoints
end

#siteObject

Returns the value of attribute site.



8
9
10
# File 'lib/team_api/api_impl.rb', line 8

def site
  @site
end

Instance Method Details

#envelop(endpoint, items) ⇒ Object



23
24
25
26
# File 'lib/team_api/api_impl.rb', line 23

def envelop(endpoint, items)
  return if items.nil? || items.empty?
  { 'self' => self_link(endpoint), 'results' => items }
end

#generate_error_endpointObject



91
92
93
94
# File 'lib/team_api/api_impl.rb', line 91

def generate_error_endpoint
  generate_errors_endpoint
  generate_errors_index_summary_endpoint
end

#generate_index_endpoint(endpoint, title, description, items) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/team_api/api_impl.rb', line 28

def generate_index_endpoint(endpoint, title, description, items)
  return if items.nil? || items.empty?
  Endpoint.create site, "#{baseurl}/#{endpoint}", items
  index_endpoints << {
    'endpoint' => endpoint, 'title' => title, 'description' => description
  }
end

#generate_index_endpoint_for_collection(endpoint_info) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/team_api/api_impl.rb', line 61

def generate_index_endpoint_for_collection(endpoint_info)
  collection = endpoint_info['collection']
  generate_index_endpoint(
    endpoint_info['collection'], endpoint_info['title'],
    endpoint_info['description'],
    envelop(collection, (data[collection] || {}).values))
end

#generate_item_endpoints(collection_name) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/team_api/api_impl.rb', line 69

def generate_item_endpoints(collection_name)
  (data[collection_name] || {}).each do |identifier, value|
    identifier = Canonicalizer.canonicalize(identifier)
    url = "#{baseurl}/#{collection_name}/#{identifier}"
    Endpoint.create site, url, value
  end
end

#generate_schema_endpoint(schema_file_location) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/team_api/api_impl.rb', line 36

def generate_schema_endpoint(schema_file_location)
  items = {}
  unless schema_file_location.nil? || schema_file_location.empty?
    about_yml_schema_file = File.read schema_file_location
    items = items.merge({ 'about_yml' => JSON.parse(about_yml_schema_file) })
  end

  local_schema_files.each do |filename|
    file = File.join File.dirname(__FILE__), filename
    schema = JSON.parse(File.read(file))
    items = items.merge schema
  end

  generate_index_endpoint('schemas', 'Schemas',
    'Schemas used to parse .about.yml and team member .yml files',
    items)
end

#generate_snippets_endpointsObject



84
85
86
87
88
89
# File 'lib/team_api/api_impl.rb', line 84

def generate_snippets_endpoints
  generate_latest_snippet_endpoint
  generate_snippets_by_date_endpoints
  generate_snippets_by_user_endpoints
  generate_snippets_index_summary_endpoint
end

#generate_tag_category_endpoint(category) ⇒ Object



54
55
56
57
58
59
# File 'lib/team_api/api_impl.rb', line 54

def generate_tag_category_endpoint(category)
  canonicalized = Canonicalizer.canonicalize(category)
  generate_index_endpoint(canonicalized, category,
    "Index of team members by #{category.downcase}",
    envelop(canonicalized, (data[canonicalized] || {}).values))
end

#local_schema_filesObject



77
78
79
80
81
82
# File 'lib/team_api/api_impl.rb', line 77

def local_schema_files
  %w(
    snippet_schema.json
    team_member_schema.json
  )
end


19
20
21
# File 'lib/team_api/api_impl.rb', line 19

def self_link(endpoint)
  File.join site.config['url'], baseurl, endpoint
end