Class: LivePaper::BaseObject

Inherits:
Object
  • Object
show all
Defined in:
lib/live_paper/base_object.rb

Direct Known Subclasses

Link, Payoff, Trigger

Constant Summary collapse

LP_API_HOST =
"https://www.livepaperapi.com"
AUTH_URL =
"#{LP_API_HOST}/auth/v2/token"
AUTH_VALIDATION_URL =
"#{LP_API_HOST}/auth/v2/validate"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ BaseObject

Returns a new instance of BaseObject.



22
23
24
# File 'lib/live_paper/base_object.rb', line 22

def initialize(data={})
  assign_attributes data
end

Instance Attribute Details

#date_createdObject

Returns the value of attribute date_created.



13
14
15
# File 'lib/live_paper/base_object.rb', line 13

def date_created
  @date_created
end

#date_modifiedObject

Returns the value of attribute date_modified.



13
14
15
# File 'lib/live_paper/base_object.rb', line 13

def date_modified
  @date_modified
end

#idObject

Returns the value of attribute id.



13
14
15
# File 'lib/live_paper/base_object.rb', line 13

def id
  @id
end

Returns the value of attribute link.



13
14
15
# File 'lib/live_paper/base_object.rb', line 13

def link
  @link
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/live_paper/base_object.rb', line 13

def name
  @name
end

Class Method Details

.api_urlObject

Raises:

  • (NotImplementedError)


179
180
181
# File 'lib/live_paper/base_object.rb', line 179

def self.api_url
  raise NotImplementedError
end

.create(data) ⇒ Object



26
27
28
# File 'lib/live_paper/base_object.rb', line 26

def self.create(data)
  self.new(data).save
end

.get(id) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/live_paper/base_object.rb', line 39

def self.get(id)
  response = rest_request( "#{api_url}/#{id}", :get )
  case response.code
    when 200
      parse response.body
    else #when 404
      nil
  end
end

.item_keyObject

Raises:

  • (NotImplementedError)


187
188
189
# File 'lib/live_paper/base_object.rb', line 187

def self.item_key
  raise NotImplementedError
end

.listObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/live_paper/base_object.rb', line 49

def self.list
  objects=[]
  # $lpp_access_token = 'force retry'

  response = rest_request( api_url, :get )
  JSON.parse(response.body, symbolize_names: true)[list_key].each do |linkdata|
    objects << self.parse({item_key => linkdata}.to_json)
  end
  objects
end

.list_keyObject

Raises:

  • (NotImplementedError)


183
184
185
# File 'lib/live_paper/base_object.rb', line 183

def self.list_key
  raise NotImplementedError
end

.parse(data) ⇒ Object



171
172
173
# File 'lib/live_paper/base_object.rb', line 171

def self.parse(data)
  self.new().parse(data)
end

.request_access_tokenObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/live_paper/base_object.rb', line 132

def self.request_access_token
  h = { method: :post,
        url: AUTH_URL,
        headers: { authorization: "Basic #{$lpp_basic_auth}",
                   content_type: 'application/x-www-form-urlencoded',
                   accept: 'application/json' },
        payload: 'grant_type=client_credentials&scope=all'
      }

  response = RestClient::Request.execute(h) { |response, request, result| response }

  parsed = JSON.parse(response.body)
  @access_token = parsed['accessToken']
  $lpp_access_token = @access_token
end

.request_project_idObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/live_paper/base_object.rb', line 148

def self.request_project_id
  project = nil

  RestClient.proxy = ENV['HTTP_PROXY'] || ENV['http_proxy']
  request = {}.to_json
  res = RestClient.post AUTH_VALIDATION_URL, request, { :Authorization => "Bearer #{$lpp_access_token}", :Accept => 'application/json'}
  @project = res.headers[:project_id]

  if @project.nil?
    raise "Project id not in response header"
  end

  $project_id = @project
end

.rest_request(url, method, options = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/live_paper/base_object.rb', line 99

def self.rest_request(url, method, options={})
  tries = 0
  verb = (method||"get").downcase.to_sym
  raise "Method '#{verb}' not supported." unless [:get, :post, :put, :delete].include?(verb)

  request_access_token unless $lpp_access_token
  request_project_id unless $project_id
  headers = {}
  headers[:authorization] = "Bearer #{$lpp_access_token}"
  headers[:accept] = options[:accept] || "application/json"
  headers[:"X-user-info"] = 'app=rubygem'
  headers[:content_type] = 'application/json' unless options[:body].nil?

  h = {:method => verb, :url => url.to_s, :headers => headers}
  h.merge!({:payload => options[:body]}) unless options[:body].nil?

  begin
    response = RestClient::Request.execute(h) { |response, request, result| response }
    raise NotAuthenticatedError if response.code == 401
  rescue NotAuthenticatedError => e
    tries += 1
    if tries < 3
      request_access_token
      request_project_id
      headers[:authorization] = "Bearer #{$lpp_access_token}"
      retry
    else
      raise e
    end
  end
  response
end

Instance Method Details

#assign_attributes(data) ⇒ Object



15
16
17
18
19
20
# File 'lib/live_paper/base_object.rb', line 15

def assign_attributes(data)
  data.each do |key, value|
    method = "#{underscore key.to_s}="
    public_send(method, value) if respond_to?(method)
  end unless data.empty?
end

#deleteObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/live_paper/base_object.rb', line 81

def delete
  if self.id
    response = BaseObject.rest_request( "#{self.class.api_url}/#{id}", :delete )
    response_code = case response.code
      when 200
        '200 OK'
      when 409
        @errors=response.body
        'Conflict'
      else
        'unknown'
    end
  else
    response_code = "Object Invalid"
  end
  response_code
end

#errorsObject



163
164
165
166
167
168
169
# File 'lib/live_paper/base_object.rb', line 163

def errors
  begin
    JSON.parse(@errors)
  rescue
    @errors
  end
end

#parse(data) ⇒ Object

Raises:

  • (NotImplementedError)


175
176
177
# File 'lib/live_paper/base_object.rb', line 175

def parse(data)
  raise NotImplementedError
end

#rel(key) ⇒ Object



191
192
193
# File 'lib/live_paper/base_object.rb', line 191

def rel(key)
  link.find { |obj| obj[:rel] == key }[:href] rescue nil
end

#saveObject



30
31
32
33
34
35
36
37
# File 'lib/live_paper/base_object.rb', line 30

def save
  validate_attributes!
  unless present? @id
    response = BaseObject.rest_request( self.class.api_url, :post, body: create_body.to_json )
    parse(response.body)
  end
  self
end

#updateObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/live_paper/base_object.rb', line 60

def update
  response_code = 'Object Invalid'
  if self.id
    response = BaseObject.rest_request( "#{self.class.api_url}/#{id}", :put, body: update_body.to_json )
    response_code = case response.code
      when 200
        parse(response.body)
        '200 OK'
      when 400
        @errors=response.body
        'Bad Request'
      when 409
        @errors=response.body
        'Conflict'
      else
        'Object Invalid'
    end
  end
  response_code
end