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.

Returns:

  • (String)


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

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

#api_versionString

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

Returns:

  • (String)


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

def api_version
  @api_version ||= "v4.2"
end

#headersArray

Returns:

  • (Array)


31
32
33
34
35
36
# File 'lib/never_bounce/api/request/base.rb', line 31

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

#user_agentString

Returns:

  • (String)


101
102
103
104
105
106
# File 'lib/never_bounce/api/request/base.rb', line 101

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)


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

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)


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

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

.response_klassClass

This method is abstract.

Returns:

  • (Class)

Raises:

  • (NotImplementedError)


55
56
57
# File 'lib/never_bounce/api/request/base.rb', line 55

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)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/never_bounce/api/request/base.rb', line 61

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}/#{api_version}/#{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)


82
83
84
# File 'lib/never_bounce/api/request/base.rb', line 82

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

#to_httpartyArray

Build argumentsfor Httparty invocation.

Returns:

  • (Array)


88
89
90
91
92
93
94
95
96
97
# File 'lib/never_bounce/api/request/base.rb', line 88

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