Giter Club home page Giter Club logo

io-client-ruby's Introduction

Build Status

adafruit-io

A Ruby client for use with with io.adafruit.com.

Note, this documentation covers the gem supporting V2 of the API, which is currently under active development and may be missing some features. It also breaks support for code that used version <= 1.0.4 of this library.

Older releases are available at these links:

This is a near complete rewrite and strip-down of the library intended to support V2 of the Adafruit IO API with less code, maintenance, and stress.

Why rewrite? This lets us the replace the existing, custom ActiveRecord-based interface with a flat, stateless API client returning plain hashes based on the JSON returned from API. Instead of writing a bunch of Ruby to make it feel like we're in a Rails app, we're just providing hooks into the API and a thin wrapper around Faraday.

The API is not very complex, code that uses it shouldn't be either.

Roadmap

It is our goal to eventually support all API V2 methods, but that will happen in stages.

  • Feeds 2.0.0.beta.1
  • Data 2.0.0.beta.1
  • Groups 2.0.0.beta.1
  • MQTT 2.0.0.beta.3
  • Tokens 2.0.0.beta.4
  • Blocks 2.0.0.beta.4
  • Dashboards 2.0.0.beta.4
  • Activities 2.0.0.beta.5
  • Permissions 2.0.0.beta.5
  • Triggers 2.0.0.beta.6

Still needing complete tests:

  • MQTT

Installation

Add this line to your application's Gemfile:

gem 'adafruit-io'

And then execute:

$ bundle

Or install it yourself as:

$ gem install adafruit-io

Basic Usage

Each time you use the library, you'll have to pass your Adafruit IO Key to the client.

require 'adafruit/io'

# create an instance
aio = Adafruit::IO::Client.new key: 'KEY'

Since every API request requires a username, you can also pass a username to the client initializer to use it for every request.

require 'adafruit/io'

# create an instance
aio = Adafruit::IO::Client.new key: 'KEY', username: 'USERNAME'

Environment Variables

Whenever possible, we recommend you keep your Adafruit IO API credentials out of your application code by using environment variables. All the examples

Others have written about using environment variables in Ruby, so we're not going to go into detail. We recommend the dotenv gem if you're building a Ruby project.

API Response Values

All return values are plain Ruby hashes based on the JSON response returned by the API. Most basic requests should get back a Hash with a key field. The key can be used in subsequent requests. API requests that return a list of objects will return a simple array of hashes. Feeds, Groups, and Dashboards all rely on the key value, other endpoints (Blocks, Permissions, Tokens, Triggers) use id.

You can find the current API documentation at https://io.adafruit.com/api/docs/. This library implements v2 of the Adafruit IO API.

API Error Responses

As of v2.0.0, this library raises an Adafruit::IO::RequestError on any non HTTP 200 status responses. Generally, this means your code should wrap API calls in begin...rescue...end blocks.

require 'adafruit/io'

api_key = ENV['IO_KEY']
username = ENV['IO_USER']

api = Adafruit::IO::Client.new key: api_key, username: username

Example

Here's an example of creating, adding data to, and deleting a feed.

require 'adafruit/io'

api_key = ENV['IO_KEY']
username = ENV['IO_USER']

api = Adafruit::IO::Client.new key: api_key, username: username

# create a feed
puts "create"
garbage = api.create_feed(name: "Garbage 123")

# add data
puts "add data"
api.send_data garbage, 'something'
api.send_data garbage, 'goes here'

# load data
puts "load data"
data = api.data(garbage)
puts "#{data.size} points: #{ data.map {|d| d['value']}.join(', ') }"

# get details
puts "read"
puts JSON.pretty_generate(api.feed_details(garbage))

# delete feed
puts "delete"
api.delete_feed(garbage)

# try reading
puts "read?"
# ... get nothing
puts api.feed(garbage['key']).inspect

This code and more is available in the examples/ directory.

License

Copyright (c) 2018 Adafruit Industries. Licensed under the MIT license.

Adafruit invests time and resources providing this open source code. Please support Adafruit and open-source hardware by purchasing products from Adafruit.

Contributing

  1. Fork it ( http://github.com/adafruit/io-client-ruby/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Write tests, write code, and run the tests (bundle exec rspec)
  4. Commit your changes (git commit -am 'Add some feature')
  5. Push to the branch (git push origin my-new-feature)
  6. Create a new Pull Request

If you'd like to contribute and don't know where to start, reach out on the Adafruit IO forum or in the adafruit-io channel on our Discord server.

io-client-ruby's People

Contributors

abachman avatar jwcooper avatar nikitaavvakumov avatar rkam avatar toddtreece avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

io-client-ruby's Issues

Establishing connection to api commented out

Please see line #57 in io-client-ruby/lib/adafruit/io/client.rb

This very important connection has been commented out. Also, it still has hashie dependency.

57  #connection = Faraday.new(:url => 'https://io.adafruit.com') do |c|

Documentation of send_data is wrong?

The documentation states : "data = aio.feeds("Test").data.send_data({value: 5})"

as far as I can see from the source code this should have been :

"data = aio.feeds("Test").data.send_data(5)", if you try to use the original version the dashboards will not work.

Typo in Group#groups

In the groups method, there's a parameter 'output_id', but the body refers to 'input_id'

-- I'm not sure which is the better name or I'd post a PR.

  0       def groups(feed_id_or_key, output_id=nil, options = {})                                                                                                                                                                 
  1         if input_id                                                              
  2           get "groups/#{feed_id_or_key}/#{input_id}", options                    
  3         else                                                                     
  4           get "groups/#{feed_id_or_key}", options                                
  5         end                                                                      
  6       end  

Unable to Write Feed Data (1.0.4)

Hey--

I did an update to 1.0.4 and noticed that my feeds were unable to write using this command:
data = aio.feeds("test_feed").data.last

I'm getting this exception thrown:

/usr/local/rvm/gems/ruby-2.3.0/gems/adafruit-io-1.0.4/lib/adafruit/io/client/data.rb:11:in `initialize': undefined method `id' for #<Adafruit::IO::Feed:0x00000002946c20> (NoMethodError)
        from /usr/local/rvm/gems/ruby-2.3.0/gems/adafruit-io-1.0.4/lib/adafruit/io/client/feed.rb:45:in `new'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/adafruit-io-1.0.4/lib/adafruit/io/client/feed.rb:45:in `data'
        from notes/adafruit_write.rb:5:in `<main>'

Downgrading to 1.0.3 fixed the issue though. (PS, love the work you guys have done on this library!)

Create Examples

Useful examples using the Ruby client for various use cases in an /examples folder.

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.