Module: Ravelry::Build

Included in:
Pattern
Defined in:
lib/ravelry/utils/build.rb

Overview

Utility module that takes the JSON response from the API call and creates the appropriate objects.

Class Method Summary collapse

Class Method Details

.author(data) ⇒ Object

Creates and returns a Author object.

See Author for more information.



11
12
13
14
15
16
17
# File 'lib/ravelry/utils/build.rb', line 11

def self.author(data)
  if data[:pattern_author]
    @author = Author.new(data[:pattern_author])
  else
    @author = nil
  end
end

.categories(data) ⇒ Object

Creates and returns an array of Category objects.

See Category for more information.



23
24
25
26
27
# File 'lib/ravelry/utils/build.rb', line 23

def self.categories(data)
  @categories = data.fetch(:pattern_categories, []).map do |cat|
    Category.new(cat)
  end
end

.craft(data) ⇒ Object

Creates and returns a Craft object.

See Craft for more information.



33
34
35
36
37
38
39
# File 'lib/ravelry/utils/build.rb', line 33

def self.craft(data)
  if data[:craft]
    @craft = Craft.new(data[:craft])
  else
    @craft = nil
  end
end

.needles(data) ⇒ Object

Creates and returns an array of Needle objects.

There is more than one API endpoint for Needles. This may not be the one you are looking for.

See Needle for more information.



47
48
49
50
51
# File 'lib/ravelry/utils/build.rb', line 47

def self.needles(data)
  @needles = data.fetch(:pattern_needle_sizes, []).map do |ndl|
    Needle.new(ndl)
  end
end

.packs(data) ⇒ Object

Creates and returns an array of Pack objects.

See Pack for more information.



57
58
59
60
61
62
63
# File 'lib/ravelry/utils/build.rb', line 57

def self.packs(data)
  @packs = data.fetch(:packs, []).map do |pack_data|
    pack = Pack.new
    pack.data = pack_data
    pack
  end
end

.pattern_type(data) ⇒ Object

Creates and returns a PatternType object.

This is not the same as a PatternCategory object.

See PatternType for more information.



71
72
73
74
75
76
77
# File 'lib/ravelry/utils/build.rb', line 71

def self.pattern_type(data)
  if data[:pattern_type]
    @pattern_type = PatternType.new(data[:pattern_type])
  else
    @pattern_type = nil
  end
end

.photos(data) ⇒ Object

Creates and returns an array of Photo objects.

See Photo for more information.



83
84
85
86
87
88
89
# File 'lib/ravelry/utils/build.rb', line 83

def self.photos(data)
  @photos = data.fetch(:photos, []).map do |photo_data|
    photo = Photo.new(photo_data[:id])
    photo.data = photo_data
    photo
  end
end

.printings(data) ⇒ Object

Creates and returns an array of Printing objects.

See Printing for more information.



95
96
97
98
99
# File 'lib/ravelry/utils/build.rb', line 95

def self.printings(data)
  @printings = data.fetch(:printings, []).map do |printing_data|
    Printing.new(printing_data)
  end
end

.user_sites(data) ⇒ Object

Creates and returns an array of UserSite objects.

See UserSite for more information.



105
106
107
108
109
# File 'lib/ravelry/utils/build.rb', line 105

def self.user_sites(data)
  @user_sites = data.fetch(:user_sites, []).map do |site|
    UserSite.new(site)
  end
end

.yarn_weights(data) ⇒ Object

Creates and returns an array of YarnWeight objects.

See YarnWeight for more information.



129
130
131
132
133
134
135
136
137
# File 'lib/ravelry/utils/build.rb', line 129

def self.yarn_weights(data)
  packs = data.fetch(:packs, []).select { |pack| pack[:yarn_weight] }

  @yarn_weights = packs.map do |pack|
    yarn_weight = YarnWeight.new
    yarn_weight.data = pack[:yarn_weight]
    yarn_weight
  end
end

.yarns(data) ⇒ Object

Creates and returns an array of Yarn objects.

See Yarn for more information.



115
116
117
118
119
120
121
122
123
# File 'lib/ravelry/utils/build.rb', line 115

def self.yarns(data)
  packs = data.fetch(:packs, []).select { |pack| pack[:yarn] }

  @yarns = packs.map do |pack|
    yarn = Yarn.new
    yarn.data = pack[:yarn]
    yarn
  end
end