Module: NbaStats::Discovery
- Defined in:
- lib/nba_stats/discovery/discovery.rb
Class Method Summary collapse
-
.class_name(path) ⇒ String
Returns a best guess at the potential api name given a path.
-
.discover(discovery_uri) ⇒ String
Given a full API call URI, generates template resource and stats classes.
-
.resources(uri, result_sets) ⇒ String
Generates a rough resource ruby file for the api call.
-
.spec(uri) ⇒ String
Generates a rough spec ruby file for the api call.
-
.stats(uri) ⇒ String
Generates a rough stats ruby file for the api call.
Class Method Details
.class_name(path) ⇒ String
Returns a best guess at the potential api name given a path
47 48 49 |
# File 'lib/nba_stats/discovery/discovery.rb', line 47 def self.class_name(path) path.split('/').last end |
.discover(discovery_uri) ⇒ String
Given a full API call URI, generates template resource and stats classes
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/nba_stats/discovery/discovery.rb', line 12 def self.discover(discovery_uri) uri = Addressable::URI.parse(discovery_uri) client = NbaStats::Client.new output = '' output += "Discovering #{class_name(uri.path).upcase}\n" output += "------------------------------------------------\n" output += "Path: #{uri.path}\n" begin client.get(uri.path, {}) rescue Exception => e output += "Required parameters: #{e.}\n" end uri.query_values.each do |key, value| output += "#{key.underscore}=#{value}, " end output += "\n" json = JSON.parse(client.get(uri.path, uri.query_values)) result_sets = json['resultSets'] output += stats(uri) output += "\n" output += resources(uri, result_sets) output += "\n" output += spec(uri) output += "\n" output end |
.resources(uri, result_sets) ⇒ String
Generates a rough resource ruby file for the api call
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/nba_stats/discovery/discovery.rb', line 103 def self.resources(uri, result_sets) output = "------------------------------------------------\n" output += "/nba_stats/resources/#{class_name(uri.path)}.rb\n" output += "------------------------------------------------\n" output += "require 'nba_stats/resources/resource_base' require 'nba_stats/constants' module NbaStats module Resources class #{class_name(uri.path)} < ResourceBase # Array of valid result set names for this resource VALID_RESULT_SETS = [\n" result_sets.each do |result_set| output += " :#{result_set['name'].underscore}," output += " #" result_set['headers'].each do |h| output += ":#{h.underscore}, " end output += "\n" end output +=" ].freeze # @return [Array] def valid_result_sets VALID_RESULT_SETS end end # #{class_name(uri.path)} end end\n" output += '------------------------------------------------' output end |
.spec(uri) ⇒ String
Generates a rough spec ruby file for the api call
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/nba_stats/discovery/discovery.rb', line 149 def self.spec(uri) output = "------------------------------------------------\n" output += "/spec/client/#{class_name(uri.path)}_spec.rb\n" output += "------------------------------------------------\n" output += "require 'spec_helper' describe 'NbaStats' do describe 'client' do client = NbaStats::Client.new describe '.#{class_name(uri.path)}' do #{class_name(uri.path)} = client.#{class_name(uri.path)}(" uri.query_values.each do |key, value| output += "#{value}, " end output += ") it 'should return a #{class_name(uri.path)} resource' do expect(#{class_name(uri.path)}).to be_a NbaStats::Resources::#{class_name(uri.path)} end it 'should be named #{class_name(uri.path)}' do expect(#{class_name(uri.path)}.name).to eq '#{class_name(uri.path)}' end NbaStats::Resources::#{class_name(uri.path)}::VALID_RESULT_SETS.each do |valid_result_set| describe \".\#{valid_result_set}\" do it 'should return an Array' do expect(#{class_name(uri.path)}.send(valid_result_set)).to be_a Array end end end end # .#{class_name(uri.path)} end # client end\n" output += '------------------------------------------------' output end |
.stats(uri) ⇒ String
Generates a rough stats ruby file for the api call
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/nba_stats/discovery/discovery.rb', line 55 def self.stats(uri) output = "------------------------------------------------\n" output += "/nba_stats/stats/#{class_name(uri.path)}.rb\n" output += "------------------------------------------------\n" output += "require 'nba_stats/resources/#{class_name(uri.path)}' module NbaStats module #{class_name(uri.path)} # The path of the #{class_name(uri.path)} API #{class_name(uri.path).upcase}_PATH = '#{uri.path}' # Calls the #{class_name(uri.path)} API and returns a #{class_name(uri.path)} resource. # " uri.query_values.each do |key, value| output += " # @param #{key.underscore} [xxxxxxxxxx]\n" end output+=" # @return [NbaStats::Resources::#{class_name(uri.path)}] def #{class_name(uri.path)}(\n" uri.query_values.each do |key, value| output += " #{key.underscore}=#{value},\n" end output += " ) NbaStats::Resources::#{class_name(uri.path)}.new( get(#{class_name(uri.path).upcase}_PATH, { " uri.query_values.each do |key, value| output += " :#{key} => #{key.underscore},\n" end output += " }) ) end end # #{class_name(uri.path)} end " output += '------------------------------------------------' output end |