Class: ShotApiGem::Consumer

Inherits:
Object
  • Object
show all
Defined in:
lib/shot_api_gem/consumer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Consumer

Returns a new instance of Consumer.



8
9
10
11
12
# File 'lib/shot_api_gem/consumer.rb', line 8

def initialize(api_key)
  @header = { authorization: api_key }
  @base_url = 'http://turbo-url.herokuapp.com/api/v1/links'
  @local_base_url = 'http://localhost:3000/api/v1/links'
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



6
7
8
# File 'lib/shot_api_gem/consumer.rb', line 6

def base_url
  @base_url
end

#headerObject (readonly)

Returns the value of attribute header.



6
7
8
# File 'lib/shot_api_gem/consumer.rb', line 6

def header
  @header
end

#local_base_urlObject (readonly)

Returns the value of attribute local_base_url.



6
7
8
# File 'lib/shot_api_gem/consumer.rb', line 6

def local_base_url
  @local_base_url
end

Instance Method Details

#create(options) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/shot_api_gem/consumer.rb', line 26

def create(options)
  response = RestClient.post(
               base_url,
               { given_url: options[:url], slug: options[:slug] },
               header
             )
  Link.new(json_response(response.body))
end

#delete(id) ⇒ Object



44
45
46
47
# File 'lib/shot_api_gem/consumer.rb', line 44

def delete(id)
  response = RestClient.delete(base_url << "/#{id}", header)
  response.code
end

#fetch(id) ⇒ Object



21
22
23
24
# File 'lib/shot_api_gem/consumer.rb', line 21

def fetch(id)
  response = RestClient.get(base_url << "/#{id}", header)
  Link.new(json_response(response.body))
end

#fetch_allObject



14
15
16
17
18
19
# File 'lib/shot_api_gem/consumer.rb', line 14

def fetch_all
  response = RestClient.get(base_url, header)
  json_response(response.body)[:links].map do |link|
    Link.new(link)
  end
end

#update(id, options) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/shot_api_gem/consumer.rb', line 35

def update(id, options)
  response = RestClient.patch(
      base_url << "/#{id}",
      { given_url: options[:url], slug: options[:slug], active: options[:active] },
      header
    )
  Link.new(json_response(response.body))
end