Sha256: 90ac032f6e85f5ac9b2fb8972649706c831b7e59b032e741f415f14c35899f7e

Contents?: true

Size: 1.36 KB

Versions: 4

Compression:

Stored size: 1.36 KB

Contents

require "dialers"

module Twitter
  class ApiCaller < Dialers::Caller
    BASE_URL = "https://api.twitter.com/1.1/"
    CONSUMER_KEY = ENV["TWITTER_CONSUMER_KEY"]
    CONSUMER_SECRET = ENV["TWITTER_CONSUMER_SECRET"]
    TOKEN = ENV["TWITTER_TOKEN"]
    TOKEN_SECRET = ENV["TWITTER_TOKEN_SECRET"]

    setup_api(url: BASE_URL, ssl: { verify: true }) do |faraday|
      faraday.request :json
      faraday.request :oauth,
        consumer_key: CONSUMER_KEY,
        consumer_secret: CONSUMER_SECRET,
        token: TOKEN,
        token_secret: TOKEN_SECRET
      faraday.request :request_headers, accept: "*/*"
      faraday.response :json
      faraday.adapter :patron
    end

    short_circuits.add(
      if: -> (response) { Dialers::Status.new(response.status).server_error? },
      do: -> (response) { fail Dialers::ServerError.new(response) }
    )

    short_circuits.add(
      if: -> (response) { Dialers::Status.new(response.status).is?(400) },
      do: -> (response) { fail Dialers::ResponseError.new(response) }
    )

    short_circuits.add(
      if: -> (response) { Dialers::Status.new(response.status).is?(401) },
      do: -> (response) { fail Dialers::UnauthorizedError.new(response) }
    )

    short_circuits.add(
      if: -> (response) { Dialers::Status.new(response.status).is?(404) },
      do: -> (response) { fail Dialers::NotFoundError.new(response) }
    )
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dialers-0.2.2 examples/twitter/api_caller.rb
dialers-0.1.2 examples/twitter/api_caller.rb
dialers-0.1.1 examples/twitter/api_caller.rb
dialers-0.1.0 examples/twitter/api_caller.rb