Sha256: 5794a6a7abe0c4622ddade23c57b5460670dbd3f8b13b2d51b81c2aea38a6cd9

Contents?: true

Size: 1.84 KB

Versions: 2

Compression:

Stored size: 1.84 KB

Contents

require "cgi"
require "httparty"

module LinkshareAPI
  # For implementation details please visit
  # https://rakutenlinkshare.zendesk.com/hc/en-us/articles/201343135-Automated-LinkGenerator-Guidelines
  class LinkGenerator
    include HTTParty

    attr_reader :api_base_url, :token, :api_timeout

    def initialize
      @token        = LinkshareAPI.token
      @api_base_url = LinkshareAPI::WEB_SERVICE_URIS[:link_generator]
      @api_timeout  = LinkshareAPI.api_timeout

      if @token.nil?
        raise AuthenticationError.new(
          "No token. Set your token by using 'LinkshareAPI.token = <TOKEN>'. " +
          "You can retrieve your token from LinkhShare's Web Services page under the Links tab. " +
          "See https://rakutenlinkshare.zendesk.com/hc/en-us/articles/200992487-What-is-a-Web-Services-Token-Feed-Token- for details."
        )
      end

      self.class.default_timeout @api_timeout
    end

    def build(mid, murl)
      raise ArgumentError, "mid must be a Fixnum, got #{mid.class} instead" unless mid.is_a?(Fixnum)

      query_string = "token=#{CGI.escape(token)}"
      query_string << "&mid=#{mid}"
      # murl must not be encoded (RFC ftw)
      query_string << "&murl=#{murl}"
      api_request_url = "#{api_base_url}?#{query_string}"
      begin
        response = self.class.get(api_request_url)
      rescue Timeout::Error, Net::OpenTimeout
        raise ConnectionError.new("Timeout error (#{api_timeout}s)")
      end

      if response.code != 200
        raise Error.new("Unexpected response: #{response.message}", response.code)
      end

      # If the body content looks like an URL, then would be
      # safe to assume that the request was processed correctly
      unless response.body.start_with? "http://", "https://"
        raise InvalidRequestError.new(response.body)
      end
      response.body
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
linkshare_api-0.3.2 lib/linkshare_api/link_generator.rb
linkshare_api-0.3.1 lib/linkshare_api/link_generator.rb