Class: Harvest::Client

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

Overview

Harvest client interface

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, state: { filtered: {} }) ⇒ Client

Returns a new instance of Client.

Parameters:

  • config (Struct::Config)

    Configuration Struct which provides attributes

  • state (Hash) (defaults to: { filtered: {} })

    State of the Client for FluentAPI



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/harvest.rb', line 38

def initialize(config, state: { filtered: {} })
  @config = config
  @client = Harvest::HTTP::Api.new(**@config)
  @factory = Harvest::ResourceFactory.new
  @state = state
  @admin_api = if active_user.is_admin
                 config.admin_api
               else
                 false
               end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object

Parameters:

  • meth (Symbol)
  • *args (Array)

    arguments passed to method.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/harvest.rb', line 75

def method_missing(meth, *args)
  if allowed?(meth)
    Harvest::Client.new(
      @config,
      state: merge_state(@state, meth, args)
    )
  else

    super
  end
rescue NoMethodError
  # require 'pry'; binding.pry
  raise Harvest::Exceptions::BadState, "#{meth} is an invalid state change."
end

Instance Attribute Details

#active_userObject (readonly)

Returns the value of attribute active_user.



34
35
36
# File 'lib/harvest.rb', line 34

def active_user
  @active_user
end

#clientObject (readonly)

Returns the value of attribute client.



34
35
36
# File 'lib/harvest.rb', line 34

def client
  @client
end

#factoryObject (readonly)

Returns the value of attribute factory.



34
35
36
# File 'lib/harvest.rb', line 34

def factory
  @factory
end

#stateObject (readonly)

Returns the value of attribute state.



34
35
36
# File 'lib/harvest.rb', line 34

def state
  @state
end

#time_entriesObject (readonly)

Returns the value of attribute time_entries.



34
35
36
# File 'lib/harvest.rb', line 34

def time_entries
  @time_entries
end

Instance Method Details

#allowed?(meth) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
# File 'lib/harvest.rb', line 54

def allowed?(meth)
  %i[
    projects
    project_tasks
    time_entry
    tasks
  ].include?(meth)
end

#change(**kwargs) ⇒ Object

Find single instance of resource



99
100
101
102
103
104
105
106
# File 'lib/harvest.rb', line 99

def change(**kwargs)
  @state[@state[:active]].map do |_obj|
    Harvest::Changers.const_get(to_class_name(@state[:active])).new.change(
      @factory, @client, active_user, @state, kwargs
    )
  end
  self
end

#cloneObject



63
64
65
# File 'lib/harvest.rb', line 63

def clone
  Harvest::Client.new(@config, state: @state.clone)
end

#create(**kwargs) ⇒ Object

Create an instance of object based on state



120
121
122
123
124
125
126
127
# File 'lib/harvest.rb', line 120

def create(**kwargs)
  @state[@state[:active]] = Harvest::Create.const_get(
    to_class_name(@state[:active])
  ).new.create(
    @factory, @client, active_user, @state, kwargs
  )
  self
end

#dataObject

Return data from the active state in the base state or filtered.



141
142
143
# File 'lib/harvest.rb', line 141

def data
  @state[@state[:active]]
end

#discover(**params) ⇒ Object

Discover resources



109
110
111
112
113
114
115
116
117
# File 'lib/harvest.rb', line 109

def discover(**params)
  @state[@state[:active]] = Harvest::Discovers
                            .const_get(to_class_name(@state[:active]))
                            .new
                            .discover(
                              @admin_api, @client, @factory, active_user, @state, params
                            )
  self
end

#find(id) ⇒ Object

Find single instance of resource



91
92
93
94
95
96
# File 'lib/harvest.rb', line 91

def find(id)
  @state[@state[:active]] = Harvest::Finders.const_get(
    to_class_name(@state[:active])
  ).new.find(@factory, @client, id)
  self
end

#map(&block) ⇒ Object

Map block of filtered items



136
137
138
# File 'lib/harvest.rb', line 136

def map(&block)
  @state[@state[:active]].map(&block)
end

#respond_to_missing?(meth) ⇒ Boolean

Parameters:

  • meth (Symbol)

Returns:

  • (Boolean)


69
70
71
# File 'lib/harvest.rb', line 69

def respond_to_missing?(meth)
  allowed?(meth)
end

#select(&block) ⇒ Object

Select a subset of all items depending on state



130
131
132
133
# File 'lib/harvest.rb', line 130

def select(&block)
  @state[@state[:active]] = @state[@state[:active]].select(&block)
  self
end