Class: Ligo::Accessory

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/ligo/accessory.rb

Overview

A virtual accessory to interact via usb with an android device according to the Android Open Accessory Protocol.

Constant Summary collapse

DEFAULT_ID =

The default id used to initialize a new accessory if none is provided to the constructor

{
  manufacturer: 'ligō',
  model:        'Demo',
  description:  'ligō virtual accessory',
  version:      '1.0',
  uri:          'https://github.com/nibua-r/ligo#readme',
  # 'ligō 1.0'.each_byte {|c| print c.to_i.to_s(16), '' }
  serial:       '6c6967c58d20312e30'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

configure_logger_for, configure_logger_output, #logger, logger_for

Constructor Details

#initialize(id = DEFAULT_ID) ⇒ Accessory

Returns a new Ligo::Accessory initialized with the provided identification

Examples:

new_id =
{
  manufacturer: 'MyVeryBigCompany Corp.',
  model:        'AwesomeProduct',
  description:  'Who cares about description! ← 😠',
  version:      '0.0',
  uri:          'http://www.foo.bar/awesome_product',
  serial:       '⚀⚁⚂⚃⚄⚅012345678'
}
accessory = Ligo::Accessory.new(new_id)

Parameters:

  • id (Hash<Symbol, String>) (defaults to: DEFAULT_ID)

    The accessory identifying information as a hash.

Raises:

  • (ArgumentError)

    if the provided id is not a Hash or if one of the identifying string is missing.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/ligo/accessory.rb', line 123

def initialize(id = DEFAULT_ID)

  unless id.is_a? Hash
    raise ArgumentError, '#new must be called with a Hash'
  end

  required_ids = [:manufacturer,
                  :model, :description,
                  :version,
                  :uri,
                  :serial]

  required_ids.each do |sym|
    raise ArgumentError, "Missing argument: #{sym}" unless id.include? sym
  end

  id.each do |k, v|
    raise ArgumentError, "#{k} is not a String" unless v.is_a? String
    raise ArgumentError, "#{k} must not be empty" if v.empty?
    if v.bytesize > 255
      raise ArgumentError, "#{k} must contain at most 255 bytes"
    end
    instance_variable_set "@#{k}", v unless v.nil?
  end

  @id = id
  logger.debug self.inspect
end

Instance Attribute Details

#descriptionString (readonly)

Returns the description identifying string

Examples:

accessory = Ligo::Accessory.new
accessory.description
# => "ligō virtual accessory"

Returns:

  • (String)

    the description identifying string.



80
81
82
# File 'lib/ligo/accessory.rb', line 80

def description
  @description
end

#idHash<Symbol, String> (readonly)

Returns the full identifying information hash

Examples:

accessory = Ligo::Accessory.new
accessory.id
# => {:manufacturer=>"ligō",
#     :model=>"Demo",
#     :description=>"ligō virtual accessory",
#     :version=>"1.0",
#     :uri=>"https://github.com/nibua-r/ligo#readme",
#     :serial=>"6c6967c58d20312e30"}

Returns:

  • (Hash<Symbol, String>)

    the full identifying information hash.



56
57
58
# File 'lib/ligo/accessory.rb', line 56

def id
  @id
end

#manufacturerString (readonly)

Returns the manufacturer name identifying string

Examples:

accessory = Ligo::Accessory.new
accessory.manufacturer
# => "ligō"

Returns:

  • (String)

    the manufacturer name identifying string.



64
65
66
# File 'lib/ligo/accessory.rb', line 64

def manufacturer
  @manufacturer
end

#modelString (readonly)

Returns the model name identifying string

Examples:

accessory = Ligo::Accessory.new
accessory.model
# => "Demo"

Returns:

  • (String)

    the model name identifying string.



72
73
74
# File 'lib/ligo/accessory.rb', line 72

def model
  @model
end

#serialString (readonly)

Returns the serial identifying string

Examples:

accessory = Ligo::Accessory.new
accessory.serial
# => "6c6967c58d20312e30"

Returns:

  • (String)

    the serial identifying string.



104
105
106
# File 'lib/ligo/accessory.rb', line 104

def serial
  @serial
end

#uriString (readonly)

Returns the uri identifying string

Examples:

acc = Ligo::Accessory.new
acc.uri
# => "https://github.com/nibua-r/ligo#readme"

Returns:

  • (String)

    the uri identifying string.



96
97
98
# File 'lib/ligo/accessory.rb', line 96

def uri
  @uri
end

#versionString (readonly)

Returns the version identifying string

Examples:

accessory = Ligo::Accessory.new
accessory.version
# => "1.0"

Returns:

  • (String)

    the version identifying string.



88
89
90
# File 'lib/ligo/accessory.rb', line 88

def version
  @version
end

Instance Method Details

#each(&block) ⇒ Object

Returns #id.each

Examples:

accessory.each do |k,v|
  puts "#{k} = #{v}"
end
# manufacturer = ligō
# model = Demo
# description = ligō virtual accessory
# version = 1.0
# uri = https://github.com/nibua-r/ligo#readme
# serial = 6c6967c58d20312e30

Returns:

  • block execution.



164
165
166
# File 'lib/ligo/accessory.rb', line 164

def each(&block)
  @id.each(&block)
end

#keysArray

Returns #id.keys

Examples:

accessory.keys.inspect
# => "[:manufacturer, :model, :description, :version, :uri, :serial]"

Returns:

  • (Array)

    #id.keys.



173
174
175
# File 'lib/ligo/accessory.rb', line 173

def keys
  @id.keys
end