class WorkInStartupsAPI

Ordering: 0: by post date 1: randomly

Constants

DEFAULT_CATEGORY
DEFAULT_COUNT
DEFAULT_ORDER
DEFAULT_TYPE

Public Class Methods

category_from_string(string) click to toggle source
# 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
new(category=DEFAULT_CATEGORY, count = DEFAULT_COUNT, random = DEFAULT_ORDER, type = DEFAULT_TYPE) click to toggle source
# 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

Public Instance Methods

create_query() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/workinstartups-api.rb, line 46
def set_format format
  @format = format
end
set_from(date) click to toggle source
# File lib/workinstartups-api.rb, line 43
def set_from date
  @from_date = date
end