Class: BrocadeIo

Inherits:
Object
  • Object
show all
Defined in:
lib/brocade_io.rb,
lib/brocade_io/item.rb,
lib/brocade_io/version.rb

Defined Under Namespace

Classes: Item

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BrocadeIo

Returns a new instance of BrocadeIo.



9
10
11
12
# File 'lib/brocade_io.rb', line 9

def initialize(options = {})
  @host = options[:host] || "https://www.brocade.io"
  @version = options[:version] || 1
end

Instance Method Details

#add_image(gtin, image, image_type) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# 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) ⇒ Object



14
15
16
17
18
19
# 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 = {}) ⇒ Object



40
41
42
43
44
45
46
47
# 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) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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) ⇒ Object



21
22
23
24
25
26
# 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