hawatel_search_jobs 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,9 @@
1
- module HawatelSearchJobs
2
- module Api
3
- require 'hawatel_search_jobs/api/indeed'
4
- require 'hawatel_search_jobs/api/reed'
5
- require 'hawatel_search_jobs/api/xing'
6
- require 'hawatel_search_jobs/api/careerbuilder'
7
- require 'hawatel_search_jobs/api/careerjet'
8
- end
9
- end
1
+ module HawatelSearchJobs
2
+ module Api
3
+ require 'hawatel_search_jobs/api/indeed'
4
+ require 'hawatel_search_jobs/api/reed'
5
+ require 'hawatel_search_jobs/api/xing'
6
+ require 'hawatel_search_jobs/api/careerbuilder'
7
+ require 'hawatel_search_jobs/api/careerjet'
8
+ end
9
+ end
@@ -1,170 +1,173 @@
1
- require 'ostruct'
2
- require 'active_support/core_ext/hash'
3
-
4
- module HawatelSearchJobs
5
- module Api
6
- ##
7
- # = Carerrbuilder API
8
- #
9
- # @see http://api.careerbuilder.com/Search/jobsearch/jobsearchinfo.aspx
10
- module Careerbuilder
11
- class << self
12
- include HawatelSearchJobs::Helpers::Base
13
-
14
- DEFAULT = {
15
- :keywords => '',
16
- :location => '',
17
- :company => ''
18
- }
19
-
20
- RESULT_LIMIT = 25
21
-
22
- # Search jobs by specific criteria
23
- # @param [Hash] args
24
- # @option args :settings [Hash] search criteria
25
- # - *:api* (String) - URL API (default: api.careerbuilder.com)
26
- # - *:version* (String) - a version of API (default: v2)
27
- # - *:clientid* (String) - Private Developer Key (see http://developer.careerbuilder.com)
28
- # @option args :query [Hash] settings of API
29
- # - *:keywors* (String)
30
- # - *:location* (String)
31
- # - *:company* (String)
32
- # @example
33
- # jobs = Careerbuilder.search({:settings => {
34
- # :api => 'api.careerbuilder.com',
35
- # :version => 'v2',
36
- # :clientid => 'secret-code'},
37
- # :query => {
38
- # :keywords => 'ruby',
39
- # :location => 'London',
40
- # :company => ''}})
41
- # @return [Hash] First page of the result (see constant RESULT_LIMIT)
42
- def search(args)
43
- args[:query] = DEFAULT.merge(args[:query]) if args[:query]
44
-
45
- if args[:query_key].nil?
46
- url_request = prepare_conn_string(args) + prepare_query(args)
47
- else
48
- url_request = args[:query_key]
49
- end
50
-
51
- response = send_request(url_request)
52
- attributes = build_jobs_table(response, url_request)
53
- OpenStruct.new(attributes)
54
- end
55
-
56
-
57
- # Get a specific page result
58
- # At the beging you have to run {search} method and get :key from result and pass it to the argument :query_key
59
- # @param [Hash] args
60
- # @option args :settings [Hash] see {search}
61
- # @option args [Integer] :page page number counted from 0
62
- # @option args [String] :query_key
63
- # @example
64
- # jobs = Careerbuilder.page({:settings => {
65
- # :api => 'api.careerbuilder.com',
66
- # :version => 'v2',
67
- # :clientid => 'secret-code'},
68
- # :page => 5,
69
- # :query_key => 'value from :key returned by search method'})
70
- # @return [Hash] Job offers from specific page
71
- def page(args)
72
- page = args[:page].to_i || 0
73
- if args[:query_key]
74
- url_request = args[:query_key].gsub(/&PageNumber=\d+/, '') << "&PageNumber=#{page+1}"
75
- args[:query_key] = url_request
76
- search(args)
77
- end
78
- end
79
-
80
- private
81
-
82
- def build_jobs_table(response, url_request)
83
- attributes = Hash.new
84
- attributes[:code] = response.code.to_i
85
- attributes[:msg] = response.message
86
-
87
- attributes[:totalResults] = 0
88
- attributes[:page] = nil
89
- attributes[:last] = nil
90
- attributes[:key] = url_request
91
- attributes[:jobs] = nil
92
-
93
- if response.code.to_i == 200
94
- xml_response = Hash.from_xml(response.body.gsub('\n', ''))
95
- begin
96
- if xml_response['ResponseJobSearch'] && xml_response['ResponseJobSearch']['Results']
97
- attributes[:totalResults] = xml_response['ResponseJobSearch']['TotalCount'].to_i || 0
98
- attributes[:page] = get_page_number(url_request) - 1
99
- attributes[:last] = xml_response['ResponseJobSearch']['TotalPages'].to_i - 1
100
- attributes[:key] = url_request
101
- attributes[:jobs] = parse_raw_data(xml_response)
102
- end
103
- rescue
104
- raise "Something wrong with returned data: #{xml_response}"
105
- end
106
- end
107
- attributes
108
- end
109
-
110
- def prepare_query(args)
111
- "Keywords=#{args[:query][:keywords]}&" \
112
- "Location=#{args[:query][:location]}&" \
113
- "CompanyName=#{args[:query][:company]}"
114
- end
115
-
116
- def prepare_conn_string(args)
117
- conn_string = "https://#{args[:settings][:api]}/#{args[:settings][:version]}/jobsearch?" \
118
- "PerPage=#{RESULT_LIMIT}&DeveloperKey=#{args[:settings][:clientid]}&"
119
-
120
- conn_string
121
- end
122
-
123
- def parse_raw_data(data)
124
- jobs = Array.new
125
- data['ResponseJobSearch']['Results']['JobSearchResult'].each do |offer|
126
- job = Hash.new
127
- job[:jobtitle] = offer['JobTitle'] if offer['JobTitle']
128
-
129
- country = get_country_name(offer['State'], data['ResponseJobSearch']['SearchMetaData']['SearchLocations'])
130
- location = country
131
- location += ", #{offer['City']}" if offer['City'] && country != offer['City']
132
- job[:location] = location
133
- job[:company] = offer['Company']
134
-
135
- newdate = offer['PostedDate'].split('/')
136
- job[:date] = "#{newdate[1]}/#{newdate[0]}/#{newdate[2]}"
137
-
138
- job[:date] = convert_date_to_format(job[:date],'%d/%m/%y')
139
- job[:url] = offer['JobDetailsURL']
140
-
141
- job = convert_empty_to_nil(job)
142
-
143
- jobs << OpenStruct.new(job)
144
- end
145
- jobs
146
- rescue
147
- raise "Cannot parse raw data - #{data.inspect}"
148
- end
149
-
150
- def get_country_name(code, locations)
151
- locations.each do |loc|
152
- if loc[1]['StateCode'] && code && code.to_s == loc[1]['StateCode'].to_s
153
- return loc[1]['City'] if loc[1]['City']
154
- end
155
- end
156
-
157
- 'United States'
158
- rescue
159
- raise "Cannot get country name (code=#{code}, locations=#{locations}"
160
- end
161
-
162
- def get_page_number(url)
163
- result = url.match(/\&PageNumber=(\d+)/)
164
- result ? result[1].to_i : 1
165
- end
166
-
167
- end
168
- end
169
- end
1
+ require 'ostruct'
2
+ require 'active_support/core_ext/hash'
3
+
4
+ module HawatelSearchJobs
5
+ module Api
6
+ ##
7
+ # = Carerrbuilder API
8
+ #
9
+ # @see http://api.careerbuilder.com/Search/jobsearch/jobsearchinfo.aspx
10
+ module Careerbuilder
11
+ class << self
12
+ include HawatelSearchJobs::Helpers::Base
13
+
14
+ DEFAULT = {
15
+ :keywords => '',
16
+ :location => '',
17
+ :company => ''
18
+ }
19
+
20
+ RESULT_LIMIT = 25
21
+
22
+ # Search jobs by specific criteria
23
+ # @param [Hash] args
24
+ # @option args :settings [Hash] search criteria
25
+ # - *:api* (String) - URL API (default: api.careerbuilder.com)
26
+ # - *:version* (String) - a version of API (default: v2)
27
+ # - *:clientid* (String) - Private Developer Key (see http://developer.careerbuilder.com)
28
+ # @option args :query [Hash] settings of API
29
+ # - *:keywors* (String)
30
+ # - *:location* (String)
31
+ # - *:company* (String)
32
+ # @example
33
+ # jobs = Careerbuilder.search({:settings => {
34
+ # :api => 'api.careerbuilder.com',
35
+ # :version => 'v2',
36
+ # :clientid => 'secret-code'},
37
+ # :query => {
38
+ # :keywords => 'ruby',
39
+ # :location => 'London',
40
+ # :company => ''}})
41
+ # @return [Hash] First page of the result (see constant RESULT_LIMIT)
42
+ def search(args)
43
+ args[:query] = DEFAULT.merge(args[:query]) if args[:query]
44
+
45
+ if args[:query_key].nil?
46
+ url_request = prepare_conn_string(args) + prepare_query(args)
47
+ else
48
+ url_request = args[:query_key]
49
+ end
50
+
51
+ response = send_request(url_request)
52
+ attributes = build_jobs_table(response, url_request)
53
+ OpenStruct.new(attributes)
54
+ end
55
+
56
+
57
+ # Get a specific page result
58
+ # At the beging you have to run {search} method and get :key from result and pass it to the argument :query_key
59
+ # @param [Hash] args
60
+ # @option args :settings [Hash] see {search}
61
+ # @option args [Integer] :page page number counted from 0
62
+ # @option args [String] :query_key
63
+ # @example
64
+ # jobs = Careerbuilder.page({:settings => {
65
+ # :api => 'api.careerbuilder.com',
66
+ # :version => 'v2',
67
+ # :clientid => 'secret-code'},
68
+ # :page => 5,
69
+ # :query_key => 'value from :key returned by search method'})
70
+ # @return [Hash] Job offers from specific page
71
+ def page(args)
72
+ page = args[:page].to_i || 0
73
+ if args[:query_key]
74
+ url_request = args[:query_key].gsub(/&PageNumber=\d+/, '') << "&PageNumber=#{page+1}"
75
+ args[:query_key] = url_request
76
+ search(args)
77
+ end
78
+ end
79
+
80
+ private
81
+
82
+ def build_jobs_table(response, url_request)
83
+ attributes = Hash.new
84
+ attributes[:code] = response.code.to_i
85
+ attributes[:msg] = response.message
86
+
87
+ attributes[:totalResults] = 0
88
+ attributes[:page] = nil
89
+ attributes[:last] = nil
90
+ attributes[:key] = url_request
91
+ attributes[:jobs] = nil
92
+
93
+ if response.code.to_i == 200
94
+ xml_response = Hash.from_xml(response.body.gsub('\n', ''))
95
+ begin
96
+ if xml_response['ResponseJobSearch'] && xml_response['ResponseJobSearch']['Results']
97
+ attributes[:totalResults] = xml_response['ResponseJobSearch']['TotalCount'].to_i || 0
98
+ attributes[:page] = get_page_number(url_request) - 1
99
+ attributes[:last] = xml_response['ResponseJobSearch']['TotalPages'].to_i - 1
100
+ attributes[:key] = url_request
101
+ attributes[:jobs] = parse_raw_data(xml_response)
102
+ end
103
+ rescue
104
+ raise "Something wrong with returned data: #{xml_response}"
105
+ end
106
+ end
107
+ attributes
108
+ end
109
+
110
+ def prepare_query(args)
111
+ "Keywords=#{args[:query][:keywords]}&" \
112
+ "Location=#{args[:query][:location]}&" \
113
+ "CompanyName=#{args[:query][:company]}"
114
+ end
115
+
116
+ def prepare_conn_string(args)
117
+ page_size = args[:settings][:page_size].to_s.empty? ? RESULT_LIMIT : args[:settings][:page_size].to_i
118
+ page_size = RESULT_LIMIT if page_size <= 0 || page_size > 100
119
+
120
+ conn_string = "https://#{args[:settings][:api]}/#{args[:settings][:version]}/jobsearch?" \
121
+ "PerPage=#{page_size}&OrderBy=Date&DeveloperKey=#{args[:settings][:clientid]}&"
122
+
123
+ conn_string
124
+ end
125
+
126
+ def parse_raw_data(data)
127
+ jobs = Array.new
128
+ data['ResponseJobSearch']['Results']['JobSearchResult'].each do |offer|
129
+ job = Hash.new
130
+ job[:jobtitle] = offer['JobTitle'] if offer['JobTitle']
131
+
132
+ country = get_country_name(offer['State'], data['ResponseJobSearch']['SearchMetaData']['SearchLocations'])
133
+ location = country
134
+ location += ", #{offer['City']}" if offer['City'] && country != offer['City']
135
+ job[:location] = location
136
+ job[:company] = offer['Company']
137
+
138
+ newdate = offer['PostedDate'].split('/')
139
+ job[:date] = "#{newdate[1]}/#{newdate[0]}/#{newdate[2]}"
140
+
141
+ job[:date] = convert_date_to_format(job[:date],'%d/%m/%y')
142
+ job[:url] = offer['JobDetailsURL']
143
+
144
+ job = convert_empty_to_nil(job)
145
+
146
+ jobs << OpenStruct.new(job)
147
+ end
148
+ jobs
149
+ rescue
150
+ raise "Cannot parse raw data - #{data.inspect}"
151
+ end
152
+
153
+ def get_country_name(code, locations)
154
+ locations.each do |loc|
155
+ if loc[1]['StateCode'] && code && code.to_s == loc[1]['StateCode'].to_s
156
+ return loc[1]['City'] if loc[1]['City']
157
+ end
158
+ end
159
+
160
+ 'United States'
161
+ rescue
162
+ raise "Cannot get country name (code=#{code}, locations=#{locations}"
163
+ end
164
+
165
+ def get_page_number(url)
166
+ result = url.match(/\&PageNumber=(\d+)/)
167
+ result ? result[1].to_i : 1
168
+ end
169
+
170
+ end
171
+ end
172
+ end
170
173
  end
@@ -1,126 +1,131 @@
1
- module HawatelSearchJobs
2
- module Api
3
- module CareerJet
4
- class << self
5
- include HawatelSearchJobs::Helpers::Base
6
-
7
- DEFAULT = {
8
- :keywords => '',
9
- :location => '',
10
- :company => ''
11
- }
12
-
13
- # Search jobs based on specified keywords or location
14
- #
15
- # @param args [Hash]
16
- # @option args :query [Hash] search criteria
17
- # - *:keywords*​ (String) - keywords for search
18
- # @option args [Integer] :page page number (default value 0)
19
- # @option args [String] :query_key option provided by page() method
20
- #
21
- # @example
22
- # search(:settings => HawatelSearchJobs.careerjet,:query => {:keywords => 'ruby'})
23
- # search(:query_key => 'http://api.../search?locale_code=US_en&sort=date&keywords=ruby', :page => 0)
24
- #
25
- # @return [Hash<OpenStruct>]
26
- def search(args)
27
- args[:query] = DEFAULT.merge(args[:query]) if args[:query]
28
- args[:page] = 0 if args[:page].nil?
29
-
30
- if args[:query_key].to_s.empty?
31
- url_request = build_url(args)
32
- else
33
- url_request = args[:query_key]
34
- end
35
-
36
- if url_request
37
- attributes = Hash.new
38
- response = send_request(url_request)
39
- result = JSON(response.body)
40
- if response.code == '200' && result['type'] == 'ERROR'
41
- attributes[:code] = 501
42
- attributes[:msg] = result['error']
43
- return OpenStruct.new(attributes)
44
- else
45
- attributes[:code] = response.code
46
- attributes[:msg] = response.message
47
- return OpenStruct.new(attributes) if response.code != '200'
48
- end
49
- attributes[:totalResults] = result['hits']
50
- attributes[:page] = args[:page]
51
- attributes[:last] = result['pages'] - 1
52
- attributes[:key] = url_request
53
- attributes[:jobs] = parse_raw_data(result)
54
- OpenStruct.new(attributes)
55
- else
56
- OpenStruct.new({:code => 501, :msg => 'lack of keywords or api setting'})
57
- end
58
- end
59
-
60
- # Show next page of results
61
- #
62
- # @param args [Hash]
63
- # @option args [Integer] :page specified page number (default 0)
64
- # @option args [String] :query_key full url from last query
65
- #
66
- # @example
67
- # page({:query_key => result.key, :page => 2}
68
- #
69
- # @return [Hash<OpenStrunct>]
70
- def page(args)
71
- args[:page] = 0 if args[:page].nil?
72
- args[:query_key] = args[:query_key].gsub(/&page=.*/, '') << "&page=#{args[:page]+1}"
73
- return search(args)
74
- end
75
-
76
- private
77
-
78
- # Build query URL
79
- #
80
- # @param args [Hash]
81
- # @option args query [Hash] - search criteria
82
- # - *:keywords* (String) - keywords for search
83
- # - *:location* (String) - specified location for search criteria (default: europe)
84
- # @option settings [Hash]
85
- # - *:api* (String) - api ip or domainname
86
- #
87
- # @example
88
- # build_url(:query => {:keywords => 'ruby'}, :settings => {:api => 'http://api...'} }
89
- #
90
- # @return [String] ready to call URL
91
- def build_url(args)
92
- keywords = args[:query][:keywords] if !args[:query][:keywords].to_s.empty?
93
- api_url = args[:settings][:api] if !args[:settings][:api].to_s.empty?
94
- if keywords && api_url
95
- !args[:query][:location].to_s.empty? ? location = args[:query][:location] : location = 'europe'
96
- "http://#{api_url}/search?locale_code=US_en&pagesize=25&sort=date&keywords=#{keywords}&location=#{location}&page=1"
97
- end
98
- end
99
-
100
-
101
- # Build jobs array with specified attributes
102
- #
103
- # @param result [Hash]
104
- # @option result [Hash] :jobs jobs hash array return from API
105
- #
106
- # @return [Array<OpenStruct>]
107
- def parse_raw_data(result)
108
- jobs = Array.new
109
- return jobs if result['jobs'].to_s.empty?
110
- result['jobs'].each do |offer|
111
- job = Hash.new
112
- job[:jobtitle] = offer['title']
113
- job[:location] = offer['locations']
114
- job[:company] = offer['company']
115
- job[:date] = convert_date_to_format(offer['date'],'%d/%m/%y')
116
- job[:url] = offer['url']
117
- job = convert_empty_to_nil(job)
118
- jobs << OpenStruct.new(job)
119
- end
120
- return jobs
121
- end
122
-
123
- end
124
- end
125
- end
126
- end
1
+ module HawatelSearchJobs
2
+ module Api
3
+ module CareerJet
4
+ class << self
5
+ include HawatelSearchJobs::Helpers::Base
6
+
7
+ DEFAULT = {
8
+ :keywords => '',
9
+ :location => '',
10
+ :company => ''
11
+ }
12
+
13
+ RESULT_LIMIT = 25
14
+
15
+ # Search jobs based on specified keywords or location
16
+ #
17
+ # @param args [Hash]
18
+ # @option args :query [Hash] search criteria
19
+ # - *:keywords*​ (String) - keywords for search
20
+ # @option args [Integer] :page page number (default value 0)
21
+ # @option args [String] :query_key option provided by page() method
22
+ #
23
+ # @example
24
+ # search(:settings => HawatelSearchJobs.careerjet,:query => {:keywords => 'ruby'})
25
+ # search(:query_key => 'http://api.../search?locale_code=US_en&sort=date&keywords=ruby', :page => 0)
26
+ #
27
+ # @return [Hash<OpenStruct>]
28
+ def search(args)
29
+ args[:query] = DEFAULT.merge(args[:query]) if args[:query]
30
+ args[:page] = 0 if args[:page].nil?
31
+
32
+ if args[:query_key].to_s.empty?
33
+ url_request = build_url(args)
34
+ else
35
+ url_request = args[:query_key]
36
+ end
37
+
38
+ if url_request
39
+ attributes = Hash.new
40
+ response = send_request(url_request)
41
+ result = JSON(response.body)
42
+ if response.code == '200' && result['type'] == 'ERROR'
43
+ attributes[:code] = 501
44
+ attributes[:msg] = result['error']
45
+ return OpenStruct.new(attributes)
46
+ else
47
+ attributes[:code] = response.code
48
+ attributes[:msg] = response.message
49
+ return OpenStruct.new(attributes) if response.code != '200'
50
+ end
51
+ attributes[:totalResults] = result['hits']
52
+ attributes[:page] = args[:page]
53
+ attributes[:last] = result['pages'] - 1
54
+ attributes[:key] = url_request
55
+ attributes[:jobs] = parse_raw_data(result)
56
+ OpenStruct.new(attributes)
57
+ else
58
+ OpenStruct.new({:code => 501, :msg => 'lack of keywords or api setting'})
59
+ end
60
+ end
61
+
62
+ # Show next page of results
63
+ #
64
+ # @param args [Hash]
65
+ # @option args [Integer] :page specified page number (default 0)
66
+ # @option args [String] :query_key full url from last query
67
+ #
68
+ # @example
69
+ # page({:query_key => result.key, :page => 2}
70
+ #
71
+ # @return [Hash<OpenStrunct>]
72
+ def page(args)
73
+ args[:page] = 0 if args[:page].nil?
74
+ args[:query_key] = args[:query_key].gsub(/&page=.*/, '') << "&page=#{args[:page]+1}"
75
+ return search(args)
76
+ end
77
+
78
+ private
79
+
80
+ # Build query URL
81
+ #
82
+ # @param args [Hash]
83
+ # @option args query [Hash] - search criteria
84
+ # - *:keywords* (String) - keywords for search
85
+ # - *:location* (String) - specified location for search criteria (default: europe)
86
+ # @option settings [Hash]
87
+ # - *:api* (String) - api ip or domainname
88
+ #
89
+ # @example
90
+ # build_url(:query => {:keywords => 'ruby'}, :settings => {:api => 'http://api...'} }
91
+ #
92
+ # @return [String] ready to call URL
93
+ def build_url(args)
94
+ keywords = args[:query][:keywords] if !args[:query][:keywords].to_s.empty?
95
+ api_url = args[:settings][:api] if !args[:settings][:api].to_s.empty?
96
+ page_size = args[:settings][:page_size].to_s.empty? ? RESULT_LIMIT : args[:settings][:page_size].to_i
97
+ page_size = RESULT_LIMIT if page_size <= 0 || page_size > 99
98
+
99
+ if keywords && api_url
100
+ !args[:query][:location].to_s.empty? ? location = args[:query][:location] : location = 'worldwide'
101
+ "http://#{api_url}/search?locale_code=US_en&pagesize=#{page_size}&sort=date&keywords=#{keywords}&location=#{location}&page=1"
102
+ end
103
+ end
104
+
105
+
106
+ # Build jobs array with specified attributes
107
+ #
108
+ # @param result [Hash]
109
+ # @option result [Hash] :jobs jobs hash array return from API
110
+ #
111
+ # @return [Array<OpenStruct>]
112
+ def parse_raw_data(result)
113
+ jobs = Array.new
114
+ return jobs if result['jobs'].to_s.empty?
115
+ result['jobs'].each do |offer|
116
+ job = Hash.new
117
+ job[:jobtitle] = offer['title']
118
+ job[:location] = offer['locations']
119
+ job[:company] = offer['company']
120
+ job[:date] = convert_date_to_format(offer['date'],'%d/%m/%y')
121
+ job[:url] = offer['url']
122
+ job = convert_empty_to_nil(job)
123
+ jobs << OpenStruct.new(job)
124
+ end
125
+ return jobs
126
+ end
127
+
128
+ end
129
+ end
130
+ end
131
+ end