Giter Site home page Giter Site logo

asynchrony's Introduction

asynchrony

GETs data from a url, retrying if an error code is found

Like most poller gems, polls a given URL to see if data is ready. The difference? If data is ready, it returns that data. Assumes that the website in question will return an error code if there is no data.

For example: Assume there is a URL, www.my_awesome_example.com/random_data, that either returns a 404 error or some actual JSON.

Asynchrony.get("www.my_awesome_example.com/random_data") will re-poll if a 404 error is seen, and return the JSON data it finds

The number of retries is finite, and if the GET is not successful it will raise an Asynchrony::HTTPError.

Use

When all goes well:

important_json = Asynchrony.get("www.my_awesome_example.com/random_data")
render important_json

When no data is gotten:

important_json = Asynchrony.get("www.my_awesome_example.com/random_data")
#==> Asynchrony::HTTPError: 404 error receiving data from 'www.my_awesome_example.com/random_data'
<code: 404. successful: false. body: I suck.>   # the actual result that was received

If you intend to get data more than once (yes, this is a contrived example):

def same_response?
  endpoint = Asynchrony.watch("www.my_awesome_example.com/random_data")
  send_thing_to_awesome_example
  response1 = endpoint.result   # or endpoint.get

  send_thing_to_awesome_example
  response2 = endpoint.result

  response1 == response2

Configuration

Asynchrony supports basic configuration. The number of retries and the amount of time to wait between retries can both be modified.

By default:

  • there are 10 retries
  • it will wait at least 1 second between retries, increased exponentially
  • it will wait no more than 10 seconds between retries

These can be changed if you actually get an object, e.g., use the Asynchrony.watch method call:

endpoint = Asynchrony.watch("www.my_awesome_example.com/random_data")
endpoint.retires = 5
endpoint.min_wait_time = 0.5   # wait at least half a second between retries
endpoint.max_wait_time = 60    # wait as long as a minute between retries

asynchrony's People

Contributors

ksearfos avatar

Watchers

James Cloos avatar Ryan Wang avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.