Module: Etsy::Model::ClassMethods
- Defined in:
- lib/etsy/model.rb
Instance Method Summary collapse
-
#association(name, options = {}) ⇒ Object
FIXME: not quite sure where I’m going with this yet.
- #attribute(name, options = {}) ⇒ Object
- #attributes(*names) ⇒ Object
- #find_one_or_more(endpoint, identifiers_and_options) ⇒ Object
- #get(endpoint, options = {}) ⇒ Object
- #get_all(endpoint, options = {}) ⇒ Object
- #options_from(argument) ⇒ Object
- #post(endpoint, options = {}) ⇒ Object
- #put(endpoint, options = {}) ⇒ Object
Instance Method Details
#association(name, options = {}) ⇒ Object
FIXME: not quite sure where I’m going with this yet. KO.
17 18 19 20 21 |
# File 'lib/etsy/model.rb', line 17 def association(name, = {}) define_method "associated_#{name}" do @result[.fetch(:from, name).to_s] end end |
#attribute(name, options = {}) ⇒ Object
6 7 8 9 10 |
# File 'lib/etsy/model.rb', line 6 def attribute(name, = {}) define_method name do @result[.fetch(:from, name).to_s] end end |
#attributes(*names) ⇒ Object
12 13 14 |
# File 'lib/etsy/model.rb', line 12 def attributes(*names) names.each {|name| attribute(name) } end |
#find_one_or_more(endpoint, identifiers_and_options) ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/etsy/model.rb', line 87 def find_one_or_more(endpoint, ) = () append = .delete(:append_to_endpoint) append = append.nil? ? "" : "/#{append}" identifiers = get("/#{endpoint}/#{identifiers.join(',')}#{append}", ) end |
#get(endpoint, options = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/etsy/model.rb', line 23 def get(endpoint, = {}) objects = get_all(endpoint, ) if objects.length == 0 nil elsif objects.length == 1 objects[0] else objects end end |
#get_all(endpoint, options = {}) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 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 72 73 74 75 76 77 |
# File 'lib/etsy/model.rb', line 34 def get_all(endpoint, ={}) limit = [:limit] if limit initial_offset = .fetch(:offset, 0) batch_size = .fetch(:batch_size, 100) result = [] if limit == :all response = Request.get(endpoint, .merge(:limit => batch_size, :offset => initial_offset)) result << response.result limit = [response.count - batch_size - initial_offset, 0].max initial_offset += batch_size end num_batches = limit / batch_size num_batches.times do |batch| total_offset = initial_offset + batch * batch_size response = Request.get(endpoint, .merge(:limit => batch_size, :offset => total_offset)) result << response.result end remainder = limit % batch_size if remainder > 0 total_offset = initial_offset + num_batches * batch_size response = Request.get(endpoint, .merge(:limit => remainder, :offset => total_offset)) result << response.result end else response = Request.get(endpoint, ) result = response.result end [result].flatten.map do |data| if [:access_token] && [:access_secret] new(data, [:access_token], [:access_secret]) else new(data) end end end |
#options_from(argument) ⇒ Object
95 96 97 |
# File 'lib/etsy/model.rb', line 95 def (argument) (argument.last.class == Hash) ? argument.pop : {} end |