Class: Honey::Okapi::ApiaryConnector

Inherits:
Object
  • Object
show all
Defined in:
lib/okapi/apiary_connector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(apiary_url, req_path, res_path) ⇒ ApiaryConnector

Returns a new instance of ApiaryConnector.



10
11
12
13
14
# File 'lib/okapi/apiary_connector.rb', line 10

def initialize(apiary_url, req_path, res_path)
  @apiary_url = apiary_url
  @req_path = req_path
  @res_path  = res_path
end

Instance Attribute Details

#apiary_urlObject (readonly)

Returns the value of attribute apiary_url.



8
9
10
# File 'lib/okapi/apiary_connector.rb', line 8

def apiary_url
  @apiary_url
end

#blueprintObject (readonly)

Returns the value of attribute blueprint.



8
9
10
# File 'lib/okapi/apiary_connector.rb', line 8

def blueprint
  @blueprint
end

Instance Method Details

#get_requests(resources, blueprint, all_resources = false, global_vars = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/okapi/apiary_connector.rb', line 25

def get_requests(resources, blueprint, all_resources = false, global_vars = {})
  resources_list = []

  resources.each() do |res|
    resources_list << {
      :resource =>  res['resource'],
      :method =>  res['method'],
      :params =>  res['params']
    }
  end

  data = {
    :resources => resources_list,
    :blueprint => blueprint,
    :all_resources => all_resources,
    :global_vars => global_vars
  }.to_json()
  
  begin
    response = RestClient.post @apiary_url + @req_path, data, :content_type => :json, :accept => :json
    get_response(response, JSON.parse(response.to_str), nil, response.code.to_i)
  rescue RestClient::BadRequest, RestClient::InternalServerError  => e
    begin
      data = JSON.parse(e.http_body)
      get_response(nil, JSON.parse(e.http_body), data['error'], e.http_code.to_i)
    rescue
      get_response(nil, nil, e.to_s, e.http_code.to_i)
    end
  rescue Exception => e
    get_response(nil, nil, e.to_s, nil)
  end

end

#get_response(raw_resp, json_data, error, code) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/okapi/apiary_connector.rb', line 16

def get_response(raw_resp, json_data, error, code)
  { :resp => raw_resp,
    :data => json_data ? json_data['requests'] : nil,
    :status => json_data ? json_data['status'] : nil ,
    :error => json_data ? json_data['error'] || error : error,
    :code => code
    }
end

#get_results(resources, blueprint) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/okapi/apiary_connector.rb', line 59

def get_results(resources, blueprint)
  resources_list = []
  resources.each() do |res|
    resources_list << {
      :request => {
        :uri =>  res.uri,
        :expandedUri => res.expanded_uri,
        :method =>  res.method,
        :headers =>  res.headers,
        :body =>  res.body,
      },
      :response => {
        :status =>  res.response.status,
        :headers => res.response.headers ,
        :body =>  res.response.body
      }
    }
  end

  data = {
    :resources => resources_list,
    :blueprint => blueprint
  }.to_json()

  begin
    response = RestClient.post @apiary_url + @res_path, data, :content_type => :json, :accept => :json
    get_response(response, JSON.parse(response.to_str), nil, response.code.to_i)
  rescue RestClient::BadRequest, RestClient::InternalServerError  => e
    begin
      get_response(nil, JSON.parse(e.http_body), data['error'], e.http_code.to_i)
    rescue
      get_response(nil, nil, e.to_s, e.http_code.to_i)
    end
  rescue Exception => e
    get_response(nil, nil, e.to_s, nil)
  end
end