Class: Chiketto::Resource
- Inherits:
-
Object
- Object
- Chiketto::Resource
show all
- Includes:
- AttrDSL
- Defined in:
- lib/chiketto/resource.rb
Direct Known Subclasses
Address, Attendee, AttendeeProfile, Category, Choice, Email, Event, Organizer, Question, TicketClass, User, Venue
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from AttrDSL
included
Constructor Details
#initialize(args = {}) ⇒ Resource
Returns a new instance of Resource.
11
12
13
14
15
|
# File 'lib/chiketto/resource.rb', line 11
def initialize(args = {})
args.each do |k, v|
instance_variable_set("@#{k}", v) unless v.nil?
end
end
|
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
9
10
11
|
# File 'lib/chiketto/resource.rb', line 9
def id
@id
end
|
Class Method Details
.build_query_string(params) ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/chiketto/resource.rb', line 47
def self.build_query_string(params)
query_params = ''
stack = []
params.each do |k, v|
if v.is_a?(Hash)
stack << [k, v]
else
query_params << "#{k}=#{CGI::escape(v)}&"
end
end
stack.each do |parent, hash|
hash.each do |k, v|
if v.is_a?(Hash)
stack << ["#{parent}[#{k}]", v]
else
query_params << "#{parent}[#{k}]=#{CGI::escape(v)}&"
end
end
end
query_params.chop!
"&#{query_params}"
end
|
.endpoint(uri) ⇒ Object
31
32
33
|
# File 'lib/chiketto/resource.rb', line 31
def self.endpoint(uri)
ENDPOINT + uri + token
end
|
.get(uri, params = {}) ⇒ Object
17
18
19
20
21
22
23
|
# File 'lib/chiketto/resource.rb', line 17
def self.get(uri, params = {})
uri = endpoint(uri) + build_query_string(params)
resource = open uri
JSON.parse resource.read
rescue OpenURI::HTTPError => e
raise e unless e.message =~ /^404/
end
|
.open_post(uri) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/chiketto/resource.rb', line 35
def self.open_post(uri)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri)
request.add_field 'Authorization', "Bearer #{Chiketto.api_key}"
response = http.request(request)
if response.code !~ /20\d/
raise Chiketto::Exception, JSON.parse(response.body)
end
response.body
end
|
.paginated(resource, id, params) ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/chiketto/resource.rb', line 81
def self.paginated(resource, id, params)
page = 0
resources = []
loop do
page += 1
response = self.send("find_#{resource}", id, params.merge(page: page.to_s))
resources.concat response[resource.to_s]
break unless should_paginate(response['pagination'])
end
resources
end
|
.post(uri, params = {}) ⇒ Object
25
26
27
28
29
|
# File 'lib/chiketto/resource.rb', line 25
def self.post(uri, params = {})
uri = URI.parse endpoint(uri) + build_query_string(params)
resource = open_post uri
JSON.parse resource
end
|
.should_paginate(pagination) ⇒ Object
77
78
79
|
# File 'lib/chiketto/resource.rb', line 77
def self.should_paginate()
['page_count'] > ['page_number']
end
|
.token ⇒ Object
73
74
75
|
# File 'lib/chiketto/resource.rb', line 73
def self.token
"/?token=#{Chiketto.api_key}"
end
|