ordrin 0.1.4 → 1.0.0

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.
@@ -0,0 +1,124 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'digest'
4
+ require 'json-schema'
5
+ require_relative 'mutate'
6
+
7
+ module Ordrin
8
+ class APIHelper
9
+
10
+ def initialize(api_key, servers)
11
+ @api_key = api_key
12
+ @urls = {}
13
+ if servers==:production
14
+ @urls[:restaurant] = "https://r.ordr.in"
15
+ @urls[:user] = "https://u.ordr.in"
16
+ @urls[:order] = "https://o.ordr.in"
17
+ elsif servers==:test
18
+ @urls[:restaurant] = "https://r-test.ordr.in"
19
+ @urls[:user] = "https://u-test.ordr.in"
20
+ @urls[:order] = "https://o-test.ordr.in"
21
+ else
22
+ raise ArgumentError.new("servers must be set to :production, :test, or :custom")
23
+ end
24
+ dir = File.dirname(__FILE__)
25
+ @ENDPOINT_INFO = JSON.parse!(File.read(File.join(dir, "schemas.json")))
26
+ end
27
+
28
+ def call_api(base_url, method, uri, data=nil, login=nil)
29
+ methods = {"GET" => Net::HTTP::Get,
30
+ "POST" => Net::HTTP::Post,
31
+ "PUT" => Net::HTTP::Put,
32
+ "DELETE" => Net::HTTP::Delete}
33
+ method = methods[method]
34
+ full_url = URI.parse(base_url+uri)
35
+ req = method.new(full_url.request_uri)
36
+ if not data.nil?
37
+ req.body = URI.encode_www_form(data)
38
+ end
39
+ if not @api_key.empty?
40
+ req['X-NAAMA-CLIENT-AUTHENTICATION'] = "id=\"#{@api_key}\", version=\"1\""
41
+ end
42
+ if not login.nil?
43
+ hash_code = Digest::SHA256.new.hexdigest("#{login[:password]}#{login[:email]}#{uri}")
44
+ req['X-NAAMA-AUTHENTICATION'] = "username=\"#{login[:email]}\", response=\"#{hash_code}\", version=\"1\""
45
+ end
46
+ http = Net::HTTP.new(full_url.host, full_url.port)
47
+ if full_url.scheme == "https"
48
+ http.use_ssl = true
49
+ http.ca_file = File.join(File.dirname(__FILE__), "cacert.pem")
50
+ end
51
+ res = http.start {|http| http.request(req)}
52
+ #error if not OK response
53
+ res.value
54
+ result = JSON.parse(res.body)
55
+ if result.is_a? Hash
56
+ if result.has_key?('_error') and result['_error']!=0
57
+ if result.has_key?('text')
58
+ raise Errors::ApiError.new(result['msg'], result['text'])
59
+ else
60
+ raise Errors::ApiError.new(result['msg'])
61
+ end
62
+ result
63
+ end
64
+ end
65
+ result
66
+ end
67
+
68
+ def call_endpoint(endpoint_group, endpoint_name, url_params, kwargs)
69
+ endpoint_data = @ENDPOINT_INFO[endpoint_group.to_s][endpoint_name]
70
+ value_mutators = {};
71
+ endpoint_data["properties"].each do |name, info|
72
+ if info.has_key?("mutator")
73
+ value_mutators[name] = Mutate.method(info["mutator"].intern)
74
+ else
75
+ value_mutators[name] = Mutate.method(:identity)
76
+ end
77
+ end
78
+ if endpoint_data.has_key?("allOf")
79
+ endpoint_data["allOf"].each do |subschema|
80
+ subschema["oneOf"].each do |option|
81
+ option["properties"].each do |name, info|
82
+ if info.has_key?("mutator")
83
+ value_mutators[name] = Mutate.method(info["mutator"].intern)
84
+ else
85
+ value_mutators[name] = Mutate.method(:identity)
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+ if not value_mutators.has_key?("email")
92
+ value_mutators["email"] = Mutate.method(:identity)
93
+ end
94
+ JSON::Validator.validate!(endpoint_data, kwargs)
95
+ arg_dict = {}
96
+ url_params.each do |name|
97
+ arg_dict[name.intern] = URI.encode(value_mutators[name].call(kwargs[name]))
98
+ end
99
+
100
+ data = {}
101
+ kwargs.each do |name, value|
102
+ if value_mutators.has_key?(name)
103
+ data[name] = value_mutators[name].call(value)
104
+ end
105
+ end
106
+ data.keep_if do |name, value|
107
+ not (url_params.include?(name) or name == "current_password")
108
+ end
109
+ if data.empty?
110
+ data = nil
111
+ end
112
+ tmpl = endpoint_data["meta"]["uri"].gsub(/\{.+?\}/, '%\0')
113
+ uri = tmpl % arg_dict
114
+ login = nil
115
+ if endpoint_data["meta"]["userAuth"]
116
+ if not (kwargs.has_key?("email") and kwargs.has_key?("current_password"))
117
+ raise ArgumentError.new("Authenticated request requires arguments 'email' and 'current_password'")
118
+ end
119
+ login = {email: kwargs["email"], password: Mutate.sha256(kwargs["curent_password"])}
120
+ end
121
+ call_api(@urls[endpoint_group], endpoint_data["meta"]["method"], uri, data, login)
122
+ end
123
+ end
124
+ end