Class: Fortnox::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/fortnox/resource.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Resource

Returns a new instance of Resource.



53
54
55
56
57
58
59
60
# File 'lib/fortnox/resource.rb', line 53

def initialize(attributes = {})
  Hash(attributes[self.class.name.demodulize]).each do |key, value|
    next if /@/ === key
    public_send("#{key.underscore}=", value)
  end

  RelationBuilder.new(self, attributes).call
end

Class Attribute Details

.default_search_optionsObject



25
26
27
# File 'lib/fortnox/resource.rb', line 25

def default_search_options
  @default_search_options ||= { page: 1, page_size: 50 }
end

Class Method Details

.build_objects(data) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/fortnox/resource.rb', line 15

def build_objects(data)
  case data
  when Hash then new(data)
  when Array then data.map{|item| build_objects(item) }
  else data
  end
end

.create(attributes) ⇒ Object



43
44
45
# File 'lib/fortnox/resource.rb', line 43

def create(attributes)
  perform_request(:post, "/#{endpoint}", attributes)
end

.endpoint(value = nil) ⇒ Object



29
30
31
32
# File 'lib/fortnox/resource.rb', line 29

def endpoint(value = nil)
  @endpoint = value if value
  @endpoint || name.demodulize.downcase.pluralize
end

.fetch(id = nil) ⇒ Object



39
40
41
# File 'lib/fortnox/resource.rb', line 39

def fetch(id = nil)
  perform_request(:get, "/#{endpoint}/#{id}")
end

.perform_request(type, url, params = {}) ⇒ Object



47
48
49
50
# File 'lib/fortnox/resource.rb', line 47

def perform_request(type, url, params = {})
  response = Request.new(type, url, params).execute
  build_objects(response)
end

.relation(name, type, class_override: nil) ⇒ Object



10
11
12
13
# File 'lib/fortnox/resource.rb', line 10

def relation(name, type, class_override: nil)
  relations << Relation.new(name, type, class_override: class_override)
  attr_accessor name
end

.relationsObject



6
7
8
# File 'lib/fortnox/resource.rb', line 6

def relations
  @relations ||= Set.new
end

.search(options = {}) ⇒ Object



34
35
36
37
# File 'lib/fortnox/resource.rb', line 34

def search(options = {})
  query = default_search_options.merge(options)
  perform_request(:get, "/#{endpoint}", query)
end

Instance Method Details

#endpointObject



90
91
92
# File 'lib/fortnox/resource.rb', line 90

def endpoint
  self.class.endpoint
end

#to_hashObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fortnox/resource.rb', line 66

def to_hash
  fields_mapping = JSONFieldsMapper.mapping

  instance_variables
    .each_with_object({}) do |variable, obj|
      value = instance_variable_get(variable)

      case value
      when Resource
        value = value.to_hash
      when Array
        value = value.map(&:to_hash)
      end

      field = variable[1..-1]
      key = fields_mapping.fetch(field.to_sym, field.camelize)
      obj[key] = value
    end
end

#to_json(*args) ⇒ Object



62
63
64
# File 'lib/fortnox/resource.rb', line 62

def to_json(*args)
  to_hash.to_json(*args)
end

#update(attributes) ⇒ Object



86
87
88
# File 'lib/fortnox/resource.rb', line 86

def update(attributes)
  self.class.perform_request(:put, "#{endpoint}/#{id}", attributes)
end