Module: Datahunter

Defined in:
lib/datahunter.rb,
lib/datahunter/base.rb,
lib/datahunter/version.rb

Constant Summary collapse

DATASETS_URL =

DATASETS_URL = “localhost:3000/api/datasets/”

"http://shrouded-harbor-5877.herokuapp.com/api/datasets/"
FEEDBACK_URL =
"https://docs.google.com/forms/d/1yNzZjCCXvWHQCbWz4sx-nui3LafeeLcT7FF9T-vbKvw/viewform"
REQUEST_URL =
"https://docs.google.com/forms/d/1NRKWmb_mcpKJmrutXvZSZnysM_v0rfLhjD897H3Myrw/viewform?usp=send_form"
VERSION =
"0.4.0"

Class Method Summary collapse

Class Method Details

.datasets_url(query) ⇒ Object



25
26
27
# File 'lib/datahunter/base.rb', line 25

def self.datasets_url query
  "#{DATASETS_URL}#{Datahunter.query_string_builder query}"
end

.download_file(url, format = "", alt_url = "") ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/datahunter/base.rb', line 69

def self.download_file url, format="", alt_url=""
  uri = Addressable::URI.parse(url)
  extension = Datahunter.parse_extension_from_uri uri

  if !@extensions.include? extension
    Launchy.open(url, options = {})
  else
    location = Dir.pwd
    uri = Addressable::URI.parse(url)
    file_name = uri.basename
    loc = location + "/" + file_name

    case ask ("### Create/overwrite #{loc}?(y/rename/n)".colorize(:yellow))
    when 'rename'
      loc = ask "Path/to/filename: ".colorize(:yellow)
    when 'n'
      abort("Ok then")
    end
    puts "### Start downloading..."
    Downloadr::HTTP.download(url, loc)
    puts "### Your file has been downloaded ;)".colorize(:green)
    Datahunter.print_excuse_and_alternative_url_message alt_url
  end
end

.download_the_data(dataset) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/datahunter/base.rb', line 94

def self.download_the_data dataset
  resources = dataset["resources"]
  number_of_downloadable_links = resources.size

  if number_of_downloadable_links == 1
    dl = 0
  else
    Datahunter.print_downloadable_links resources
    dl = ask("### which one? (0/1/...)".colorize(:yellow), Integer) {|i| i.in = 0..(number_of_downloadable_links - 1)}
  end
  
  dl = dl.to_i
  Datahunter.download_file(resources[dl]["url"], resources[dl]["format"], dataset["uri"])
end

.ds_url(id) ⇒ Object



29
30
31
# File 'lib/datahunter/base.rb', line 29

def self.ds_url id
  "#{DATASETS_URL}#{id}"    
end

.get_dataset(dataset) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/datahunter/base.rb', line 118

def self.get_dataset dataset
  if dataset.has_key?("resources") and dataset["resources"].any?
    Datahunter.download_the_data dataset
  else
    Datahunter.open_in_browser dataset["uri"]
  end
end

.open_in_browser(url) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/datahunter/base.rb', line 109

def self.open_in_browser url
  if url =~ /\A#{URI::regexp}\z/
    puts "You can't download this dataset directly, but you should from there"
    Launchy.open(url, options = {})
  else
    Datahunter.print_bad_uri_message
  end
end

.parse_extension_from_uri(uri) ⇒ Object



65
66
67
# File 'lib/datahunter/base.rb', line 65

def self.parse_extension_from_uri uri
  uri.basename.split(".").last
end


142
143
144
145
# File 'lib/datahunter/base.rb', line 142

def self.print_bad_uri_message
  puts "The URL given by the publisher is not valid. We'll try to find out why "\
       "as soon as we can!".colorize(:red)
end


45
46
47
48
49
50
51
52
53
54
55
# File 'lib/datahunter/base.rb', line 45

def self.print_coll_of_datasets_info_light coll_of_datasets
  coll_of_datasets.each_with_index do |ds, index|
    puts ("#{index+1}. ".colorize(:yellow) +
          "#{ds["title"]}".colorize(:green) +
          " id: ".colorize(:blue) +
          "#{ds["_id"]}")
    puts ("#{ds["spatial"].take(5)}")
    puts ("#{ds["description"][0..100].gsub(/\w+\s*$/,'...')}".colorize(:blue))
  end
  puts
end


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/datahunter/base.rb', line 33

def self.print_dataset_info dataset
  puts ("#{dataset["title"]}".colorize(:green))
  puts ("#{dataset["description"]}".colorize(:blue))
  puts
  puts ("publisher: ".colorize(:blue) + "#{dataset["publisher"]}")
  puts ("temporal: ".colorize(:blue) + "#{dataset["temporal"]}")
  puts ("spatial: ".colorize(:blue) + "#{dataset["spatial"]}")
  puts ("created: ".colorize(:blue) + "#{dataset["created"]}")
  puts ("updated: ".colorize(:blue) + "#{dataset["updated"]}")
  puts ("score: ".colorize(:blue) + "#{dataset["huntscore"]}")
end


57
58
59
60
61
62
63
# File 'lib/datahunter/base.rb', line 57

def self.print_downloadable_links resources
  resources.each_with_index do |dl, i|
    puts ("#{i}. ".colorize(:yellow) +
          "#{dl["title"]} - ".colorize(:blue) + 
          "#{dl["format"]}".colorize(:green))
  end
end


134
135
136
137
138
139
140
# File 'lib/datahunter/base.rb', line 134

def self.print_excuse_and_alternative_url_message alt_url=""
  puts "If this is not the file you expected, it's maybe because publisher"\
       " don't always keep the metadata up-to-date. We try to clean most of"\
       " uri's and check the url. Anyway you may be able to download your"\
       " file by hand here:"
  puts "#{alt_url}".colorize(:blue)
end

Feedback requests



149
150
151
152
153
154
155
156
# File 'lib/datahunter/base.rb', line 149

def self.print_feedback_request
  case ask "### give feedback? (y/n)".colorize(:yellow)
  when 'y'
    Launchy.open(FEEDBACK_URL, options = {})
  else
    puts "Bye for now!"
  end
end

Messages



127
128
129
130
131
132
# File 'lib/datahunter/base.rb', line 127

def self.print_no_dataset_message
  puts "We've found nothing for your query. "\
       "Remember, this is a first prototype, there will be a lot more "\
       "datasets indexed soon. If you want us to find a dataset for you, or "\
       "if you just want to give us a feedback, don't hesitate!".colorize(:red)
end


158
159
160
161
162
163
# File 'lib/datahunter/base.rb', line 158

def self.print_request_dataset_message
  case ask "### request a dataset? (y/n)".colorize(:yellow)
  when 'y'
    Launchy.open(REQUEST_URL, options = {})
  end
end

.query_string_builder(string) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/datahunter/base.rb', line 16

def self.query_string_builder string
  s = string
      .strip
      .gsub(/ {3,}/, ' ')
      .gsub(/ {2,}/, ' ')
      .gsub(/ /, '+')
  "?q=#{s}"
end