Module: Etsy::Model::ClassMethods

Defined in:
lib/etsy/model.rb

Instance Method Summary collapse

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, options = {})
  define_method "associated_#{name}" do
    @result[options.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, options = {})
  define_method name do
    @result[options.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, identifiers_and_options)
  options = options_from(identifiers_and_options)
  append = options.delete(:append_to_endpoint)
  append = append.nil? ? "" : "/#{append}"
  identifiers = identifiers_and_options
  get("/#{endpoint}/#{identifiers.join(',')}#{append}", options)
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, options = {})
  objects = get_all(endpoint, options)
  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, options={})
  limit = options[:limit]

  if limit
    initial_offset = options.fetch(:offset, 0)
    batch_size = options.fetch(:batch_size, 100)

    result = []

    if limit == :all
      response = Request.get(endpoint, options.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, options.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, options.merge(:limit => remainder, :offset => total_offset))
      result << response.result
    end
  else
    response = Request.get(endpoint, options)
    result = response.result
  end

  [result].flatten.map do |data|
    if options[:access_token] && options[:access_secret]
      new(data, options[:access_token], options[:access_secret])
    else
      new(data)
    end
  end
end

#options_from(argument) ⇒ Object



95
96
97
# File 'lib/etsy/model.rb', line 95

def options_from(argument)
  (argument.last.class == Hash) ? argument.pop : {}
end

#post(endpoint, options = {}) ⇒ Object



79
80
81
# File 'lib/etsy/model.rb', line 79

def post(endpoint, options={})
  Request.post(endpoint, options)
end

#put(endpoint, options = {}) ⇒ Object



83
84
85
# File 'lib/etsy/model.rb', line 83

def put(endpoint, options={})
  Request.put(endpoint, options)
end