spear-cb-api 0.0.1 → 0.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c347867ceceb0398580e288d134eb2aa2b3e9c4a
4
- data.tar.gz: 7d488d3e2419fb71060740d8a5f8b6cf25e94942
3
+ metadata.gz: 29b5f37465df0d5077509166f6c6ddf4a213199c
4
+ data.tar.gz: c360be2ffff026916b5db1aee3152b5a0973d9e6
5
5
  SHA512:
6
- metadata.gz: 5350b889086c8cbadea37283ac9507dfbc6c43e1d965246a53e4ea0bb03c08f51447c296b22556161a7227a2ba1cf112fdc0cb1534a0c489908c8cf102d452d4
7
- data.tar.gz: 978e149c92640f48eb988ff4a09f8ab072aa6f920c17bc5f8fe9c11faef7314c0861340315db137a07e5f8b79bad0c63aec645c49217ab77f701a8352715edbc
6
+ metadata.gz: 3a006f99f34b2a912857f5f574de2028f673ffba67662333aff12e07685843d1c1303dd5980392690d98af45984c0a90235f9f0ce92d5e2c27399eaba7e9dbfa
7
+ data.tar.gz: 8469319fd0519780ac29a35347c0696271516a8e1ae2ae2d73c5ae6b87d94c348aeca4c97cc6cecde7c2c13c5dd297a10fad8637bc8e12831246bc9ba777069c
data/lib/spear.rb CHANGED
@@ -1,17 +1,70 @@
1
1
  # encoding: utf-8
2
2
 
3
- require "spear/version"
3
+ require 'active_support'
4
+ require 'active_support/core_ext'
5
+
6
+ require 'sucker_punch'
7
+ require 'httparty'
4
8
 
5
9
  module Spear
6
- autoload :AsyncJob, 'spear/async_job'
7
- autoload :Client, 'spear/client'
10
+ autoload :Configuration, 'spear/configuration'
11
+ autoload :Resources, 'spear/resources'
8
12
  autoload :Request, 'spear/request'
9
13
 
10
- # exception
11
- autoload :InvalidClientOptionsException, 'spear/exceptions'
14
+ autoload :ParametersRequired, 'spear/exceptions'
15
+ autoload :ParametersNotValid, 'spear/exceptions'
16
+ autoload :NetworkError, 'spear/exceptions'
17
+ autoload :ObjectTypeError, 'spear/exceptions'
18
+
19
+ module Structure
20
+ autoload :Base, 'spear/structure/base'
21
+
22
+ module User
23
+ autoload :CheckExisting, 'spear/structure/user/check_existing'
24
+ autoload :Create, 'spear/structure/user/create'
25
+ autoload :Retrieve, 'spear/structure/user/retrieve'
26
+ end
27
+
28
+ module Resume
29
+ autoload :EmbededClass, 'spear/structure/resume/embeded_class'
30
+ autoload :Create, 'spear/structure/resume/create'
31
+ autoload :Edit, 'spear/structure/resume/edit'
32
+ autoload :Ownall, 'spear/structure/resume/ownall'
33
+ autoload :Parse, 'spear/structure/resume/parse'
34
+ autoload :Retrieve, 'spear/structure/resume/retrieve'
35
+ autoload :Upload, 'spear/structure/resume/upload'
36
+ end
37
+
38
+ module Application
39
+ end
40
+
41
+ module Job
42
+ end
43
+
44
+ module TalentNetwork
45
+ end
46
+ end
47
+
48
+ module Plugins
49
+ autoload :SaveApis, 'spear/plugins/save_apis'
50
+ autoload :Models, 'spear/plugins/models'
51
+
52
+ module Model
53
+ autoload :User, 'spear/plugins/model/user'
54
+ autoload :Resume, 'spear/plugins/model/resume'
55
+ autoload :Application, 'spear/plugins/model/application'
56
+ autoload :Job, 'spear/plugins/model/job'
57
+ autoload :TalentNetwork, 'spear/plugins/model/talent_network'
58
+ end
59
+ end
12
60
 
13
61
  module Resource
14
62
  autoload :User, 'spear/resource/user'
15
63
  autoload :Resume, 'spear/resource/resume'
64
+ autoload :Application, 'spear/resource/application'
65
+ autoload :Job, 'spear/resource/job'
66
+ autoload :TalentNetwork, 'spear/resource/talent_network'
16
67
  end
68
+
69
+ extend Configuration
17
70
  end
@@ -0,0 +1,62 @@
1
+ module Spear
2
+ module Configuration
3
+ include Resources
4
+
5
+ @@options = {}
6
+
7
+ def test?
8
+ @@options[:use_test] || false
9
+ end
10
+
11
+ # use https or http
12
+ def ssh?
13
+ @@options[:use_ssh] || true
14
+ end
15
+
16
+ def hostname
17
+ @@options[:hostname] || 'api.careerbuilder.com'
18
+ end
19
+
20
+ def port
21
+ @@options[:port] || 443
22
+ end
23
+
24
+ def version
25
+ @@options[:version] || "v2"
26
+ end
27
+
28
+ def dev_key
29
+ @@options[:dev_key]
30
+ end
31
+
32
+ # need save api info
33
+ def save_api?
34
+ @@options[:saving_api] || false
35
+ end
36
+
37
+ def use_model?
38
+ @@options[:using_model] || false
39
+ end
40
+
41
+ def project
42
+ @@options[:project]
43
+ end
44
+
45
+ def config(options)
46
+ raise Spear::ParametersRequired.new('DeveloperKey') if options[:dev_key].nil?
47
+ raise Spear::ParametersRequired.new('ProjectName') if options[:project].nil?
48
+
49
+ @@options = options
50
+
51
+ # add plugins to request
52
+ include_plugins
53
+ end
54
+
55
+ private
56
+ def include_plugins
57
+ Request.include Plugins::SaveApis if save_api?
58
+ Spear.extend Plugins::Models if use_model?
59
+ end
60
+
61
+ end
62
+ end
@@ -1,3 +1,21 @@
1
1
  module Spear
2
- class InvalidClientOptionsException < Exception; end
2
+ class Error < StandardError; end
3
+
4
+ class ParametersRequired < Error
5
+ attr_reader :parameter
6
+
7
+ def initialize(parameter)
8
+ if parameter.kind_of?(Array)
9
+ @parameter = parameter.join(', ')
10
+ else
11
+ @parameter = parameter
12
+ end
13
+
14
+ super("#{@parameter} is required.")
15
+ end
16
+ end
17
+
18
+ class ParametersNotValid < Error; end
19
+ class NetworkError < Error; end
20
+ class ObjectTypeError < Error; end
3
21
  end
@@ -0,0 +1,8 @@
1
+ module Spear
2
+ module Plugins
3
+ module Model
4
+ module Application
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module Spear
2
+ module Plugins
3
+ module Model
4
+ module Job
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,38 @@
1
+ module Spear
2
+ module Plugins
3
+ module Model
4
+ module Resume
5
+ def parse_file(file)
6
+ response = super(file)
7
+ response = Structure::Resume::Parse.new(response)
8
+ end
9
+
10
+ def create_resume(data={})
11
+ response = super(data)
12
+ response = Structure::Resume::Create.new(response)
13
+ end
14
+
15
+ def edit_resume(data={})
16
+ response = super(data)
17
+ response = Structure::Resume::Edit.new(response)
18
+ end
19
+
20
+ def upload_file(file, resume_external_id, user_external_id)
21
+ response = super(file, resume_external_id, user_external_id)
22
+ response = Structure::Resume::Upload.new(response)
23
+ end
24
+
25
+ def own_all(user_external_id, host_site)
26
+ response = super(user_external_id, host_site)
27
+ response = Structure::Resume::Ownall.new(response)
28
+ end
29
+
30
+ def retrieve_resume(resume_external_id, user_external_id)
31
+ response = super(user_external_id, host_site)
32
+ response = Structure::Resume::Retrieve.new(response)
33
+ end
34
+
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,8 @@
1
+ module Spear
2
+ module Plugins
3
+ module Model
4
+ module TalentNetwork
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,23 @@
1
+ module Spear
2
+ module Plugins
3
+ module Model
4
+ module User
5
+ def check_existing(email, password='')
6
+ response = super(email, password)
7
+ Structure::User::CheckExisting.new(response)
8
+ end
9
+
10
+ def create_user(data={})
11
+ response = super(data)
12
+ Structure::User::Create.new(response)
13
+ end
14
+
15
+ def retrieve_user(user_external_id, password)
16
+ response = super(user_external_id, password)
17
+ Structure::User::Retrieve.new(response)
18
+ end
19
+
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,11 @@
1
+ module Spear
2
+ module Plugins
3
+ module Models
4
+ include Model::User
5
+ include Model::Resume
6
+ include Model::Application
7
+ include Model::Job
8
+ include Model::TalentNetwork
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,64 @@
1
+ # encoding: utf-8
2
+
3
+ module Spear
4
+ module Plugins
5
+ module SaveApis
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ class_attribute :api_response, :api_started_at, :save_api_instance
10
+
11
+ before_execute :record_started_time
12
+ after_execute :save_infos
13
+ end
14
+
15
+ def record_started_time
16
+ self.api_started_at = Time.now
17
+ end
18
+
19
+ def save_infos
20
+ self.save_api_instance ||= AsyncSaveApi.new
21
+
22
+ params = {
23
+ project: Spear.project,
24
+ request: nil,
25
+ response: self.api_response.body,
26
+ method: self.api_response.request.http_method.to_s.split('::').last,
27
+ url: self.api_response.request.path.to_s,
28
+ duration: ((Time.now - self.api_started_at).to_f * 1000.0).to_i
29
+ }
30
+
31
+ params[:request] = self.api_response.request.options[:body]
32
+ if params[:request].kind_of?(String)
33
+ # parse file api
34
+ params[:request].gsub!(/<FileBytes>(.*?)<\/FileBytes>/m, '<FileBytes>...</FileBytes>')
35
+ elsif params[:request].kind_of?(Hash)
36
+ # upload file api
37
+ params[:request][:FileBytes] = '...'
38
+ end
39
+
40
+ self.save_api_instance.async.perform(params)
41
+ end
42
+
43
+ class AsyncSaveApi
44
+ include SuckerPunch::Job
45
+
46
+ def perform(options={})
47
+ HTTParty.post(
48
+ %q{http://ciws.hilotus.com/api/v1/api-info},
49
+ :body => {
50
+ :project => options[:project],
51
+ :url => options[:url],
52
+ :method => options[:method],
53
+ :request => options[:request],
54
+ :response => options[:response],
55
+ :duration => options[:duration]
56
+ }.to_json,
57
+ :options => {:headers => {'Content-Type' => 'application/json'}}
58
+ ) rescue options
59
+ end
60
+ end
61
+
62
+ end
63
+ end
64
+ end
data/lib/spear/request.rb CHANGED
@@ -1,75 +1,87 @@
1
+ require 'active_model'
2
+ require 'active_model/callbacks'
3
+
1
4
  module Spear
2
5
  class Request
3
- attr_reader :client, :resource
6
+ extend ActiveModel::Callbacks
7
+ define_model_callbacks :execute, :only => [:before, :after]
8
+
9
+ attr_reader :resource
4
10
 
5
- def initialize(options={})
6
- @client = options[:client]
7
- @resource = resource
8
- @async_job = Spear::AsyncJob.new
11
+ # params: {:body => {}, :query => {}, :api_options => {}}
12
+ # exp: api_options: {:path => "/v1"}
13
+ def execute(method, endpoint, params={})
14
+ run_callbacks(:execute) {
15
+ response = exec(method, endpoint, params)
16
+ begin
17
+ self.api_response = response
18
+ rescue NoMethodError => e # if we don't use save api info plugin, it'll throw NoMethodError.
19
+ response
20
+ end
21
+ }
9
22
  end
10
23
 
11
- # TODO: custom reqeust headers support.
12
- # payload: {:body => {}, :query => {}}
13
- def execute(http_method, endpoint, payload={})
14
- start_time = Time.now;
15
- url = @resource + endpoint;
24
+ private
25
+ def exec(method, endpoint, params={})
26
+ query = params[:query] || {}
27
+ body = params[:body] || {}
28
+ api_options = params[:api_options] || {}
29
+ url = generate_resource(api_options) + endpoint
16
30
 
17
- case http_method
18
- when :get
19
- req = splice(payload)
20
- resp = HTTParty.get(url, req)
21
- when :post
22
- req = {:body => to_xml(payload)}
23
- resp = HTTParty.post(url, req)
24
- when :upload_file
25
- req = splice(payload)
26
- req[:body] = "file base64 content..."
27
- resp = HTTParty.post(url, splice(payload))
28
- when :parse_file
29
- req = {:body => payload}
30
- req[:body][:FileBytes] = nil
31
- resp = HTTParty.post(url, :body => to_xml(payload))
32
- else
33
- resp = {"Errors" => {"Error" => "%q{#{http_method} http method is not support.}"}}
31
+ begin
32
+ case method
33
+ when :get
34
+ HTTParty.get(url, query: splice_query(query))
35
+ when :post
36
+ HTTParty.post(url, body: splice_body(body), query: query)
37
+ when :upload_file
38
+ HTTParty.post(url, body: body, query: splice_query(query))
39
+ when :parse_file
40
+ HTTParty.post(url, body: splice_body(body))
41
+ when :apply
42
+ HTTParty.post(url, body: to_xml(body), query: splice_query(query))
43
+ else
44
+ raise Spear::ParametersNotValid.new("#{method} is not support.")
45
+ end
46
+ rescue SocketError => e # if the network is disconnected, it'll throw SocketError.
47
+ raise Spear::NetworkError.new(e.message)
48
+ end
34
49
  end
35
50
 
36
- if @client.save_api?
37
- options = {
38
- project: @client.project,
39
- request: req,
40
- response: resp.to_h,
41
- method: http_method.to_s,
42
- url: url,
43
- duration: ((Time.now - start_time).to_f * 1000.0).to_i
44
- }
45
- @async_job.async.perform(options)
46
- end
51
+ # generate cb api host and version
52
+ def generate_resource(api_options)
53
+ uri = URI("")
54
+ uri.scheme = Spear.ssh? ? 'https' : 'http'
55
+ uri.host = Spear.hostname
56
+ uri.port = Spear.port
47
57
 
48
- resp
49
- end
58
+ # Some api endpoint have no version and fixed pattern.
59
+ if api_options[:path].nil?
60
+ uri.path = "/#{Spear.version}"
61
+ else
62
+ uri.path = "/#{api_options[:path]}"
63
+ end
50
64
 
51
- private
52
- def resource
53
- uri = URI("")
54
- uri.scheme = @client.ssh? ? 'https' : 'http'
55
- uri.host = @client.hostname
56
- uri.port = @client.port
57
- uri.path = "/#{@client.version}"
58
65
  uri.to_s
59
66
  end
60
67
 
61
- # parse payload to xml format
62
- def to_xml(payload)
63
- payload[:DeveloperKey] = @client.dev_key
64
- payload[:Test] = @client.test?
65
- payload = payload.to_xml(root: 'Request', skip_instruct: true, skip_types: true)
68
+ # splice request body
69
+ def splice_body(body, xml=true)
70
+ body[:DeveloperKey] = Spear.dev_key
71
+ body[:Test] = Spear.test?
72
+ xml ? to_xml(body) : body
73
+ end
74
+
75
+ # parse body from hash to xml format
76
+ def to_xml(body)
77
+ body.to_xml(root: 'Request', skip_instruct: true, skip_types: true)
66
78
  end
67
79
 
68
- # splice payload {:body => {}, :query => {}} with DeveloperKey and Test
69
- def splice(payload)
70
- payload[:query][:DeveloperKey] = @client.dev_key
71
- payload[:query][:Test] = @client.test?
72
- payload
80
+ # splice query(url params) {:query => {}} with DeveloperKey and Test
81
+ def splice_query(query)
82
+ query[:DeveloperKey] = Spear.dev_key
83
+ query[:Test] = Spear.test?
84
+ query
73
85
  end
74
86
 
75
87
  end