Sha256: 2658635539e960ec0db1c331f9c15e867d036562e51dc90545865617f52f7fb5

Contents?: true

Size: 1.27 KB

Versions: 4

Compression:

Stored size: 1.27 KB

Contents

module IRKit

  class InternetAPI

    attr_reader :clientkey, :deviceid

    def initialize(opts = {:clientkey => nil, :deviceid => nil})
      raise ArgumentError, "clientkey must be String" unless opts[:clientkey].kind_of? String
      raise ArgumentError, "deviceid must be String" unless opts[:deviceid].kind_of? String
      @clientkey = opts[:clientkey]
      @deviceid = opts[:deviceid]
    end

    def url
      "https://api.getirkit.com/1"
    end

    def to_s
      %Q{#<#{self.class} @deviceid="#{@deviceid[0..6]}XXXXX" @clientkey="#{@clientkey[0..6]}XXXXX">}
    end

    def get_messages(query={})
      opts = {
        :query => {:clientkey => @clientkey}
      }
      query.each do |k,v|
        opts[:query][k] = v
      end
      res = HTTParty.get("#{url}/messages", opts)
      case res.code
      when 200
        return nil if res.body.length < 1
        return IRKit::Response.new JSON.parse res.body
      when 401
        raise IRKit::Error, "Unauthorized"
      else
        raise IRKit::Error, res.body
      end
    end

    def post_messages(data)
      opts = {
        :body => {
          :deviceid => @deviceid,
          :clientkey => @clientkey,
          :message => data.to_json
        }
      }
      HTTParty.post "#{url}/messages", opts
    end

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
irkit-0.1.0 lib/irkit/internet_api.rb
irkit-0.0.9 lib/irkit/internet_api.rb
irkit-0.0.8 lib/irkit/internet_api.rb
irkit-0.0.7 lib/irkit/internet_api.rb