forked from Jberlinsky/commonhack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.rb
116 lines (73 loc) · 1.68 KB
/
item.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
require 'json'
require 'open-uri'
# graceful error handling
class CommonHack
#makes these three fields accessable via dot notation
attr_reader :username,:email,:lastUpdated
#intitalizes the class
def initialize(url)
if url.include? "http://" or url.include? "https://"
body = open(url)
contents = body.read
else
#its a local file, so use File.Read
file = File.open("data.json", "rb")
contents = file.read
end
@result = JSON.parse(contents)
#make the basic first level info accessible
@username = @result["username"]
@email = @result["email"]
@lastUpdated = @result["lastUpdated"]
end
# A helper method. All the names methods use it to
# interact with the named method
def fetch(parent,method)
item = @result[parent][method]
if item != nil
return item
else
return "Error: Cannot find this field."
end
end
# A method to help extract the bio part of the application
# "fields", returns a list of fields
def bio(method)
if method != "fields"
return fetch("bio",method)
else
return [
"firstName",
"lastName",
"gender",
"email",
"phone",
"dietaryRestrictions",
"summary",
"location",
"websites",
"resume",
"picture"
]
end
end
# These fields return arrays so they pass the array to the user,
# rather than looping through them
def education(method)
return @result["education"]
end
def work()
return @result["work"]
end
# hackathons is the same thing
def hackathons()
return @result["hackathons"]
end
def projects()
return @result["projects"]
end
def skills()
return @result["skills"]
end
end
app = CommonHack.new("data.json")