class BrocadeIo

Constants

VERSION

Public Class Methods

new(options = {}) click to toggle source
# File lib/brocade_io.rb, line 9
def initialize(options = {})
  @host = options[:host] || "https://www.brocade.io"
  @version = options[:version] || 1
end

Public Instance Methods

add_image(gtin, image, image_type) click to toggle source
# File lib/brocade_io.rb, line 28
def add_image(gtin, image, image_type)
  params = {
    gtin: gtin,
    image: image,
    perspective: image_type
  }
  response = http_client.post("/api/items/#{gtin}/images?version=#{@version}", params)
  if response.success?
    JSON.parse(response.body)
  end
end
item(gtin) click to toggle source
# File lib/brocade_io.rb, line 14
def item(gtin)
  response = http_client.get("/api/items/#{gtin}?version=#{@version}")
  if response.success?
    Item.new(JSON.parse(response.body))
  end
end
items(params = {}) click to toggle source
# File lib/brocade_io.rb, line 40
def items(params = {})
  response = http_client.get("/api/items?version=#{@version}", params)
  if response.success?
    JSON.parse(response.body).map do |item|
      Item.new(item)
    end
  end
end
paginated_items(params) { |item| ... } click to toggle source
# File lib/brocade_io.rb, line 49
def paginated_items(params)
  response = http_client.get("/api/items?version=#{@version}", params)
  loop do
    links = {}
    if response.success?
      JSON.parse(response.body).map do |item|
        yield Item.new(item)
      end

      # https://gist.github.com/davidcelis/5896686
      response.headers['Link'].to_s.split(',').each do |link|
        link.strip!
        parts = link.match(/<(.+)>; *rel="(.+)"/)
        links[parts[2]] = parts[1]
      end
    end

    break unless links["next"] && (response = http_client.get(links["next"]))
  end
end
update_item(gtin, attributes) click to toggle source
# File lib/brocade_io.rb, line 21
def update_item(gtin, attributes)
  response = http_client.put("/api/items/#{gtin}?version=#{@version}", attributes)
  if response.success?
    Item.new(JSON.parse(response.body))
  end
end

Protected Instance Methods

http_client() click to toggle source
# File lib/brocade_io.rb, line 72
def http_client
  Faraday.new(url: @host) do |conn|
    conn.request :multipart
    conn.request :url_encoded
    conn.adapter :net_http
  end
end