Class: Pinata::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/pinata/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pinata_api_key, pinata_secret_api_key, api_endpoint, gateway_endpoint) ⇒ Client

Returns a new instance of Client.



15
16
17
18
19
20
21
# File 'lib/pinata/client.rb', line 15

def initialize(pinata_api_key, pinata_secret_api_key, api_endpoint, gateway_endpoint)
  @pinata_api_key = pinata_api_key
  @pinata_secret_api_key = pinata_secret_api_key
  @api_endpoint = api_endpoint
  @gateway_endpoint = gateway_endpoint
  @http_client = HTTP
end

Instance Attribute Details

#api_endpointObject (readonly)

Returns the value of attribute api_endpoint.



13
14
15
# File 'lib/pinata/client.rb', line 13

def api_endpoint
  @api_endpoint
end

#gateway_endpointObject (readonly)

Returns the value of attribute gateway_endpoint.



13
14
15
# File 'lib/pinata/client.rb', line 13

def gateway_endpoint
  @gateway_endpoint
end

#http_clientObject (readonly)

Returns the value of attribute http_client.



13
14
15
# File 'lib/pinata/client.rb', line 13

def http_client
  @http_client
end

Instance Method Details

#add(path) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pinata/client.rb', line 23

def add(path)
  res = RestClient.post "#{@api_endpoint}",
    { file: File.new(path,"rb") },
	{ "pinata_api_key" => @pinata_api_key, "pinata_secret_api_key" => @pinata_secret_api_key }

  if res.code >= 200 && res.code <= 299
    JSON.parse(res.body)
  else
    raise Error, res.body
  end
end

#build_file_url(hash) ⇒ Object



57
58
59
# File 'lib/pinata/client.rb', line 57

def build_file_url(hash)
  "#{@gateway_endpoint}/#{hash}"
end

#cat(hash, offset, length) ⇒ Object



35
36
37
38
# File 'lib/pinata/client.rb', line 35

def cat(hash, offset, length)
  res = @http_client.get("#{@api_endpoint}/api/v0/cat?arg#{hash}&offset=#{offset}&length=#{length}")
  res.body
end

#download(hash, &block) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/pinata/client.rb', line 40

def download(hash, &block)
  url = build_file_url(hash)
  res = RestClient.get(url)

  if block_given?
    res.return!(&block)
  else
    res.return!
  end
end

#file_exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
# File 'lib/pinata/client.rb', line 51

def file_exists?(key)
  url = build_file_url(key)
  res = RestClient.get "#{@gateway_endpoint}#{key}"
  res.code == 200
end