infogram-ruby 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c506de4569cbef7cc17a74fe6e23eaf9c027ae4a
4
- data.tar.gz: e0206530c3695c98f4460391bb0ff471218da4c2
3
+ metadata.gz: 968b32a5699c924baebf97495b400460f3c9239f
4
+ data.tar.gz: c3fe53dd1921cdd715ef9c16df1355cbb41c9854
5
5
  SHA512:
6
- metadata.gz: f9194557c5bf6e8b65bb73855e8e76cd03ec05e0119dda542d15979894e2146e06d3d13f45906adcca4c26390c66cef5547ce4918937cd9615bd16b871c906bd
7
- data.tar.gz: 60921e4df0f4645806a893a6549ca161cbf044c14bb86336b1c548afca5f31e6f4481eddd0cecf3c695752d59dd1a7b06eb9b8c2b89110c21997ec56dd1547c5
6
+ metadata.gz: 88cdb908c047d268536ea7a7bb0587aec1a29f80842297ab8846b16a96d1db26e2cf143d8b79747372a3c08bcb4716d9a33d8dfa9fcd5ddf11768464747de914
7
+ data.tar.gz: eef64086dfae9252b2c716b31b2b49dc0adb5d20040225c36c13a4e1ac273ca948a9f69e962b1932c1d3d400bfa0c7f8bdb0ddf604e8d5fdc5a06cba1915d8ff
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = 'infogram-ruby'
7
- s.version = '0.0.2'
7
+ s.version = '0.0.3'
8
8
  s.authors = ['Maksim Berjoza']
9
9
  s.email = ['torbjon@gmail.com']
10
10
  s.summary = %q{Infogr.am Ruby SDK}
@@ -0,0 +1,59 @@
1
+ require 'httparty'
2
+ require 'json'
3
+ require 'cgi'
4
+
5
+ class Infogram
6
+ API_URL = 'https://infogr.am/service/v1'
7
+
8
+ attr_accessor :api_key, :api_secret, :api_url
9
+
10
+ def initialize(api_key, api_secret)
11
+ @api_key = api_key
12
+ @api_secret = api_secret
13
+ @api_url = API_URL
14
+ end
15
+
16
+ def decode_params(params)
17
+ params.keys.sort.map{ |k|
18
+ value = (k.to_s == 'content') ? url_escaping(params[k].to_json) : params[k]
19
+ "#{k}=#{value}"
20
+ }.join('&')
21
+ end
22
+
23
+ def signature(method, path, params)
24
+ string_to_sign = [method.upcase, url_escaping("#{api_url}/#{path}"), url_escaping(decode_params(params))].compact.join('&')
25
+ raw_hmac = OpenSSL::HMAC.digest('sha1', api_secret, string_to_sign)
26
+ Base64.encode64(raw_hmac)[0..-2]
27
+ end
28
+
29
+ def url_escaping(string)
30
+ CGI.escape(string).gsub('+', '%20')
31
+ end
32
+
33
+ # themes
34
+ def get_themes(opts={})
35
+ opts[:api_key] = @api_key
36
+ opts[:api_sig] = signature('GET', 'themes', opts)
37
+ HTTParty.get("#{@api_url}/themes", query: opts)
38
+ end
39
+
40
+ # infographics
41
+ def get_infographics(opts={})
42
+ opts[:api_key] = @api_key
43
+ opts[:api_sig] = signature('GET', "infographics", opts)
44
+ HTTParty.get("#{@api_url}/infographics", query: opts)
45
+ end
46
+
47
+ def get_infographic(id, opts={})
48
+ opts[:api_key] = @api_key
49
+ opts[:api_sig] = signature('GET', "infographics/#{id}", opts)
50
+ HTTParty.get("#{@api_url}/infographics/#{id}", query: opts)
51
+ end
52
+
53
+ def create_infographic(opts={})
54
+ opts[:api_key] = @api_key
55
+ opts[:api_sig] = signature('POST', 'infographics', opts)
56
+ opts[:content] = opts[:content].to_json
57
+ HTTParty.post("#{@api_url}/infographics", body: opts)
58
+ end
59
+ end
@@ -0,0 +1,63 @@
1
+ require 'helper'
2
+
3
+ describe 'Infogram' do
4
+ let (:api_key) { 'nMECGhmHe9' }
5
+ let (:api_secret) { 'da5xoLrCCx' }
6
+ let (:client) { infogram_client(api_key, api_secret) }
7
+ let (:params) { {
8
+ content: [
9
+ {
10
+ type: 'h1',
11
+ text: 'Hello infogr.am'
12
+ }
13
+ ],
14
+ theme_id: 45,
15
+ publish: false,
16
+ title: 'Hello'
17
+ }
18
+ }
19
+
20
+ context 'Signature' do
21
+ let (:encoded_params) { 'api_key=nMECGhmHe9&content=%5B%7B%22type%22%3A%22h1%22%2C%22text%22%3A%22Hello%20infogr.am%22%7D%5D&publish=false&theme_id=45&title=Hello' }
22
+
23
+ it 'should encode params' do
24
+ params[:api_key] = api_key
25
+ expect(client.decode_params(params)).to eq(encoded_params)
26
+ end
27
+
28
+ it 'should generate signature' do
29
+ params[:api_key] = api_key
30
+ signature = client.signature('post', 'infographics', params)
31
+ expect(signature).to eq('bqwCqAk1TWDYNy3eqV0BiNuIERQ=')
32
+ end
33
+ end
34
+
35
+ context 'Themes' do
36
+ it 'should return themes list' do
37
+ stub_get('themes').to_return(with_fixture('themes.json'))
38
+ themes = client.get_themes()
39
+ expect(themes[0]['id']).to eq(61)
40
+ expect(themes[1]['title']).to eq('Megaphone')
41
+ end
42
+ end
43
+
44
+ context 'Infographic' do
45
+ it 'GET infographics' do
46
+ stub_get('infographics').to_return(with_fixture('infographics.json'))
47
+ ig = client.get_infographics
48
+ expect(ig[1]['id']).to eq('d819d9b7-67fb-40ec-9400-d63cc209aea8')
49
+ end
50
+
51
+ it 'GET infographics/:id' do
52
+ stub_get('infographics').to_return(with_fixture('infographic.json'))
53
+ ig = client.get_infographic('d819d9b7-67fb-40ec-9400-d63cc209aea8')
54
+ expect(ig['title']).to eq('sample infographic 2')
55
+ end
56
+
57
+ it 'POST infographics' do
58
+ stub_post('infographics').to_return(with_fixture('infographic.json'))
59
+ ig = client.create_infographic(params)
60
+ expect(ig['title']).to eq('sample infographic 2')
61
+ end
62
+ end
63
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: infogram-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maksim Berjoza
@@ -81,10 +81,12 @@ files:
81
81
  - README.md
82
82
  - Rakefile
83
83
  - infogram-ruby.gemspec
84
+ - lib/infogram-ruby.rb
84
85
  - spec/fixtures/infographic.json
85
86
  - spec/fixtures/infographics.json
86
87
  - spec/fixtures/themes.json
87
88
  - spec/helper.rb
89
+ - spec/infogram-ruby_spec.rb
88
90
  homepage: ''
89
91
  licenses:
90
92
  - MIT
@@ -105,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
107
  version: '0'
106
108
  requirements: []
107
109
  rubyforge_project:
108
- rubygems_version: 2.4.3
110
+ rubygems_version: 2.4.5
109
111
  signing_key:
110
112
  specification_version: 4
111
113
  summary: Infogr.am Ruby SDK
@@ -114,3 +116,4 @@ test_files:
114
116
  - spec/fixtures/infographics.json
115
117
  - spec/fixtures/themes.json
116
118
  - spec/helper.rb
119
+ - spec/infogram-ruby_spec.rb