Class: WorkInStartupsAPI
- Inherits:
-
Object
- Object
- WorkInStartupsAPI
- Defined in:
- lib/workinstartups-api.rb
Overview
Ordering: 0: by post date 1: randomly
Constant Summary collapse
- DEFAULT_CATEGORY =
0
- DEFAULT_COUNT =
20
- DEFAULT_ORDER =
false
- DEFAULT_TYPE =
0
Class Method Summary collapse
Instance Method Summary collapse
- #create_query ⇒ Object
- #format(string) ⇒ Object
- #get_job(id = nil) ⇒ Object
- #get_latest(formatted = true) ⇒ Object
-
#initialize(category = DEFAULT_CATEGORY, count = DEFAULT_COUNT, random = DEFAULT_ORDER, type = DEFAULT_TYPE) ⇒ WorkInStartupsAPI
constructor
A new instance of WorkInStartupsAPI.
- #set_format(format) ⇒ Object
- #set_from(date) ⇒ Object
Constructor Details
#initialize(category = DEFAULT_CATEGORY, count = DEFAULT_COUNT, random = DEFAULT_ORDER, type = DEFAULT_TYPE) ⇒ WorkInStartupsAPI
Returns a new instance of WorkInStartupsAPI.
35 36 37 38 39 40 41 42 |
# File 'lib/workinstartups-api.rb', line 35 def initialize(category=DEFAULT_CATEGORY, count = DEFAULT_COUNT, random = DEFAULT_ORDER, type = DEFAULT_TYPE) @category = category @count = count @random = (random ? 1 : 0) @type = type @from_date = 0 @format = 'id title' end |
Class Method Details
.category_from_string(string) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/workinstartups-api.rb', line 49 def self.category_from_string string stripped = string.downcase.gsub(/\W+/, '') category = case stripped when 'all' then 0 when 'cofounder' then 16 when 'programmer' then 1 when 'designer' then 2 when 'intern' then 3 when 'tester' then 5 when 'marketer' then 7 when 'manager' then 8 when 'consultant' then 9 when 'sale' then 15 end end |
Instance Method Details
#create_query ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/workinstartups-api.rb', line 65 def create_query base_uri = URI.parse("http://workinstartups.com/job-board/api/api.php") params = { action: 'getJobs', type: @type, category: @category, count: @count, random: @random, days_behind: 0, response: 'json' } query_string = params.map{|k,v| "#{k}=#{v}"}.join('&') @query = base_uri @query.query = query_string @query end |
#format(string) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/workinstartups-api.rb', line 81 def format string formatted = "" if @format.include?"id" formatted += "id: " + string["id"] end if @format.include?"title" formatted += "\nTitle: " + string["title"] end if @format.include?"category" formatted += "\nCategory: " + string["category_name"] end if @format.include?"description" formatted += "\nDescription: " + string["description"] end formatted end |
#get_job(id = nil) ⇒ Object
123 124 125 126 127 128 129 130 131 |
# File 'lib/workinstartups-api.rb', line 123 def get_job id=nil if id.nil? raise "No Id for job" end if @latest.nil? get_latest end format @latest.detect{|obj| obj["id"] == id}.first end |
#get_latest(formatted = true) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/workinstartups-api.rb', line 97 def get_latest formatted=true query = create_query open(query) do |f| f.each_line do |line| raw = line unless raw.nil? data = raw.gsub('var jobs = ','').gsub(';', '') obj = JSON.parse(data) @formatted = Array.new @latest = Array.new @from_date ||= Date.today obj.each do |job| if Date.parse(job["created_on"]) > @from_date @formatted << (format job) @latest << job end end end end end if formatted @formatted else @latest end end |
#set_format(format) ⇒ Object
46 47 48 |
# File 'lib/workinstartups-api.rb', line 46 def set_format format @format = format end |
#set_from(date) ⇒ Object
43 44 45 |
# File 'lib/workinstartups-api.rb', line 43 def set_from date @from_date = date end |