Class: WorkInStartupsAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/workinstartups-api.rb

Instance Method Summary collapse

Constructor Details

#initialize(category = 0, count = 20, random = false, type = 0) ⇒ WorkInStartupsAPI

Returns a new instance of WorkInStartupsAPI.



5
6
7
8
9
10
11
12
# File 'lib/workinstartups-api.rb', line 5

def initialize(category=0, count = 20, random = false, type = 0)
  @category = category
  @count = count
  @random = (random ? 1 : 0)
  @type = type
  @from_date = 0
  @format = 'id title'
end

Instance Method Details

#category_from_string(string) ⇒ Object

Ordering: 0: by post date 1: randomly



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/workinstartups-api.rb', line 46

def 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

#create_queryObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/workinstartups-api.rb', line 62

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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/workinstartups-api.rb', line 78

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



120
121
122
123
124
125
126
127
128
# File 'lib/workinstartups-api.rb', line 120

def get_job id=nil
  if id.nil?
    raise "No Id for job"
  end
  if @latest.nil?
    get_latest
  end
  format @latest.select{|obj| obj["id"] == id}.first
end

#get_latest(formatted = true) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/workinstartups-api.rb', line 94

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



16
17
18
# File 'lib/workinstartups-api.rb', line 16

def set_format format
  @format = format
end

#set_from(date) ⇒ Object



13
14
15
# File 'lib/workinstartups-api.rb', line 13

def set_from date
  @from_date = date
end