Module: HNsearch

Defined in:
lib/HNsearch.rb,
lib/HNsearch/cli.rb,
lib/HNsearch/version.rb,
lib/HNsearch/HNsearch_api.rb

Overview

The driver

Defined Under Namespace

Classes: CLI, HNsearchAPI

Constant Summary collapse

VERSION =
"1.0.1"

Class Method Summary collapse

Class Method Details

.filter(result) ⇒ Object

Example:

>> HNsearch.filter(some_json_object)
=> Returns result key-value from the JSON object

Arguments:

result: (JSON object)


43
44
45
# File 'lib/HNsearch.rb', line 43

def self.filter(result)
  return result.fetch("results")
end

.items(query) ⇒ Object

Example:

>> HNsearch.items("awesome")
=> Returns JSON object of the result and then runs print_items

Arguments:

query: (String)


29
30
31
32
# File 'lib/HNsearch.rb', line 29

def self.items(query)
  result = HNsearchAPI.new.query_items(query)
  print_items filter(result)
end


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/HNsearch.rb', line 73

def self.print_items(result)
  result.each {
    |e|
    e = e.fetch("item")
    title = e.fetch("title")
    url = e.fetch("url")
    username = e.fetch("username")

    out = []
    out.push "Title: #{title}"
    out.push "URL: #{url}"
    out.push "Posted by: https://news.ycombinator.com/user?id=#{username} (#{username})"

    puts out.join("\n")
    puts("\n")
  }
end

Example:

>> HNsearch.filter(some_json_object)
=> Returns result key-value from the JSON object

Arguments:

result: (JSON object)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/HNsearch.rb', line 56

def self.print_users(result)
  result.each {
    |e|
    e = e.fetch("item")
    username = e.fetch("username")
    karma = e.fetch("karma")

    out = []
    out.push "Username: #{username}"
    out.push "Karma: #{karma}"
    out.push "Profile URL: https://news.ycombinator.com/user?id=#{username}"

    puts out.join("\n")
    puts("\n")
  }
end

.users(query) ⇒ Object

Example:

>> HNsearch.users("aniketpant")
=> Returns JSON object of the result and then runs print_users

Arguments:

query: (String)


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

def self.users(query)
  result = HNsearchAPI.new.query_users(query)
  print_users filter(result)
end