Class: CommonHack
- Inherits:
-
Object
- Object
- CommonHack
- Defined in:
- lib/commonhack.rb
Overview
graceful error handling
Instance Attribute Summary collapse
-
#email ⇒ Object
readonly
makes these three fields accessable via dot notation.
-
#lastUpdated ⇒ Object
readonly
makes these three fields accessable via dot notation.
-
#username ⇒ Object
readonly
makes these three fields accessable via dot notation.
Instance Method Summary collapse
-
#bio(method) ⇒ Object
A method to help extract the bio part of the application “fields”, returns a list of fields.
-
#education(method) ⇒ Object
These fields return arrays so they pass the array to the user, rather than looping through them.
-
#fetch(parent, method) ⇒ Object
A helper method.
-
#hackathons ⇒ Object
hackathons is the same thing.
-
#initialize(url) ⇒ CommonHack
constructor
intitalizes the class.
- #projects ⇒ Object
- #skills ⇒ Object
- #work ⇒ Object
Constructor Details
#initialize(url) ⇒ CommonHack
intitalizes the class
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/commonhack.rb', line 16 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 |
Instance Attribute Details
#email ⇒ Object (readonly)
makes these three fields accessable via dot notation
11 12 13 |
# File 'lib/commonhack.rb', line 11 def email @email end |
#lastUpdated ⇒ Object (readonly)
makes these three fields accessable via dot notation
11 12 13 |
# File 'lib/commonhack.rb', line 11 def lastUpdated @lastUpdated end |
#username ⇒ Object (readonly)
makes these three fields accessable via dot notation
11 12 13 |
# File 'lib/commonhack.rb', line 11 def username @username end |
Instance Method Details
#bio(method) ⇒ Object
A method to help extract the bio part of the application “fields”, returns a list of fields
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/commonhack.rb', line 57 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 |
#education(method) ⇒ Object
These fields return arrays so they pass the array to the user, rather than looping through them
85 86 87 88 |
# File 'lib/commonhack.rb', line 85 def education(method) return @result["education"] end |
#fetch(parent, method) ⇒ Object
A helper method. All the names methods use it to interact with the named method
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/commonhack.rb', line 42 def fetch(parent,method) item = @result[parent][method] if item != nil return item else return "Error: Cannot find this field." end end |
#hackathons ⇒ Object
hackathons is the same thing
98 99 100 101 |
# File 'lib/commonhack.rb', line 98 def hackathons() return @result["hackathons"] end |
#projects ⇒ Object
103 104 105 106 |
# File 'lib/commonhack.rb', line 103 def projects() return @result["projects"] end |
#skills ⇒ Object
108 109 110 111 |
# File 'lib/commonhack.rb', line 108 def skills() return @result["skills"] end |
#work ⇒ Object
91 92 93 94 |
# File 'lib/commonhack.rb', line 91 def work() return @result["work"] end |