Class: Readability::Client

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

Constant Summary collapse

BASE_API =
"/api/rest/v1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ Client



8
9
10
11
12
13
# File 'lib/readability/client.rb', line 8

def initialize(token)
  @token = token.dup

  # get available resource types
  @resources = get
end

Instance Attribute Details

#resourcesObject

Returns the value of attribute resources.



4
5
6
# File 'lib/readability/client.rb', line 4

def resources
  @resources
end

Instance Method Details

#format_query(resource, args) ⇒ Object Also known as: to_url



27
28
29
30
31
32
33
# File 'lib/readability/client.rb', line 27

def format_query(resource, args)
  options = args.dup
  query = "#{BASE_API}/#{resource}"
  query << "/#{options.delete :id}" if options.include? :id
  query << "?#{parameterize(options)}" unless options.empty?
  query
end

#get(resource = "", args = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/readability/client.rb', line 15

def get(resource="", args={})
  response = @token.get(format_query(resource, args))
  case response
    when Net::HTTPSuccess
      data = JSON.parse(response.body)
      data = data[resource.to_s] unless resource.blank? || args.include?(:id)
      data
  else
    raise StandardError, "Could not get data for those params."
  end
end

#parameterize(params) ⇒ Object



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

def parameterize(params)
  URI.escape(params.collect{|k,v| "#{k}=#{v}"}.join('&'))
end