This repository has been archived by the owner on Apr 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.rb
121 lines (111 loc) · 3.41 KB
/
api.rb
1
2
3
4
5
6
7
8
9
10
11
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
42
43
44
45
46
47
48
49
50
51
52
53
54
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
require "grape"
require "rack/contrib"
require "./csrhub_company.rb"
require "./category.rb"
$api_root = "http://socialimpact.harryrickards.com/"
module SocialImpact
class API < Grape::API
use Rack::JSONP
format :json
helpers do
# TODO Is there not a gem that can do this in a less hackish, less likely
# to break way?
def format_output data
if data.is_a? Hash
return Hash[data.map { |k, v| [k, format_output(v)] }]
elsif data.is_a? Array
return data.map { |x| format_output(x) }
elsif data.is_a? String
datai = data.to_i
dataf = data.to_f
if (%w{N/A NA - NR} << "").include? data
return nil
elsif datai.to_s == data
return datai
elsif (Float(data) rescue false)
return dataf
# elsif data =~ /(N\/A|[\d+\-%]*) +- +(N\/A|[\d+\-%\.]*)/i
# groups = data.match /(N\/A|[\d+\-%]*) +- +(N\/A|[\d+\-%\.]*)/i
# return format_output [groups[1], groups[2]]
elsif data[0] == "+"
num = format_output(data[1..-1])
if num.is_a? String
return data
else
return num
end
elsif data[0] == "-"
num = format_output(data[1..-1])
unless num.is_a? Integer or num.is_a? Float
return data
else
return -num
end
else
data.gsub! /<b>(.*)<\/b>/i, '\1'
data.gsub! " ", ""
return data
end
else
return data
end
end
end
before do
header "Access-Control-Allow-Origin", "*"
$api_root = "http://#{env['HTTP_HOST']}"
end
resource :companies do
desc "See possible search filters."
get '/search' do
format_output CSRHubCompany.search_filters
end
desc "See possible search operators."
get '/search/:filter' do
format_output CSRHubCompany.search_operators(params[:filter])
end
desc "Search for companies."
get '/search/:search_filter', requirements: {search_filter: /.*/} do
filter_string = params[:search_filter].split("/")
filters = []
filter_string.each_slice(3) do |filter, operator, value|
filters << {
filter: filter,
operator: operator,
value: value
}
end
format_output CSRHubCompany.search(filters)
end
desc "Return info on a company."
params do
requires :name, type: String, desc: "Company Name"
end
get '/:name', requirements: { name: /.*/ } do
name = params[:name]
name = name[0...-1] if name[-1] == "."
# name.gsub! /[^a-zA-Z]/, " "
company = CSRHubCompany.new name: name
format_output company.resp
end
end
resource :categories do
desc "View a list of all categories."
get do
format_output Category.all
end
desc "Get a list of companies for a certain category"
get '/:name', requirements: { name: /.*/ } do
format_output CSRHubCompany.in_category(params[:name])
end
end
resource :subcategories do
desc "Get a list of subcategories for a certain category"
route_param :name do
get do
format_output Category.subcategories_of(params[:name])
end
end
end
end
end