Class: NeverBounce::API::Request::Base Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/never_bounce/api/request/base.rb

Overview

This class is abstract.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_keyString

User’s API key.

Returns:

  • (String)


14
15
16
# File 'lib/never_bounce/api/request/base.rb', line 14

def api_key
  @api_key
end

#api_urlString

Custom API URL. Default is https://api.neverbounce.com/v4.

Returns:

  • (String)


19
20
21
# File 'lib/never_bounce/api/request/base.rb', line 19

def api_url
  @api_url ||= "https://api.neverbounce.com/v4"
end

#headersArray

Returns:

  • (Array)


25
26
27
28
29
30
# File 'lib/never_bounce/api/request/base.rb', line 25

def headers
  {
    "Content-Type" => "application/json",
    "User-Agent" => user_agent,
  }
end

#user_agentString

Returns:

  • (String)


95
96
97
98
99
100
# File 'lib/never_bounce/api/request/base.rb', line 95

def user_agent
  @user_agent ||= [
    "NeverBounceApi-Ruby/#{API::VERSION} (#{RUBY_PLATFORM})",
    "Ruby/#{RUBY_VERSION} (p #{RUBY_PATCHLEVEL}; rev #{RUBY_REVISION})",
  ].join(" ")
end

Class Method Details

.http_methodSymbol, String

This method is abstract.

:get, :post or whatever.

Returns:

  • (Symbol)
  • (String)

Raises:

  • (NotImplementedError)


36
37
38
# File 'lib/never_bounce/api/request/base.rb', line 36

def self.http_method
  raise NotImplementedError, "Redefine `self.http_method` in your class: #{self}"
end

.pathString

This method is abstract.

Request path on server, e.g. "jobs/parse".

Returns:

  • (String)

Raises:

  • (NotImplementedError)


43
44
45
# File 'lib/never_bounce/api/request/base.rb', line 43

def self.path
  raise NotImplementedError, "Redefine `self.path` in your class: #{self}"
end

.response_klassClass

This method is abstract.

Returns:

  • (Class)

Raises:

  • (NotImplementedError)


49
50
51
# File 'lib/never_bounce/api/request/base.rb', line 49

def self.response_klass
  raise NotImplementedError, "Redefine `self.response_klass` in your class: #{self}"
end

Instance Method Details

#to_curlArray

Build arguments for cURL OS command.

Returns:

  • (Array)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/never_bounce/api/request/base.rb', line 55

def to_curl
  # NOTE: I consider we should use long options to avoid ambiguity of ones like `-u` etc.
  @curl ||= begin
    ar = [
      "--request", self.class.http_method.to_s.upcase,
      "--url", "#{api_url}/#{self.class.path}",
    ]

    ar += headers.reject { |k,| k == "User-Agent" }.flat_map do |k, v|
      ["--header", "#{k}: #{v}"]
    end

    ar += ["--data-binary", to_h.to_json]

    ar
  end
end

#to_hHash

This method is abstract.

Build a Hash representation of request data.

Returns:

  • (Hash)

Raises:

  • (NotImplementedError)


76
77
78
# File 'lib/never_bounce/api/request/base.rb', line 76

def to_h
  raise NotImplementedError, "Redefine `to_h` in your class: #{self.class}"
end

#to_httpartyArray

Build argumentsfor Httparty invocation.

Returns:

  • (Array)


82
83
84
85
86
87
88
89
90
91
# File 'lib/never_bounce/api/request/base.rb', line 82

def to_httparty
  [
    self.class.http_method,   # E.g. `:get`.
    "#{api_url}/#{self.class.path}",
    {
      body: to_h.to_json,
      headers: headers,
    }
  ]
end