Class: Megaplan::Api

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/megaplan/api.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Api

Returns a new instance of Api.



11
12
13
14
15
# File 'lib/megaplan/api.rb', line 11

def initialize(attrs = {})
  @endpoint = attrs[:endpoint]
  @login    = attrs[:login]
  @password = attrs[:password]
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



9
10
11
# File 'lib/megaplan/api.rb', line 9

def endpoint
  @endpoint
end

#loginObject (readonly)

Returns the value of attribute login.



9
10
11
# File 'lib/megaplan/api.rb', line 9

def 
  @login
end

#passwordObject (readonly)

Returns the value of attribute password.



9
10
11
# File 'lib/megaplan/api.rb', line 9

def password
  @password
end

Class Method Details

.bad_response(response, parsed_body) ⇒ Object

Raises:

  • (StandardError)


171
172
173
174
175
176
177
178
# File 'lib/megaplan/api.rb', line 171

def bad_response(response, parsed_body)
  puts parsed_body

  if response.class == HTTParty::Response
    raise HTTParty::ResponseError, response
  end
  raise StandardError, (parsed_body['status']['message'] rescue 'unknown error')
end

.card(client, query = {}) ⇒ Object



116
117
118
# File 'lib/megaplan/api.rb', line 116

def card(client, query = {})
  make_get_req('card.api', client, query, nil)
end

.check_response(response) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/megaplan/api.rb', line 77

def check_response(response)
  if response.success?
    parsed_body(response)
  else
    bad_response(response, parsed_body(response))
  end
end

.create(client, query = {}) ⇒ Object



120
121
122
# File 'lib/megaplan/api.rb', line 120

def create(client, query = {})
  make_post_req('create.api', client, query, nil)
end

.custom_get(client, custom_path, query = {}) ⇒ Object



136
137
138
# File 'lib/megaplan/api.rb', line 136

def custom_get(client, custom_path, query = {})
  make_get_req(nil, client, query, custom_path)
end

.custom_post(client, custom_path, query = {}) ⇒ Object



140
141
142
# File 'lib/megaplan/api.rb', line 140

def custom_post(client, custom_path, query = {})
  make_post_req(nil, client, query, custom_path)
end

.delete(client, query = {}) ⇒ Object



132
133
134
# File 'lib/megaplan/api.rb', line 132

def delete(client, query = {})
  make_post_req('delete.api', client, query, nil)
end

.edit(client, query = {}) ⇒ Object



128
129
130
# File 'lib/megaplan/api.rb', line 128

def edit(client, query = {})
  make_post_req('edit.api', client, query, nil)
end

.find(client, query) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/megaplan/api.rb', line 85

def find(client, query)
  output = []
  @arr.each do |item|
    success = if query.keys.all? { |x| item.keys.include? x }
                if query.all? { |k,v| !v.respond_to?(:keys)}
                  query.keys.all? { |i| query[i] == item[i]} ? true : false
                else
                  result = []
                  query.keys.each do |each|
                    result << if query[each].respond_to?(:keys)
                                if query[each].keys.all? { |x| item[each].keys.include? x }
                                   query[each].keys.all?{ |i| query[each][i] == item[each][i] } ? true : false
                                end
                              else
                                true if query[each] == item[each]
                              end
                  end
                  result.uniq == [true] ? true : false
                end
              else
                false
              end
    output << item if success
  end
  output
end

.list(client, query = {}) ⇒ Object



112
113
114
# File 'lib/megaplan/api.rb', line 112

def list(client, query = {})
  make_get_req('list.api', client, query, nil)
end

.make_get_req(action, client, query, custom_path) ⇒ Object



151
152
153
154
155
156
# File 'lib/megaplan/api.rb', line 151

def make_get_req(action, client, query, custom_path)
  path = resource_path(:get, client, action, custom_path, query)
  headers = client.get_headers(:get, path.gsub('https://', ''))
  response = HTTParty.get(path, headers: headers)
  check_response(response)
end

.make_post_req(action, client, query, custom_path) ⇒ Object



144
145
146
147
148
149
# File 'lib/megaplan/api.rb', line 144

def make_post_req(action, client, query, custom_path)
  path = resource_path(:post, client, action, custom_path, query)
  headers = client.get_headers(:post, path.gsub('https://', ''))
  response = HTTParty.post(path, body: query.to_json, headers: headers)
  check_response(response)
end

.parsed_body(res) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/megaplan/api.rb', line 60

def parsed_body(res)
  body = JSON.parse(res.body) rescue {}
  if body["status"]["code"] != "error"
    body["data"]
  else
    body
  end
end

.query_path(path, query) ⇒ Object



73
74
75
# File 'lib/megaplan/api.rb', line 73

def query_path(path, query)
  path + (query.any? ? "?#{to_query(query)}" : "")
end

.resource_path(type, client, action_path, custom_path, query = {}) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/megaplan/api.rb', line 158

def resource_path(type, client, action_path, custom_path, query = {})
  if custom_path
    url = "https://#{client.initial_path}" << custom_path
  else
    class_name = name.split('::').inject(Object) do |mod, class_name|
      mod.const_get(class_name)
    end
    class_endpoint = class_name.class_endpoint rescue "/"
    url = "https://#{client.initial_path}" << class_endpoint << action_path
  end
  query_path(url, query)
end

.save(client, query = {}) ⇒ Object



124
125
126
# File 'lib/megaplan/api.rb', line 124

def save(client, query = {})
  make_post_req('save.api', client, query, nil)
end

.to_query(params) ⇒ Object



69
70
71
# File 'lib/megaplan/api.rb', line 69

def to_query(params)
  params.to_a.map { |x| "#{CGI.escape(x[0])}=#{CGI.escape(x[1])}" }.join("&")
end

Instance Method Details

#auth_paramsObject



26
27
28
29
# File 'lib/megaplan/api.rb', line 26

def auth_params
  require 'digest/md5'
  { Login: , Password: Digest::MD5.hexdigest(password) }
end

#auth_pathObject



31
32
33
# File 'lib/megaplan/api.rb', line 31

def auth_path
  "https://" + initial_path + "/BumsCommonApiV01/User/authorize.api"
end

#authenticateObject



17
18
19
20
21
22
23
24
# File 'lib/megaplan/api.rb', line 17

def authenticate
  response = HTTParty.get(auth_path, :query => auth_params)
  if response.success?
    parsed_body(response)
  else
    bad_response(response, parsed_body(response), auth_params)
  end
end

#create_signature(type, key, date, path) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/megaplan/api.rb', line 50

def create_signature(type, key, date, path)
  require 'cgi'
  require 'openssl'

  data = "#{type == :get ? "GET" : "POST"}\n\napplication/json\n#{date}\n#{path}"
  Base64.strict_encode64(OpenSSL::HMAC.hexdigest('sha1', key, data))
end

#get_headers(type, path) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/megaplan/api.rb', line 39

def get_headers(type, path)
  attrs = authenticate
  secret_key = attrs['SecretKey']
  date = Time.now.rfc2822
  { "Date"=> date,
    "Accept"=> "application/json",
    "Content-Type" => "application/json",
    "X-Authorization" => "#{attrs['AccessId']}:#{create_signature(type, secret_key, date, path)}"
  }
end

#initial_pathObject



35
36
37
# File 'lib/megaplan/api.rb', line 35

def initial_path
  "#{endpoint}.megaplan.ru"
end