class NeverBounce::API::Request::Base

@abstract @see API::Feature::BasicInitialize

Attributes

api_key[RW]

User's API key. @return [String]

api_url[W]
api_version[W]
headers[W]
user_agent[W]

Public Class Methods

http_method() click to toggle source

:get, :post or whatever. @abstract @return [Symbol] @return [String]

# 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
path() click to toggle source

Request path on server, e.g. "jobs/parse". @abstract @return [String]

# File lib/never_bounce/api/request/base.rb, line 49
def self.path
  raise NotImplementedError, "Redefine `self.path` in your class: #{self}"
end
response_klass() click to toggle source

@abstract @return [Class]

# 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

Public Instance Methods

api_url() click to toggle source

Custom API URL. Default is https://api.neverbounce.com. @return [String]

# File lib/never_bounce/api/request/base.rb, line 19
def api_url
  @api_url ||= "https://api.neverbounce.com"
end
api_version() click to toggle source

Custom API URL. Default is https://api.neverbounce.com. @return [String]

# File lib/never_bounce/api/request/base.rb, line 25
def api_version
  @api_version ||= "v4.2"
end
headers() click to toggle source

@!attribute headers @return [Array]

# File lib/never_bounce/api/request/base.rb, line 31
def headers
  {
    "Content-Type" => "application/json",
    "User-Agent" => user_agent,
  }
end
to_curl() click to toggle source

Build arguments for cURL OS command. @return [Array]

# 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_h() click to toggle source

Build a Hash representation of request data. @abstract @return [Hash]

# 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_httparty() click to toggle source

Build argumentsfor Httparty invocation. @return [Array]

# 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
user_agent() click to toggle source

@!attribute user_agent @return [String]

# 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