Class: Gitabu::Items

Inherits:
Object
  • Object
show all
Defined in:
lib/gitabu/items.rb

Overview

Items of Github API documentation that get populated into ruby templates.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_spec_result, version) ⇒ Items

Returns a new instance of Items.



13
14
15
16
# File 'lib/gitabu/items.rb', line 13

def initialize(json_spec_result, version)
  @json_spec_result = json_spec_result
  @version          = version
end

Instance Attribute Details

#json_spec_resultObject

Returns the value of attribute json_spec_result.



7
8
9
# File 'lib/gitabu/items.rb', line 7

def json_spec_result
  @json_spec_result
end

#versionObject

Returns the value of attribute version.



7
8
9
# File 'lib/gitabu/items.rb', line 7

def version
  @version
end

Class Method Details

.items(json_spec_result:, version:) ⇒ Object



9
10
11
# File 'lib/gitabu/items.rb', line 9

def self.items(json_spec_result:, version:)
  new(json_spec_result, version).items
end

Instance Method Details

#api_hash_for(block, values) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/gitabu/items.rb', line 93

def api_hash_for(block, values)
  api_hash = {}
  api_hash["http_method"]                  = block["method"]
  api_hash["endpoint"]                     = block["endpoint"].gsub("{", '#{')
  api_hash["description"]                  = block["namespace_description"].downcase
  api_hash["method_name"]                  = block["namespace_description"].downcase.gsub("-", "_").split(" ").join("_")
  api_hash["auth"]                         = values[:auth].size.zero? ? "nil" : "{ #{values[:auth].join(", ")} }"
  api_hash["body"]                         = values[:body].size.zero? ? "nil" : "{ #{values[:body].join(", ")} }"
  api_hash["documentation"]                = values[:documentation].size.zero? ? "# @params options [Hash]" : values[:documentation].join("\n\t\t\t\t")
  api_hash["headers"]                      = values[:headers].size.zero? ? "nil" : "{ #{values[:headers].join(", ")} }"
  api_hash["params"]                       = values[:params].size.zero? ? "nil" : "{ #{values[:params].join(", ")} }"
  api_hash["path_params_flat"]             = values[:params_flat].size.zero? ? "(options)" : "(#{values[:params_flat].join(", ")}, options)"
  api_hash["params_key_value"]             = values[:params_kv].size.zero? ? "(options: nil)" : "(#{values[:params_kv].join(", ")}, options: nil)"
  api_hash["params_key_value_without_nil"] = values[:params_kv_no_nil].size.zero? ? "options: options)" : "(#{values[:params_kv_no_nil].join(", ")}, options: nil)"
  api_hash
end

#get_auth_from(value) ⇒ Object



57
58
59
# File 'lib/gitabu/items.rb', line 57

def get_auth_from(value)
  "#{value["fields"]["name"]}: #{value["fields"]["name"]}"
end

#get_body_from(value) ⇒ Object



61
62
63
# File 'lib/gitabu/items.rb', line 61

def get_body_from(value)
  "#{value["fields"]["name"]}: #{value["fields"]["name"]}"
end

#get_docu_from(value) ⇒ Object



65
66
67
# File 'lib/gitabu/items.rb', line 65

def get_docu_from(value)
  "# @param #{value["fields"]["name"]} [#{value["fields"]["type"].capitalize}] #{value["fields"]["description"]}"
end

#get_headers_from(value) ⇒ Object



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

def get_headers_from(value)
  if value["fields"]["name"] == "accept"
    "#{value["fields"]["name"]}: 'application/vnd.github.v#{version}+json'"
  else
    "#{value["fields"]["name"]}: #{value["fields"]["name"]}"
  end
end

#get_params_flat_from(value) ⇒ Object



81
82
83
# File 'lib/gitabu/items.rb', line 81

def get_params_flat_from(value)
  value["fields"]["name"].to_s
end

#get_params_from(value) ⇒ Object



77
78
79
# File 'lib/gitabu/items.rb', line 77

def get_params_from(value)
  "#{value["fields"]["name"]}: options[:#{value["fields"]["name"]}]"
end

#get_params_kv_from(value) ⇒ Object



85
86
87
# File 'lib/gitabu/items.rb', line 85

def get_params_kv_from(value)
  "#{value["fields"]["name"]}: nil"
end

#get_params_kv_no_nil_from(value) ⇒ Object



89
90
91
# File 'lib/gitabu/items.rb', line 89

def get_params_kv_no_nil_from(value)
  "#{value["fields"]["name"]}: #{value["fields"]["name"]}"
end

#itemsObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/gitabu/items.rb', line 18

def items
  collection = []

  json_spec_result.each do |block|
    fields = block.except("namespace_description", "method", "endpoint")
    values = values_from(fields)
    collection << api_hash_for(block, values)
  end

  collection
end

#values_from(fields) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/gitabu/items.rb', line 30

def values_from(fields)
  auth             = []
  body             = []
  documentation    = []
  headers          = []
  params           = []
  params_flat      = []
  params_kv        = []
  params_kv_no_nil = []

  fields.each do |_, value|
    auth             << get_auth_from(value)             if value["field_type"] == "auth"
    body             << get_body_from(value)             if value["field_type"] == "body"
    documentation    << get_docu_from(value)             if %w[body path].include?(value["field_type"])
    headers          << get_headers_from(value)          if value["field_type"] == "header"
    params           << get_params_from(value)           if value["field_type"] == "query"
    params_flat      << get_params_flat_from(value)      if %w[body path].include?(value["field_type"])
    params_kv        << get_params_kv_from(value)        if %w[body path].include?(value["field_type"])
    params_kv_no_nil << get_params_kv_no_nil_from(value) if %w[body path].include?(value["field_type"])
  end

  {
    auth: auth, body: body, documentation: documentation, headers: headers,
    params: params, params_flat: params_flat, params_kv: params_kv, params_kv_no_nil: params_kv_no_nil
  }
end