hawatel_search_jobs 0.1.0 → 0.1.1

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,186 +1,186 @@
1
- require 'ostruct'
2
-
3
- module HawatelSearchJobs
4
- module Api
5
- ##
6
- # = Reed.co.uk API
7
- #
8
- # @see https://www.reed.co.uk/developers/Jobseeker
9
- module Reed
10
- class << self
11
- include HawatelSearchJobs::Helpers::Base
12
-
13
- DEFAULT = {
14
- :keywords => '',
15
- :location => '',
16
- }
17
-
18
- RESULT_LIMIT = 25
19
-
20
- # Search jobs by specific criteria
21
- # @param [Hash] args
22
- # @option args :settings [Hash] search criteria
23
- # - *:api* (String) - URL API (default: reed.co.uk/api)
24
- # - *:version* (String) - a version of API (default: 1.0)
25
- # - *:clientid* (String) - API Key (see https://www.reed.co.uk/developers/Jobseeker)
26
- # @option args :query [Hash] settings of API
27
- # - *:keywors* (String)
28
- # - *:location* (String)
29
- # @example
30
- # jobs = Reed.search({:settings => {
31
- # :api => 'reed.co.uk/api',
32
- # :version => '1.0',
33
- # :clientid => 'secret-code'},
34
- # :query => {
35
- # :keywords => 'ruby',
36
- # :location => 'London'})
37
- # @return [Hash] First page of the result (see constant RESULT_LIMIT)
38
- def search(args)
39
- args[:query] = DEFAULT.merge(args[:query]) if args[:query]
40
-
41
- if args[:query_key].nil?
42
- url_request = prepare_conn_string(args) + prepare_query(args)
43
- else
44
- url_request = args[:query_key]
45
- end
46
-
47
- response = api_request(url_request, args[:settings][:clientid])
48
- attributes = build_jobs_table(response, url_request)
49
- OpenStruct.new(attributes)
50
- end
51
-
52
- # Get a specific page result
53
- # At the beging you have to run {search} method and get :key from result and pass it to the argument :query_key
54
- # @param [Hash] args
55
- # @option args :settings [Hash] see {search}
56
- # @option args [Integer] :page page number counted from 0
57
- # @option args [String] :query_key
58
- # @example
59
- # jobs = Careerbuilder.page({:settings => {
60
- # :api => 'api.careerbuilder.com',
61
- # :version => 'v2',
62
- # :clientid => 'secret-code'},
63
- # :page => 5,
64
- # :query_key => 'value from :key returned by search method'})
65
- # @return [Hash] Job offers from specific page
66
- def page(args)
67
- page = args[:page].to_i || 0
68
- if args[:query_key]
69
- limit = result_limit(args[:query_key])
70
- url_request = args[:query_key].gsub(/&resultsToSkip=\d+/, '') << "&resultsToSkip=#{page * limit}"
71
- args[:query_key] = url_request
72
- search(args)
73
- end
74
- end
75
-
76
- private
77
-
78
- def build_jobs_table(response, url_request)
79
- attributes = Hash.new
80
- attributes[:code] = response.code.to_i
81
- attributes[:msg] = response.message
82
-
83
- attributes[:totalResults] = 0
84
- attributes[:page] = nil
85
- attributes[:last] = nil
86
- attributes[:key] = url_request
87
- attributes[:jobs] = nil
88
-
89
-
90
- if response.code.to_i == 200
91
- json_response = JSON.parse(response.body)
92
- begin
93
- if !json_response['results'].to_s.empty?
94
- attributes[:totalResults] = json_response['totalResults'] || 0
95
-
96
- page_info = paging_info(url_request, attributes[:totalResults])
97
- attributes[:page] = page_info.page
98
- attributes[:last] = page_info.last
99
-
100
- attributes[:key] = url_request
101
- attributes[:jobs] = parse_raw_data(json_response)
102
- end
103
- rescue
104
- raise "Something wrong with returned data: #{json_response}"
105
- end
106
- end
107
- attributes
108
- end
109
-
110
- def api_request(url, clientid = nil)
111
- opt = Hash.new
112
-
113
- if clientid
114
- opt = { :basic_auth => {
115
- :username => clientid,
116
- :password => ''
117
- }}
118
- end
119
-
120
- send_request(url, opt)
121
- end
122
-
123
- def prepare_query(args)
124
- "keywords=#{args[:query][:keywords]}&locationName=#{args[:query][:location]}"
125
- end
126
-
127
- def prepare_conn_string(args)
128
- conn_string = "https://#{args[:settings][:api]}/#{args[:settings][:version]}/search?resultsToTake=#{RESULT_LIMIT}&"
129
- conn_string
130
- end
131
-
132
- def parse_raw_data(data)
133
- jobs = Array.new
134
- data['results'].each do |offer|
135
- job = Hash.new
136
- job[:jobtitle] = offer['jobTitle'] if offer['jobTitle']
137
- job[:location] = "United Kingdom, #{offer['locationName']}"
138
- job[:company] = offer['employerName']
139
- job[:date] = convert_date_to_format(offer['date'],'%d/%m/%y')
140
- job[:url] = offer['jobUrl']
141
-
142
- job = convert_empty_to_nil(job)
143
-
144
- jobs << OpenStruct.new(job)
145
- end
146
- jobs
147
- rescue
148
- raise "Cannot parse raw data: #{data}"
149
- end
150
-
151
- def result_limit(url)
152
- result = url.match(/\resultsToTake=(\d+)/)
153
- result ? result[1].to_i : RESULT_LIMIT
154
- end
155
-
156
- def result_skip(url)
157
- result = url.match(/\&resultsToSkip=(\d+)/)
158
- result ? result[1].to_i : 0
159
- end
160
-
161
- def paging_info(url, total_result)
162
- return OpenStruct.new({:page => nil, :last => nil}) if total_result == 0
163
-
164
- result_limit = result_limit(url)
165
- result_skip = result_skip(url)
166
-
167
- if result_skip == 0 && total_result > 0
168
- page = 0
169
- else
170
- page = (result_skip / result_limit).to_i
171
- end
172
-
173
- mod = total_result.to_i % result_limit.to_i
174
- if mod == 0
175
- last = (total_result.to_i / result_limit.to_i) - 1
176
- else
177
- last = (total_result.to_i / result_limit.to_i).to_i
178
- end
179
-
180
- OpenStruct.new({:page => page, :last => last})
181
- end
182
-
183
- end
184
- end
185
- end
1
+ require 'ostruct'
2
+
3
+ module HawatelSearchJobs
4
+ module Api
5
+ ##
6
+ # = Reed.co.uk API
7
+ #
8
+ # @see https://www.reed.co.uk/developers/Jobseeker
9
+ module Reed
10
+ class << self
11
+ include HawatelSearchJobs::Helpers::Base
12
+
13
+ DEFAULT = {
14
+ :keywords => '',
15
+ :location => '',
16
+ }
17
+
18
+ RESULT_LIMIT = 25
19
+
20
+ # Search jobs by specific criteria
21
+ # @param [Hash] args
22
+ # @option args :settings [Hash] search criteria
23
+ # - *:api* (String) - URL API (default: reed.co.uk/api)
24
+ # - *:version* (String) - a version of API (default: 1.0)
25
+ # - *:clientid* (String) - API Key (see https://www.reed.co.uk/developers/Jobseeker)
26
+ # @option args :query [Hash] settings of API
27
+ # - *:keywors* (String)
28
+ # - *:location* (String)
29
+ # @example
30
+ # jobs = Reed.search({:settings => {
31
+ # :api => 'reed.co.uk/api',
32
+ # :version => '1.0',
33
+ # :clientid => 'secret-code'},
34
+ # :query => {
35
+ # :keywords => 'ruby',
36
+ # :location => 'London'})
37
+ # @return [Hash] First page of the result (see constant RESULT_LIMIT)
38
+ def search(args)
39
+ args[:query] = DEFAULT.merge(args[:query]) if args[:query]
40
+
41
+ if args[:query_key].nil?
42
+ url_request = prepare_conn_string(args) + prepare_query(args)
43
+ else
44
+ url_request = args[:query_key]
45
+ end
46
+
47
+ response = api_request(url_request, args[:settings][:clientid])
48
+ attributes = build_jobs_table(response, url_request)
49
+ OpenStruct.new(attributes)
50
+ end
51
+
52
+ # Get a specific page result
53
+ # At the beging you have to run {search} method and get :key from result and pass it to the argument :query_key
54
+ # @param [Hash] args
55
+ # @option args :settings [Hash] see {search}
56
+ # @option args [Integer] :page page number counted from 0
57
+ # @option args [String] :query_key
58
+ # @example
59
+ # jobs = Careerbuilder.page({:settings => {
60
+ # :api => 'api.careerbuilder.com',
61
+ # :version => 'v2',
62
+ # :clientid => 'secret-code'},
63
+ # :page => 5,
64
+ # :query_key => 'value from :key returned by search method'})
65
+ # @return [Hash] Job offers from specific page
66
+ def page(args)
67
+ page = args[:page].to_i || 0
68
+ if args[:query_key]
69
+ limit = result_limit(args[:query_key])
70
+ url_request = args[:query_key].gsub(/&resultsToSkip=\d+/, '') << "&resultsToSkip=#{page * limit}"
71
+ args[:query_key] = url_request
72
+ search(args)
73
+ end
74
+ end
75
+
76
+ private
77
+
78
+ def build_jobs_table(response, url_request)
79
+ attributes = Hash.new
80
+ attributes[:code] = response.code.to_i
81
+ attributes[:msg] = response.message
82
+
83
+ attributes[:totalResults] = 0
84
+ attributes[:page] = nil
85
+ attributes[:last] = nil
86
+ attributes[:key] = url_request
87
+ attributes[:jobs] = nil
88
+
89
+
90
+ if response.code.to_i == 200
91
+ json_response = JSON.parse(response.body)
92
+ begin
93
+ if !json_response['results'].to_s.empty?
94
+ attributes[:totalResults] = json_response['totalResults'] || 0
95
+
96
+ page_info = paging_info(url_request, attributes[:totalResults])
97
+ attributes[:page] = page_info.page
98
+ attributes[:last] = page_info.last
99
+
100
+ attributes[:key] = url_request
101
+ attributes[:jobs] = parse_raw_data(json_response)
102
+ end
103
+ rescue
104
+ raise "Something wrong with returned data: #{json_response}"
105
+ end
106
+ end
107
+ attributes
108
+ end
109
+
110
+ def api_request(url, clientid = nil)
111
+ opt = Hash.new
112
+
113
+ if clientid
114
+ opt = { :basic_auth => {
115
+ :username => clientid,
116
+ :password => ''
117
+ }}
118
+ end
119
+
120
+ send_request(url, opt)
121
+ end
122
+
123
+ def prepare_query(args)
124
+ "keywords=#{args[:query][:keywords]}&locationName=#{args[:query][:location]}"
125
+ end
126
+
127
+ def prepare_conn_string(args)
128
+ conn_string = "https://#{args[:settings][:api]}/#{args[:settings][:version]}/search?resultsToTake=#{RESULT_LIMIT}&"
129
+ conn_string
130
+ end
131
+
132
+ def parse_raw_data(data)
133
+ jobs = Array.new
134
+ data['results'].each do |offer|
135
+ job = Hash.new
136
+ job[:jobtitle] = offer['jobTitle'] if offer['jobTitle']
137
+ job[:location] = "United Kingdom, #{offer['locationName']}"
138
+ job[:company] = offer['employerName']
139
+ job[:date] = convert_date_to_format(offer['date'],'%d/%m/%y')
140
+ job[:url] = offer['jobUrl']
141
+
142
+ job = convert_empty_to_nil(job)
143
+
144
+ jobs << OpenStruct.new(job)
145
+ end
146
+ jobs
147
+ rescue
148
+ raise "Cannot parse raw data: #{data}"
149
+ end
150
+
151
+ def result_limit(url)
152
+ result = url.match(/\resultsToTake=(\d+)/)
153
+ result ? result[1].to_i : RESULT_LIMIT
154
+ end
155
+
156
+ def result_skip(url)
157
+ result = url.match(/\&resultsToSkip=(\d+)/)
158
+ result ? result[1].to_i : 0
159
+ end
160
+
161
+ def paging_info(url, total_result)
162
+ return OpenStruct.new({:page => nil, :last => nil}) if total_result == 0
163
+
164
+ result_limit = result_limit(url)
165
+ result_skip = result_skip(url)
166
+
167
+ if result_skip == 0 && total_result > 0
168
+ page = 0
169
+ else
170
+ page = (result_skip / result_limit).to_i
171
+ end
172
+
173
+ mod = total_result.to_i % result_limit.to_i
174
+ if mod == 0
175
+ last = (total_result.to_i / result_limit.to_i) - 1
176
+ else
177
+ last = (total_result.to_i / result_limit.to_i).to_i
178
+ end
179
+
180
+ OpenStruct.new({:page => page, :last => last})
181
+ end
182
+
183
+ end
184
+ end
185
+ end
186
186
  end
@@ -1,121 +1,121 @@
1
- require 'xing_api'
2
-
3
- module HawatelSearchJobs::Api
4
- module Xing
5
- class << self
6
- include HawatelSearchJobs::Helpers::Base
7
-
8
- DEFAULT = {
9
- :keywords => '',
10
- :location => '',
11
- :company => ''
12
- }
13
-
14
- # @see https://github.com/xing/xing_api
15
- # Search jobs based on specified keywords
16
- #
17
- # @param args [Hash]
18
- # @option args :query [Hash] search criteria
19
- # - *:keywords* (String )
20
- # @option args :setting [Hash] @see https://dev.xing.com/docs/authentication
21
- # - *:consumer_key* (String) - consumer key, required for authentication
22
- # - *:consumer_secret* (String) - consumer secret, required for authentication
23
- # - *:oauth_token* (String) - outh toker, required for authentication
24
- # - *:oauth_token_secret* (String) - outh token secret, required for authentication
25
- #
26
- # @example
27
- # search(:settings => HawatelSearchJobs.xing,:query => {:keywords => 'ruby'})
28
- #
29
- # @return [Hash<OpenStruct>]
30
- def search(args)
31
- args[:query] = DEFAULT.merge(args[:query]) if args[:query]
32
- keywords = args[:query][:keywords]
33
- result = send_request({:keywords => keywords, :offset => 0, :settings => args[:settings]})
34
- if !result[:code]
35
- set_attributes({:result => result, :page => 0, :keywords => keywords})
36
- else
37
- OpenStruct.new({:code => 501, :msg => 'incorrect settings'})
38
- end
39
- end
40
-
41
- # Show next page of results
42
- #
43
- # @param args [Hash]
44
- # @option page [Integer] page numer (default 0)
45
- # @option query_key [String] keywords from last query
46
- #
47
- # @example
48
- # page({:query_key => result.key, :page => 2}
49
- #
50
- # @return [Hash<OpenStruct>]
51
- def page(args)
52
- args[:page] = 0 if args[:page].nil?
53
- result = XingApi::Job.search(args[:query_key], {:limit => 25, :offset => args[:page]*25})
54
- set_attributes({:result => result, :page => args[:page], :keywords => args[:query_key]})
55
- rescue XingApi::Error => e
56
- {:code => e.status_code, :msg => e.text}
57
- end
58
-
59
- private
60
- # Call Xing client request
61
- #
62
- # @param args [Hash]
63
- # @option settings [Hash] authentication attributes
64
- # @option keywords [String] keywords for query
65
- #
66
- # @return [Hash<OpenStruct>]
67
- def send_request(args)
68
- set_settings(args[:settings])
69
- XingApi::Job.search(args[:keywords], {:limit => 25, :offset => 0})
70
- rescue XingApi::Error => e
71
- {:code => e.status_code, :msg => e.text}
72
- end
73
-
74
- # Build final result - set required attributes and return openstruct object
75
- #
76
- def set_attributes(args)
77
- attributes = Hash.new
78
- attributes[:totalResults] = args[:result][:jobs][:total]
79
- attributes[:code] = '200'
80
- attributes[:msg] = "OK"
81
- attributes[:page] = args[:page]
82
- attributes[:last] = args[:result][:jobs][:total] / 25
83
- attributes[:key] = args[:keywords]
84
- attributes[:jobs] = parse_raw_data(args[:result])
85
- OpenStruct.new(attributes)
86
- end
87
-
88
- # Build jobs array with specified attributes
89
- #
90
- # @return [Array<OpenStruct>]
91
- def parse_raw_data(result)
92
- jobs = Array.new
93
- return jobs if result['jobs'].to_s.empty?
94
- result[:jobs][:items].each do |offer|
95
- job = Hash.new
96
- job[:jobtitle] = offer[:title]
97
- job[:location] = "#{offer[:location][:country]}, #{offer[:location][:city]}"
98
- job[:company] = offer[:company][:name]
99
- job[:date] = convert_date_to_format(offer[:published_at], '%d/%m/%y')
100
- job[:url] = offer[:links][:xing]
101
- job = convert_empty_to_nil(job)
102
- jobs << OpenStruct.new(job)
103
- end
104
- return jobs
105
- end
106
-
107
- # Set settings for XingApi client
108
- #
109
- # @param args [Hash]
110
- def set_settings(args)
111
- XingApi::Client.configure do |config|
112
- config.consumer_key = args[:consumer_key]
113
- config.consumer_secret = args[:consumer_secret]
114
- config.oauth_token = args[:oauth_token]
115
- config.oauth_token_secret = args[:oauth_token_secret]
116
- end
117
- end
118
-
119
- end
120
- end
121
- end
1
+ require 'xing_api'
2
+
3
+ module HawatelSearchJobs::Api
4
+ module Xing
5
+ class << self
6
+ include HawatelSearchJobs::Helpers::Base
7
+
8
+ DEFAULT = {
9
+ :keywords => '',
10
+ :location => '',
11
+ :company => ''
12
+ }
13
+
14
+ # @see https://github.com/xing/xing_api
15
+ # Search jobs based on specified keywords
16
+ #
17
+ # @param args [Hash]
18
+ # @option args :query [Hash] search criteria
19
+ # - *:keywords* (String )
20
+ # @option args :setting [Hash] @see https://dev.xing.com/docs/authentication
21
+ # - *:consumer_key* (String) - consumer key, required for authentication
22
+ # - *:consumer_secret* (String) - consumer secret, required for authentication
23
+ # - *:oauth_token* (String) - outh toker, required for authentication
24
+ # - *:oauth_token_secret* (String) - outh token secret, required for authentication
25
+ #
26
+ # @example
27
+ # search(:settings => HawatelSearchJobs.xing,:query => {:keywords => 'ruby'})
28
+ #
29
+ # @return [Hash<OpenStruct>]
30
+ def search(args)
31
+ args[:query] = DEFAULT.merge(args[:query]) if args[:query]
32
+ keywords = args[:query][:keywords]
33
+ result = send_request({:keywords => keywords, :offset => 0, :settings => args[:settings]})
34
+ if !result[:code]
35
+ set_attributes({:result => result, :page => 0, :keywords => keywords})
36
+ else
37
+ OpenStruct.new({:code => 501, :msg => 'incorrect settings'})
38
+ end
39
+ end
40
+
41
+ # Show next page of results
42
+ #
43
+ # @param args [Hash]
44
+ # @option page [Integer] page numer (default 0)
45
+ # @option query_key [String] keywords from last query
46
+ #
47
+ # @example
48
+ # page({:query_key => result.key, :page => 2}
49
+ #
50
+ # @return [Hash<OpenStruct>]
51
+ def page(args)
52
+ args[:page] = 0 if args[:page].nil?
53
+ result = XingApi::Job.search(args[:query_key], {:limit => 25, :offset => args[:page]*25})
54
+ set_attributes({:result => result, :page => args[:page], :keywords => args[:query_key]})
55
+ rescue XingApi::Error => e
56
+ {:code => e.status_code, :msg => e.text}
57
+ end
58
+
59
+ private
60
+ # Call Xing client request
61
+ #
62
+ # @param args [Hash]
63
+ # @option settings [Hash] authentication attributes
64
+ # @option keywords [String] keywords for query
65
+ #
66
+ # @return [Hash<OpenStruct>]
67
+ def send_request(args)
68
+ set_settings(args[:settings])
69
+ XingApi::Job.search(args[:keywords], {:limit => 25, :offset => 0})
70
+ rescue XingApi::Error => e
71
+ {:code => e.status_code, :msg => e.text}
72
+ end
73
+
74
+ # Build final result - set required attributes and return openstruct object
75
+ #
76
+ def set_attributes(args)
77
+ attributes = Hash.new
78
+ attributes[:totalResults] = args[:result][:jobs][:total]
79
+ attributes[:code] = '200'
80
+ attributes[:msg] = "OK"
81
+ attributes[:page] = args[:page]
82
+ attributes[:last] = args[:result][:jobs][:total] / 25
83
+ attributes[:key] = args[:keywords]
84
+ attributes[:jobs] = parse_raw_data(args[:result])
85
+ OpenStruct.new(attributes)
86
+ end
87
+
88
+ # Build jobs array with specified attributes
89
+ #
90
+ # @return [Array<OpenStruct>]
91
+ def parse_raw_data(result)
92
+ jobs = Array.new
93
+ return jobs if result['jobs'].to_s.empty?
94
+ result[:jobs][:items].each do |offer|
95
+ job = Hash.new
96
+ job[:jobtitle] = offer[:title]
97
+ job[:location] = "#{offer[:location][:country]}, #{offer[:location][:city]}"
98
+ job[:company] = offer[:company][:name]
99
+ job[:date] = convert_date_to_format(offer[:published_at], '%d/%m/%y')
100
+ job[:url] = offer[:links][:xing]
101
+ job = convert_empty_to_nil(job)
102
+ jobs << OpenStruct.new(job)
103
+ end
104
+ return jobs
105
+ end
106
+
107
+ # Set settings for XingApi client
108
+ #
109
+ # @param args [Hash]
110
+ def set_settings(args)
111
+ XingApi::Client.configure do |config|
112
+ config.consumer_key = args[:consumer_key]
113
+ config.consumer_secret = args[:consumer_secret]
114
+ config.oauth_token = args[:oauth_token]
115
+ config.oauth_token_secret = args[:oauth_token_secret]
116
+ end
117
+ end
118
+
119
+ end
120
+ end
121
+ end