Class: Infogram

Inherits:
Object
  • Object
show all
Defined in:
lib/infogram-ruby.rb

Constant Summary collapse

API_URL =
'https://infogr.am/service/v1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, api_secret) ⇒ Infogram

Returns a new instance of Infogram.



10
11
12
13
14
# File 'lib/infogram-ruby.rb', line 10

def initialize(api_key, api_secret)
  @api_key = api_key
  @api_secret = api_secret
  @api_url = API_URL
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



8
9
10
# File 'lib/infogram-ruby.rb', line 8

def api_key
  @api_key
end

#api_secretObject

Returns the value of attribute api_secret.



8
9
10
# File 'lib/infogram-ruby.rb', line 8

def api_secret
  @api_secret
end

#api_urlObject

Returns the value of attribute api_url.



8
9
10
# File 'lib/infogram-ruby.rb', line 8

def api_url
  @api_url
end

Instance Method Details

#create_infographic(opts = {}) ⇒ Object



50
51
52
53
54
55
# File 'lib/infogram-ruby.rb', line 50

def create_infographic(opts={})
  opts[:api_key] = @api_key
  opts[:content] = opts[:content].to_json
  opts[:api_sig] = signature('POST', 'infographics', opts)
  HTTParty.post("#{@api_url}/infographics", body: opts)
end

#decode_params(params) ⇒ Object



16
17
18
# File 'lib/infogram-ruby.rb', line 16

def decode_params(params)
  params.keys.sort.map{ |k| "#{k}=#{url_escaping(params[k].to_s)}" }.join('&')
end

#get_infographic(id, opts = {}) ⇒ Object



44
45
46
47
48
# File 'lib/infogram-ruby.rb', line 44

def get_infographic(id, opts={})
  opts[:api_key] = @api_key
  opts[:api_sig] = signature('GET', "infographics/#{id}", opts)
  HTTParty.get("#{@api_url}/infographics/#{id}", query: opts)
end

#get_infographics(opts = {}) ⇒ Object

infographics



38
39
40
41
42
# File 'lib/infogram-ruby.rb', line 38

def get_infographics(opts={})
  opts[:api_key] = @api_key
  opts[:api_sig] = signature('GET', "infographics", opts)
  HTTParty.get("#{@api_url}/infographics", query: opts)
end

#get_themes(opts = {}) ⇒ Object

themes



31
32
33
34
35
# File 'lib/infogram-ruby.rb', line 31

def get_themes(opts={})
  opts[:api_key] = @api_key
  opts[:api_sig] = signature('GET', 'themes', opts)
  HTTParty.get("#{@api_url}/themes", query: opts)
end

#signature(method, path, params) ⇒ Object



20
21
22
23
24
# File 'lib/infogram-ruby.rb', line 20

def signature(method, path, params)
  string_to_sign = [method.upcase, url_escaping("#{api_url}/#{path}"), url_escaping(decode_params(params))].compact.join('&')
  raw_hmac = OpenSSL::HMAC.digest('sha1', api_secret, string_to_sign)
  Base64.encode64(raw_hmac)[0..-2]
end

#url_escaping(string) ⇒ Object



26
27
28
# File 'lib/infogram-ruby.rb', line 26

def url_escaping(string)
  CGI.escape(string).gsub('+', '%20')
end