Class: Berrycrawl::Jobs::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/Berrycrawl/jobs/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ void



9
10
11
# File 'lib/Berrycrawl/jobs/client.rb', line 9

def initialize(client:)
  @client = client
end

Instance Method Details

#cancel(request_options: {}, **params) ⇒ Berrycrawl::Types::CancelJobResponse

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (Hash)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :id (String)

Returns:



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/Berrycrawl/jobs/client.rb', line 104

def cancel(request_options: {}, **params)
  params = Berrycrawl::Internal::Types::Utils.normalize_keys(params)
  request = Berrycrawl::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "DELETE",
    path: "jobs/#{URI.encode_uri_component(params[:id].to_s)}",
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Berrycrawl::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Berrycrawl::Types::CancelJobResponse.load(response.body)
  else
    error_class = Berrycrawl::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#get(request_options: {}, **params) ⇒ Berrycrawl::Types::JobResponse

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (Hash)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :id (String)
  • :page (Integer, nil)
  • :limit (Integer, nil)

Returns:



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/Berrycrawl/jobs/client.rb', line 67

def get(request_options: {}, **params)
  params = Berrycrawl::Internal::Types::Utils.normalize_keys(params)
  query_params = {}
  query_params["page"] = params[:page] if params.key?(:page)
  query_params["limit"] = params[:limit] if params.key?(:limit)

  request = Berrycrawl::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "GET",
    path: "jobs/#{URI.encode_uri_component(params[:id].to_s)}",
    query: query_params,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Berrycrawl::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Berrycrawl::Types::JobResponse.load(response.body)
  else
    error_class = Berrycrawl::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#list(request_options: {}, **params) ⇒ Berrycrawl::Types::ListJobsResponse

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (Hash)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

Returns:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/Berrycrawl/jobs/client.rb', line 26

def list(request_options: {}, **params)
  params = Berrycrawl::Internal::Types::Utils.normalize_keys(params)
  query_params = {}
  query_params["type"] = params[:type] if params.key?(:type)
  query_params["status"] = params[:status] if params.key?(:status)
  query_params["page"] = params[:page] if params.key?(:page)
  query_params["limit"] = params[:limit] if params.key?(:limit)

  request = Berrycrawl::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "GET",
    path: "jobs",
    query: query_params,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Berrycrawl::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Berrycrawl::Types::ListJobsResponse.load(response.body)
  else
    error_class = Berrycrawl::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end