pagifyio 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/pagifyio.rb +70 -0
- data/lib/pagifyio/client.rb +43 -0
- metadata +68 -0
data/lib/pagifyio.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
class Pagifyio
|
2
|
+
attr_accessor :options, :app_id, :app_secret
|
3
|
+
|
4
|
+
def initialize(id, secret)
|
5
|
+
@app_id = id
|
6
|
+
@app_secret = secret
|
7
|
+
@options = {
|
8
|
+
:host_name => "alpha.pagify.io",
|
9
|
+
:port => "80",
|
10
|
+
:method => "",
|
11
|
+
:path => "",
|
12
|
+
:accept_type => ""
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
def generate_pdf(template_id, data)
|
17
|
+
throw 'Please supply template_id' if (template_id == '' || template_id == nil)
|
18
|
+
request_data = {
|
19
|
+
:data => data
|
20
|
+
}
|
21
|
+
@options[:path] = "/api/templates/#{template_id}/generate_pdf"
|
22
|
+
@options[:accept_type] = "application/pdf"
|
23
|
+
@options[:method] = "post"
|
24
|
+
res = Client.request(@options, request_data, @app_id, @app_secret)
|
25
|
+
begin
|
26
|
+
JSON.parse(res.body)
|
27
|
+
rescue
|
28
|
+
res.body
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def list_templates
|
33
|
+
@options[:path] = "/api/templates"
|
34
|
+
@options[:accept_type] = "application/json"
|
35
|
+
@options[:method] = "get"
|
36
|
+
res = Client.request(@options, {}, @app_id, @app_secret)
|
37
|
+
JSON.parse(res.body)
|
38
|
+
end
|
39
|
+
|
40
|
+
def create_template(name)
|
41
|
+
throw 'Please supply name' if (name == '' || name == nil)
|
42
|
+
request_data = {
|
43
|
+
:template_name => name
|
44
|
+
}
|
45
|
+
@options[:path] = "/api/templates"
|
46
|
+
@options[:accept_type] = "application/json"
|
47
|
+
@options[:method] = "post"
|
48
|
+
res = Client.request(@options, request_data, @app_id, @app_secret)
|
49
|
+
JSON.parse(res.body)
|
50
|
+
end
|
51
|
+
|
52
|
+
def edit_template(template_id)
|
53
|
+
throw 'Please supply template_id' if (template_id == '' || template_id == nil)
|
54
|
+
@options[:path] = "/api/templates/#{template_id}/edit"
|
55
|
+
@options[:accept_type] = "application/json"
|
56
|
+
@options[:method] = "get"
|
57
|
+
res = Client.request(@options, {}, @app_id, @app_secret)
|
58
|
+
JSON.parse(res.body)
|
59
|
+
end
|
60
|
+
|
61
|
+
def delete_template(template_id)
|
62
|
+
@options[:path] = "/api/templates/#{template_id}"
|
63
|
+
@options[:accept_type] = "application/json"
|
64
|
+
@options[:method] = "delete"
|
65
|
+
res = Client.request(@options, {}, @app_id, @app_secret)
|
66
|
+
JSON.parse(res.body)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
require 'pagifyio/client'
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'openssl'
|
3
|
+
require 'base64'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module Pagifyio::Client
|
7
|
+
def self.request(options, data, api_key, api_secret)
|
8
|
+
conn = Faraday.new(:url => "http://#{options[:host_name]}:#{options[:port]}") do |faraday|
|
9
|
+
faraday.adapter Faraday.default_adapter
|
10
|
+
end
|
11
|
+
|
12
|
+
response = conn.send(options[:method]) do |req|
|
13
|
+
req.url options[:path]
|
14
|
+
req.headers['Content-Type'] = "application/json"
|
15
|
+
req.headers['Accept-Type'] = options[:accept_type]
|
16
|
+
if options[:method] != 'get'
|
17
|
+
req.body = JSON.generate(data)
|
18
|
+
req.headers['Content-Length'] = req.body.length.to_s
|
19
|
+
else
|
20
|
+
req.headers['Content-Length'] = "0"
|
21
|
+
end
|
22
|
+
req.headers['Date'] = Time.now().to_s
|
23
|
+
req.headers['Authentication'] = api_key + ':' + sign_request(req, api_secret)
|
24
|
+
end
|
25
|
+
response
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.sign_request(request, secret)
|
29
|
+
digest = OpenSSL::Digest::Digest.new('sha1')
|
30
|
+
Base64.encode64(OpenSSL::HMAC.digest(digest, secret, canonical_string(request))).strip
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.canonical_string(request)
|
34
|
+
method = request.method.to_s.upcase
|
35
|
+
content_type = request.headers['Content-Type']
|
36
|
+
content_md5 = ''
|
37
|
+
content_length = request.headers['Content-Length']
|
38
|
+
date = request.headers['Date']
|
39
|
+
path = request.respond_to?(:unparsed_uri) ? request.unparsed_uri : request.path
|
40
|
+
canonical_str = method + content_type + content_md5 + content_length + date + path
|
41
|
+
canonical_str
|
42
|
+
end
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pagifyio
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kush Kella
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: faraday
|
16
|
+
requirement: &24911256 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *24911256
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: json
|
27
|
+
requirement: &24929580 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *24929580
|
36
|
+
description:
|
37
|
+
email: kush@pagify.io
|
38
|
+
executables: []
|
39
|
+
extensions: []
|
40
|
+
extra_rdoc_files: []
|
41
|
+
files:
|
42
|
+
- lib/pagifyio.rb
|
43
|
+
- lib/pagifyio/client.rb
|
44
|
+
homepage:
|
45
|
+
licenses: []
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.8.16
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: Pagify client for ruby
|
68
|
+
test_files: []
|