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



53
54
55
56
57
58
# File 'lib/infogram-ruby.rb', line 53

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

#decode_params(params) ⇒ Object



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

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

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



47
48
49
50
51
# File 'lib/infogram-ruby.rb', line 47

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



41
42
43
44
45
# File 'lib/infogram-ruby.rb', line 41

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



34
35
36
37
38
# File 'lib/infogram-ruby.rb', line 34

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



23
24
25
26
27
# File 'lib/infogram-ruby.rb', line 23

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



29
30
31
# File 'lib/infogram-ruby.rb', line 29

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