Class: HackOne

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ HackOne

Returns a new instance of HackOne.



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
# File 'lib/hackone.rb', line 27

def initialize(url)
  if url.include? "http://" or url.include? "https://"
    url += "?api_key=#{HackOne.api_key}" if HackOne.api_key != ""
    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

  begin
    @result = cleanse(JSON.parse(contents))
  rescue
    raise HackOneInvalidCommonHackDocument
  end

  if @result["error"] == "user_not_found"
    raise HackOneUserNotFound
  end

  if @result["error"] == "api_key_invalid"
    raise HackOneInvalidAPIKey
  end

  # Basic level.
  @username = @result[:username]
  @email = @result[:email]
  @lastUpdated = @result[:lastUpdated]
  @shirtSize = @result[:shirtSize]
  @is_private = @result[:private]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/hackone.rb', line 106

def method_missing(method, *args, &block)
  attr = method.to_s
  if @result
    @result[attr]
  else
    super
  end
end

Instance Attribute Details

#emailObject (readonly)

Returns the value of attribute email.



13
14
15
# File 'lib/hackone.rb', line 13

def email
  @email
end

#is_privateObject (readonly)

Returns the value of attribute is_private.



13
14
15
# File 'lib/hackone.rb', line 13

def is_private
  @is_private
end

#lastUpdatedObject (readonly)

Returns the value of attribute lastUpdated.



13
14
15
# File 'lib/hackone.rb', line 13

def lastUpdated
  @lastUpdated
end

#shirtSizeObject (readonly)

Returns the value of attribute shirtSize.



13
14
15
# File 'lib/hackone.rb', line 13

def shirtSize
  @shirtSize
end

#usernameObject (readonly)

Returns the value of attribute username.



13
14
15
# File 'lib/hackone.rb', line 13

def username
  @username
end

Class Method Details

.api_keyObject



19
20
21
# File 'lib/hackone.rb', line 19

def self.api_key
  @api_key || ""
end

.api_key=(api_key) ⇒ Object



15
16
17
# File 'lib/hackone.rb', line 15

def self.api_key=(api_key)
  @api_key = api_key
end

.parse(url) ⇒ Object



23
24
25
# File 'lib/hackone.rb', line 23

def self.parse(url)
  new(url)
end

Instance Method Details

#bioObject

A method to help extract the bio part of the application “fields”, returns a list of fields



82
83
84
# File 'lib/hackone.rb', line 82

def bio
  return @result[:bio]
end

#educationObject



86
87
88
# File 'lib/hackone.rb', line 86

def education
  return @result[:education]
end

#fetch(parent, method) ⇒ Object

A helper method. All the names methods use it to interact with the named method



70
71
72
73
74
75
76
77
# File 'lib/hackone.rb', line 70

def fetch(parent,method)
  item = @result[parent][method]
  if item != nil 
    return item
  else
    return "Error: Cannot find this field."
  end
end

#hackathonsObject



94
95
96
# File 'lib/hackone.rb', line 94

def hackathons
  return @result[:hackathons]
end

#private?Boolean

Returns:



60
61
62
# File 'lib/hackone.rb', line 60

def private?
  @is_private
end

#projectsObject



98
99
100
# File 'lib/hackone.rb', line 98

def projects
  return @result[:projects]
end

#public?Boolean

Returns:



64
65
66
# File 'lib/hackone.rb', line 64

def public?
  !@is_private
end

#skillsObject



102
103
104
# File 'lib/hackone.rb', line 102

def skills
  return @result[:skills]
end

#workObject



90
91
92
# File 'lib/hackone.rb', line 90

def work
  return @result[:work]
end